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