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()]);
}
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()]);
}
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;
}
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);
}
Aggregations