Search in sources :

Example 26 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project main by JohnPeng739.

the class QrCodeUtils method parseQrCode.

public static QrCodeResult parseQrCode(InputStream in) {
    try {
        MultiFormatReader reader = new MultiFormatReader();
        BufferedImage image = ImageIO.read(in);
        Map<DecodeHintType, Object> map = new HashMap<>();
        map.put(DecodeHintType.CHARACTER_SET, "ISO-8859-1");
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        Result result = reader.decode(binaryBitmap, map);
        return new QrCodeResult(result);
    } catch (IOException ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Read the QrCode fail.", ex);
        }
        throw new UserInterfaceFfeeErrorException(UserInterfaceFfeeErrorException.FfeeErrors.QRCODE_IO_FAIL);
    } catch (NotFoundException ex) {
        if (logger.isErrorEnabled()) {
            logger.error("The QrCode not found any content.", ex);
        }
        throw new UserInterfaceFfeeErrorException(UserInterfaceFfeeErrorException.FfeeErrors.QRCODE_DECODE_FAIL);
    }
}
Also used : HashMap(java.util.HashMap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) UserInterfaceFfeeErrorException(org.mx.tools.ffee.error.UserInterfaceFfeeErrorException) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource)

Example 27 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project notes by KevinBlandy.

the class QrCodeUtils method QRReader.

/**
 * ʶ���ά��
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String QRReader(InputStream qrCode) throws IOException, NotFoundException {
    MultiFormatReader formatReader = new MultiFormatReader();
    // ��ȡָ���Ķ�ά���ļ�
    BufferedImage bufferedImage = ImageIO.read(qrCode);
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
    Map hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    com.google.zxing.Result result = formatReader.decode(binaryBitmap, hints);
    // System.out.println("���������" + result.toString());
    // System.out.println("��ά���ʽ���ͣ�" + result.getBarcodeFormat());
    // System.out.println("��ά���ı����ݣ�" + result.getText());
    bufferedImage.flush();
    return result.getText();
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) HashMap(java.util.HashMap) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) HashMap(java.util.HashMap) Map(java.util.Map) BufferedImage(java.awt.image.BufferedImage)

Example 28 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project ProjectSE-Unicam by HTTPiego.

the class QRCodeGenerator method readQRCode.

/**
 * Restituisce la stringa rappresentata dal {@link QRCode} specificato.
 *
 * @param qrCode il {@code QRCode} di cui leggerne la stringa che rappresenta
 * @return la stringa rappresentata del {@code QRCode}
 * oppure {@code null} se il {@code QRCode} specificato non rappresenta una stringa valida
 */
public static String readQRCode(QRCode qrCode) {
    Result result = null;
    try {
        ByteArrayInputStream bytes = new ByteArrayInputStream(qrCode.getQRCodeImage());
        BufferedImage QRCodeImage = ImageIO.read(bytes);
        LuminanceSource source = new BufferedImageLuminanceSource(QRCodeImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Reader reader = new MultiFormatReader();
        result = reader.decode(bitmap);
    } catch (FormatException | NotFoundException | ChecksumException | IOException e) {
        e.printStackTrace();
    }
    return result != null ? result.getText() : null;
}
Also used : Reader(com.google.zxing.Reader) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource)

Example 29 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project keycloak by keycloak.

the class RequiredActionsTest method testBarcodeOtp.

private void testBarcodeOtp() throws Exception {
    // HtmlUnit browser cannot take screenshots
    assumeFalse(driver instanceof HtmlUnitDriver);
    TakesScreenshot screenshotDriver = (TakesScreenshot) driver;
    QRCodeReader qrCodeReader = new QRCodeReader();
    initiateRequiredAction(otpSetupPage);
    otpSetupPage.localeDropdown().selectAndAssert(CUSTOM_LOCALE_NAME);
    otpSetupPage.clickManualMode();
    otpSetupPage.clickBarcodeMode();
    assertTrue(otpSetupPage.isBarcodePresent());
    assertFalse(otpSetupPage.isSecretKeyPresent());
    assertTrue(otpSetupPage.feedbackMessage().isWarning());
    assertEquals("You need to set up Mobile Authenticator to activate your account.", otpSetupPage.feedbackMessage().getText());
    // empty input
    otpSetupPage.submit();
    assertTrue(otpSetupPage.feedbackMessage().isError());
    assertEquals("Please specify authenticator code.", otpSetupPage.feedbackMessage().getText());
    // take a screenshot of the QR code
    byte[] screenshot = screenshotDriver.getScreenshotAs(OutputType.BYTES);
    BufferedImage screenshotImg = ImageIO.read(new ByteArrayInputStream(screenshot));
    BinaryBitmap screenshotBinaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(screenshotImg)));
    Result qrCode = qrCodeReader.decode(screenshotBinaryBitmap);
    // parse the QR code string
    Pattern qrUriPattern = Pattern.compile("^otpauth:\\/\\/(?<type>.+)\\/(?<realm>.+):(?<user>.+)\\?secret=(?<secret>.+)&digits=(?<digits>.+)&algorithm=(?<algorithm>.+)&issuer=(?<issuer>.+)&(?:period=(?<period>.+)|counter=(?<counter>.+))$");
    Matcher qrUriMatcher = qrUriPattern.matcher(qrCode.getText());
    assertTrue(qrUriMatcher.find());
    // extract data
    String type = qrUriMatcher.group("type");
    String realm = qrUriMatcher.group("realm");
    String user = qrUriMatcher.group("user");
    String secret = qrUriMatcher.group("secret");
    int digits = Integer.parseInt(qrUriMatcher.group("digits"));
    String algorithm = qrUriMatcher.group("algorithm");
    String issuer = qrUriMatcher.group("issuer");
    Integer period = type.equals(TOTP) ? Integer.parseInt(qrUriMatcher.group("period")) : null;
    Integer counter = type.equals(HOTP) ? Integer.parseInt(qrUriMatcher.group("counter")) : null;
    RealmRepresentation realmRep = testRealmResource().toRepresentation();
    String expectedRealmName = realmRep.getDisplayName() != null && !realmRep.getDisplayName().isEmpty() ? realmRep.getDisplayName() : realmRep.getRealm();
    // basic assertations
    assertEquals(QR_CODE, qrCode.getBarcodeFormat());
    assertEquals(expectedRealmName, realm);
    assertEquals(expectedRealmName, issuer);
    assertEquals(testUser.getUsername(), user);
    // the actual test
    testOtp(type, algorithm, digits, period, counter, secret);
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) TakesScreenshot(org.openqa.selenium.TakesScreenshot) Result(com.google.zxing.Result) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap)

Example 30 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project java-apply by javachengwc.

the class ZXingUtil method decodeQRCodeImage.

/**
 * 解析二维码
 * @param imagePath 二维码图片存放路径(含文件名)
 * @param charset   解码二维码内容时采用的字符集(传null时默认采用UTF-8编码)
 * @return 解析成功后返回二维码文本,否则返回空字符串
 */
public static String decodeQRCodeImage(String imagePath, String charset) {
    BufferedImage image = null;
    try {
        image = ImageIO.read(new File(imagePath));
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
    if (null == image) {
        System.out.println("Could not decode QRCodeImage");
        return "";
    }
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
    Map<DecodeHintType, String> hints = new HashMap<DecodeHintType, String>();
    hints.put(DecodeHintType.CHARACTER_SET, charset == null ? "UTF-8" : charset);
    Result result = null;
    try {
        result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    } catch (NotFoundException e) {
        System.out.println("二维码图片[" + imagePath + "]解析失败,堆栈轨迹如下");
        e.printStackTrace();
        return "";
    }
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) HashMap(java.util.HashMap) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) File(java.io.File)

Aggregations

BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)51 HybridBinarizer (com.google.zxing.common.HybridBinarizer)51 BufferedImage (java.awt.image.BufferedImage)33 BinaryBitmap (com.google.zxing.BinaryBitmap)24 Result (com.google.zxing.Result)19 MultiFormatReader (com.google.zxing.MultiFormatReader)17 HashMap (java.util.HashMap)15 DecodeHintType (com.google.zxing.DecodeHintType)10 File (java.io.File)10 Hashtable (java.util.Hashtable)10 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)9 LuminanceSource (com.google.zxing.LuminanceSource)8 IOException (java.io.IOException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Binarizer (com.google.zxing.Binarizer)3 NotFoundException (com.google.zxing.NotFoundException)3 Reader (com.google.zxing.Reader)3 ReaderException (com.google.zxing.ReaderException)3 Map (java.util.Map)3 JSONObject (com.alibaba.fastjson.JSONObject)2