Search in sources :

Example 46 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project document-management-system by openkm.

the class BarcodeTextExtractor method simple.

/**
 * Decode only one barcode
 */
@SuppressWarnings("unused")
private String simple(BufferedImage img) throws NotFoundException, ChecksumException, FormatException {
    long begin = System.currentTimeMillis();
    LuminanceSource source = new BufferedImageLuminanceSource(img);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    com.google.zxing.Reader reader = new MultiFormatReader();
    Result result = reader.decode(bitmap);
    SystemProfiling.log(null, System.currentTimeMillis() - begin);
    log.trace("simple.Time: {}", System.currentTimeMillis() - begin);
    return result.getText();
}
Also used : BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) com.google.zxing(com.google.zxing) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 47 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project document-management-system by openkm.

the class BarcodeTextExtractor method multiple.

/**
 * Decode all barcodes in the image
 */
private String multiple(BufferedImage img) throws NotFoundException, ChecksumException, FormatException {
    long begin = System.currentTimeMillis();
    LuminanceSource source = new BufferedImageLuminanceSource(img);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    com.google.zxing.Reader reader = new MultiFormatReader();
    MultipleBarcodeReader bcReader = new GenericMultipleBarcodeReader(reader);
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
    hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    StringBuilder sb = new StringBuilder();
    for (Result result : bcReader.decodeMultiple(bitmap, hints)) {
        sb.append(result.getText()).append(" ");
    }
    SystemProfiling.log(null, System.currentTimeMillis() - begin);
    log.trace("multiple.Time: {}", System.currentTimeMillis() - begin);
    return sb.toString();
}
Also used : GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) Hashtable(java.util.Hashtable) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) com.google.zxing(com.google.zxing) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource)

Example 48 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project orion-kit by lijiahangmax.

the class CodeGenerator method decode.

/**
 * 解析条码数据
 *
 * @param image img
 * @return data
 */
public String decode(BufferedImage image) {
    try {
        // 设置实际大小 如果不设置可能会导致解析失败
        image = image.getSubimage(0, imgTopMargin, width, height);
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        return new MultiFormatReader().decode(bitmap, this.getDecodeHint()).getText();
    } catch (Exception e) {
        return null;
    }
}
Also used : BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 49 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project jtelebot by stdmk.

the class Qr method getTextFromQr.

private String getTextFromQr(BufferedImage image) throws NotFoundException {
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
    Result result = new MultiFormatReader().decode(binaryBitmap);
    return result.getText();
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result)

Example 50 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project RuleApi by buxia97.

the class QRCodeUtil method parseQRCodeByUrl.

/**
 * 根据网络二维码图片解析二维码内容, 区别仅仅在于 ImageIO.read(url); 这一个重载的方法)
 *
 * @param url 二维码图片网络地址,如 https://res.wx.qq.com/mpres/htmledition/images/mp_qrcode3a7b38.gif
 * @return
 * @throws Exception
 */
public static String parseQRCodeByUrl(URL url) {
    String resultStr = null;
    if (url == null) {
        return resultStr;
    }
    try {
        /*
             * ImageIO 的 BufferedImage read(URL input) 方法用于读取网络图片文件转为内存缓冲图像
             * 同理还有:read(File input)、read(InputStream input)、、read(ImageInputStream stream)
             * 如果图片网络地址错误,比如不能访问,则 read 抛异常:javax.imageio.IIOException: Can't get input stream from URL!
             */
        BufferedImage bufferedImage = ImageIO.read(url);
        /*
             * com.google.zxing.client.j2se.BufferedImageLuminanceSource:缓冲图像亮度源
             * 将 java.awt.image.BufferedImage 转为 zxing 的 缓冲图像亮度源
             * 关键就是下面这几句:HybridBinarizer 用于读取二维码图像数据,BinaryBitmap 二进制位图
             */
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Hashtable hints = new Hashtable();
        /*
             * 如果内容包含中文,则解码的字符集格式应该和编码时一致
             */
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        /*
             * 如果图片不是二维码图片,则 decode 抛异常:com.google.zxing.NotFoundException
             * MultiFormatWriter 的 encode 用于对内容进行编码成 2D 矩阵
             * MultiFormatReader 的 decode 用于读取二进制位图数据
             */
        Result result = new MultiFormatReader().decode(bitmap, hints);
        resultStr = result.getText();
    } catch (IOException e) {
        e.printStackTrace();
        log.error("二维码图片地址错误, 地址是: {}!", url);
    } catch (NotFoundException e) {
        e.printStackTrace();
        log.error("图片非二维码图片, 地址是: {}!", url);
    }
    return resultStr;
}
Also used : Hashtable(java.util.Hashtable) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) IOException(java.io.IOException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage)

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