Search in sources :

Example 11 with QRCodeReader

use of com.google.zxing.qrcode.QRCodeReader in project android-zxing by PearceXu.

the class MultiFormatReader method setHints.

/**
 * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
 * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
 * is important for performance in continuous scan clients.
 *
 * @param hints The set of hints to use for subsequent calls to decode(image)
 */
public void setHints(Map<DecodeHintType, ?> hints) {
    this.hints = hints;
    boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
    @SuppressWarnings("unchecked") Collection<BarcodeFormat> formats = hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
    Collection<Reader> readers = new ArrayList<>();
    if (formats != null) {
        boolean addOneDReader = formats.contains(BarcodeFormat.UPC_A) || formats.contains(BarcodeFormat.UPC_E) || formats.contains(BarcodeFormat.EAN_13) || formats.contains(BarcodeFormat.EAN_8) || formats.contains(BarcodeFormat.CODABAR) || formats.contains(BarcodeFormat.CODE_39) || formats.contains(BarcodeFormat.CODE_93) || formats.contains(BarcodeFormat.CODE_128) || formats.contains(BarcodeFormat.ITF) || formats.contains(BarcodeFormat.RSS_14) || formats.contains(BarcodeFormat.RSS_EXPANDED);
        // Put 1D readers upfront in "normal" mode
        if (addOneDReader && !tryHarder) {
            readers.add(new MultiFormatOneDReader(hints));
        }
        if (formats.contains(BarcodeFormat.QR_CODE)) {
            readers.add(new QRCodeReader());
        }
        if (formats.contains(BarcodeFormat.DATA_MATRIX)) {
            readers.add(new DataMatrixReader());
        }
        if (formats.contains(BarcodeFormat.AZTEC)) {
            readers.add(new AztecReader());
        }
        if (formats.contains(BarcodeFormat.PDF_417)) {
            readers.add(new PDF417Reader());
        }
        if (formats.contains(BarcodeFormat.MAXICODE)) {
            readers.add(new MaxiCodeReader());
        }
        // At end in "try harder" mode
        if (addOneDReader && tryHarder) {
            readers.add(new MultiFormatOneDReader(hints));
        }
    }
    if (readers.isEmpty()) {
        if (!tryHarder) {
            readers.add(new MultiFormatOneDReader(hints));
        }
        readers.add(new QRCodeReader());
        readers.add(new DataMatrixReader());
        readers.add(new AztecReader());
        readers.add(new PDF417Reader());
        readers.add(new MaxiCodeReader());
        if (tryHarder) {
            readers.add(new MultiFormatOneDReader(hints));
        }
    }
    this.readers = readers.toArray(new Reader[readers.size()]);
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) DataMatrixReader(com.google.zxing.datamatrix.DataMatrixReader) AztecReader(com.google.zxing.aztec.AztecReader) ArrayList(java.util.ArrayList) MaxiCodeReader(com.google.zxing.maxicode.MaxiCodeReader) AztecReader(com.google.zxing.aztec.AztecReader) DataMatrixReader(com.google.zxing.datamatrix.DataMatrixReader) MultiFormatOneDReader(com.google.zxing.oned.MultiFormatOneDReader) PDF417Reader(com.google.zxing.pdf417.PDF417Reader) QRCodeReader(com.google.zxing.qrcode.QRCodeReader) MultiFormatOneDReader(com.google.zxing.oned.MultiFormatOneDReader) PDF417Reader(com.google.zxing.pdf417.PDF417Reader) MaxiCodeReader(com.google.zxing.maxicode.MaxiCodeReader)

Example 12 with QRCodeReader

use of com.google.zxing.qrcode.QRCodeReader in project incubator-weex by apache.

the class MultiFormatReader method setHints.

/**
 * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
 * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
 * is important for performance in continuous scan clients.
 *
 * @param hints The set of hints to use for subsequent calls to decode(image)
 */
public void setHints(Map<DecodeHintType, ?> hints) {
    this.hints = hints;
    boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
    @SuppressWarnings("unchecked") Collection<BarcodeFormat> formats = hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
    Collection<Reader> readers = new ArrayList<Reader>();
    if (formats != null) {
        boolean addOneDReader = formats.contains(BarcodeFormat.UPC_A) || formats.contains(BarcodeFormat.UPC_E) || formats.contains(BarcodeFormat.EAN_13) || formats.contains(BarcodeFormat.EAN_8) || formats.contains(BarcodeFormat.CODABAR) || formats.contains(BarcodeFormat.CODE_39) || formats.contains(BarcodeFormat.CODE_93) || formats.contains(BarcodeFormat.CODE_128) || formats.contains(BarcodeFormat.ITF) || formats.contains(BarcodeFormat.RSS_14) || formats.contains(BarcodeFormat.RSS_EXPANDED);
        // Put 1D readers upfront in "normal" mode
        if (addOneDReader && !tryHarder) {
            readers.add(new MultiFormatOneDReader(hints));
        }
        if (formats.contains(BarcodeFormat.QR_CODE)) {
            readers.add(new QRCodeReader());
        }
        if (formats.contains(BarcodeFormat.DATA_MATRIX)) {
            readers.add(new DataMatrixReader());
        }
        if (formats.contains(BarcodeFormat.AZTEC)) {
            readers.add(new AztecReader());
        }
        if (formats.contains(BarcodeFormat.PDF_417)) {
            readers.add(new PDF417Reader());
        }
        if (formats.contains(BarcodeFormat.MAXICODE)) {
            readers.add(new MaxiCodeReader());
        }
        // At end in "try harder" mode
        if (addOneDReader && tryHarder) {
            readers.add(new MultiFormatOneDReader(hints));
        }
    }
    if (readers.isEmpty()) {
        if (!tryHarder) {
            readers.add(new MultiFormatOneDReader(hints));
        }
        readers.add(new QRCodeReader());
        readers.add(new DataMatrixReader());
        readers.add(new AztecReader());
        readers.add(new PDF417Reader());
        readers.add(new MaxiCodeReader());
        if (tryHarder) {
            readers.add(new MultiFormatOneDReader(hints));
        }
    }
    this.readers = readers.toArray(new Reader[readers.size()]);
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) DataMatrixReader(com.google.zxing.datamatrix.DataMatrixReader) AztecReader(com.google.zxing.aztec.AztecReader) ArrayList(java.util.ArrayList) MaxiCodeReader(com.google.zxing.maxicode.MaxiCodeReader) AztecReader(com.google.zxing.aztec.AztecReader) DataMatrixReader(com.google.zxing.datamatrix.DataMatrixReader) MultiFormatOneDReader(com.google.zxing.oned.MultiFormatOneDReader) PDF417Reader(com.google.zxing.pdf417.PDF417Reader) QRCodeReader(com.google.zxing.qrcode.QRCodeReader) MultiFormatOneDReader(com.google.zxing.oned.MultiFormatOneDReader) PDF417Reader(com.google.zxing.pdf417.PDF417Reader) MaxiCodeReader(com.google.zxing.maxicode.MaxiCodeReader)

Example 13 with QRCodeReader

use of com.google.zxing.qrcode.QRCodeReader in project QrCodeScanner by ekibun.

the class Decoder method decode.

public static Result decode(byte[] data, int width, int height, Rect crop) {
    Result result = null;
    try {
        Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
        hints.put(DecodeHintType.CHARACTER_SET, "ISO-8859-1");
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
        /*
            Collection<BarcodeFormat> barcodeFormats = new ArrayList<>();
            barcodeFormats.add(BarcodeFormat.QR_CODE);
            barcodeFormats.add(BarcodeFormat.CODE_39);
            barcodeFormats.add(BarcodeFormat.CODE_93);
            barcodeFormats.add(BarcodeFormat.CODE_128);
            hints.put(DecodeHintType.POSSIBLE_FORMATS, barcodeFormats);*/
        PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, crop.left, crop.top, crop.width(), crop.height(), false);
        // BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
        result = new QRCodeReader().decode(bitmap1, hints);
    } catch (NotFoundException e) {
    // e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) NotFoundException(com.google.zxing.NotFoundException) ReaderException(com.google.zxing.ReaderException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Result(com.google.zxing.Result)

Example 14 with QRCodeReader

use of com.google.zxing.qrcode.QRCodeReader 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)

Aggregations

QRCodeReader (com.google.zxing.qrcode.QRCodeReader)14 HybridBinarizer (com.google.zxing.common.HybridBinarizer)10 BinaryBitmap (com.google.zxing.BinaryBitmap)8 DecodeHintType (com.google.zxing.DecodeHintType)6 NotFoundException (com.google.zxing.NotFoundException)4 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)4 Hashtable (java.util.Hashtable)4 BitmapFactory (android.graphics.BitmapFactory)3 ChecksumException (com.google.zxing.ChecksumException)3 FormatException (com.google.zxing.FormatException)3 AztecReader (com.google.zxing.aztec.AztecReader)3 DataMatrixReader (com.google.zxing.datamatrix.DataMatrixReader)3 MaxiCodeReader (com.google.zxing.maxicode.MaxiCodeReader)3 MultiFormatOneDReader (com.google.zxing.oned.MultiFormatOneDReader)3 PDF417Reader (com.google.zxing.pdf417.PDF417Reader)3 BufferedImage (java.awt.image.BufferedImage)3 ArrayList (java.util.ArrayList)3 Result (com.google.zxing.Result)2 IndexController (alfio.controller.IndexController)1 AdditionalServiceApiController (alfio.controller.api.admin.AdditionalServiceApiController)1