use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project anttu.code.github.io by anTtutu.
the class Demo method decoder.
/**
* 流图片解码
* @param input
* @return QRResult
*/
public static QRResult decoder(InputStream input) {
BufferedImage image;
try {
if (null == input) {
return new QRResult("得到的文件不存在!", 300);
}
image = ImageIO.read(input);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType, Object> hints = new LinkedHashMap<DecodeHintType, Object>();
// 解码设置编码方式为:utf-8,
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
// 优化精度
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
// 复杂模式,开启PURE_BARCODE模式
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
Result result = new MultiFormatReader().decode(bitmap, hints);
String txt = result.getText();
return new QRResult("成功解码!", 200, txt);
} catch (Exception e) {
// LoggerUtils.error(MatrixUtil.class,"解码失败。", e);
return new QRResult("解码失败,请确认的你二维码是否正确,或者图片有多个二维码!", 500);
}
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project okta-idx-java by okta.
the class QrCodePage method decodeQRCode.
private static String decodeQRCode(byte[] imageBytes) throws IOException, NotFoundException {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);
return qrCodeResult.getText();
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project OkapiBarcode by woo-j.
the class SymbolTest method verifySuccess.
/**
* Verifies that the specified symbol was encoded and rendered in a way that matches expectations.
*
* @param symbol the symbol to check
* @param actualError the actual error message
* @throws IOException if there is any I/O error
* @throws ReaderException if ZXing has an issue decoding the barcode image
*/
private void verifySuccess(Symbol symbol, String actualError) throws IOException, ReaderException {
assertEquals("error message", null, actualError);
// try to verify logs
String info = symbol.getEncodeInfo();
String[] actualLog = (!info.isEmpty() ? symbol.getEncodeInfo().split("\n") : new String[0]);
assertEquals("log size", config.expectedLog.size(), actualLog.length);
for (int i = 0; i < actualLog.length; i++) {
String expected = config.expectedLog.get(i).trim();
String actual = actualLog[i].trim();
assertEquals("at log line " + i, expected, actual);
}
try {
// try to verify codewords
int[] actualCodewords = symbol.getCodewords();
assertEquals("codeword count", config.expectedCodewords.size(), actualCodewords.length);
for (int i = 0; i < actualCodewords.length; i++) {
int expected = Integer.parseInt(config.expectedCodewords.get(i));
int actual = actualCodewords[i];
assertEquals("at codeword index " + i, expected, actual);
}
} catch (UnsupportedOperationException e) {
// codewords aren't supported, try to verify patterns
String[] actualPatterns = symbol.pattern;
assertEquals(config.expectedCodewords.size(), actualPatterns.length);
for (int i = 0; i < actualPatterns.length; i++) {
String expected = config.expectedCodewords.get(i);
String actual = actualPatterns[i];
assertEquals("at pattern index " + i, expected, actual);
}
}
// make sure the barcode images match
String parentName = pngFile.getParentFile().getName();
String pngName = pngFile.getName();
String dirName = parentName + "-" + pngName.substring(0, pngName.lastIndexOf('.'));
BufferedImage expected = ImageIO.read(pngFile);
BufferedImage actual = draw(symbol);
assertEqual(expected, actual, dirName);
// if possible, ensure an independent third party (ZXing) can read the generated barcode and agrees on what it represents
Reader zxingReader = findReader(symbol);
if (zxingReader != null) {
LuminanceSource source = new BufferedImageLuminanceSource(actual);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType, Boolean> hints = new HashMap<>();
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = zxingReader.decode(bitmap, hints);
String zxingData = massageZXingData(result.getText(), symbol);
String okapiData = massageOkapiData(symbol.getContent(), symbol);
assertEquals("checking against ZXing results", okapiData, zxingData);
}
// TODO: check against Zint?
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project sparrow by sparrowwallet.
the class WebcamService method readQR.
private Result readQR(BufferedImage bufferedImage) {
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
return qrReader.decode(bitmap, Map.of(DecodeHintType.TRY_HARDER, Boolean.TRUE));
} catch (ReaderException e) {
// fall thru, it means there is no QR code in image
return null;
}
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project uaa by cloudfoundry.
the class TotpMfaEndpointIntegrationTests method qrCodeText.
private String qrCodeText(String dataUrl) throws Exception {
QRCodeReader reader = new QRCodeReader();
String[] rawSplit = dataUrl.split(",");
assertEquals("data:image/png;base64", rawSplit[0]);
byte[] decodedByte = Base64.getDecoder().decode(rawSplit[1]);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedByte));
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
Map<DecodeHintType, Object> hintMap = new HashMap<>();
hintMap.put(DecodeHintType.PURE_BARCODE, true);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
return reader.decode(bitmap, hintMap).getText();
}
Aggregations