Search in sources :

Example 36 with BufferedImageLuminanceSource

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);
    }
}
Also used : HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) LinkedHashMap(java.util.LinkedHashMap) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource)

Example 37 with BufferedImageLuminanceSource

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();
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result)

Example 38 with BufferedImageLuminanceSource

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?
}
Also used : DecodeHintType(com.google.zxing.DecodeHintType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Code93Reader(com.google.zxing.oned.Code93Reader) PDF417Reader(com.google.zxing.pdf417.PDF417Reader) RSS14Reader(com.google.zxing.oned.rss.RSS14Reader) QRCodeReader(com.google.zxing.qrcode.QRCodeReader) EAN13Reader(com.google.zxing.oned.EAN13Reader) AztecReader(com.google.zxing.aztec.AztecReader) CodaBarReader(com.google.zxing.oned.CodaBarReader) UPCAReader(com.google.zxing.oned.UPCAReader) RSSExpandedReader(com.google.zxing.oned.rss.expanded.RSSExpandedReader) Code128Reader(com.google.zxing.oned.Code128Reader) UPCEReader(com.google.zxing.oned.UPCEReader) DataMatrixReader(com.google.zxing.datamatrix.DataMatrixReader) EAN8Reader(com.google.zxing.oned.EAN8Reader) Reader(com.google.zxing.Reader) Code39Reader(com.google.zxing.oned.Code39Reader) Integer.toHexString(java.lang.Integer.toHexString) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap)

Example 39 with BufferedImageLuminanceSource

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;
    }
}
Also used : BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 40 with BufferedImageLuminanceSource

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();
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) DecodeHintType(com.google.zxing.DecodeHintType) HashMap(java.util.HashMap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap)

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