Search in sources :

Example 16 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project pancm_project by xuwujing.

the class QrCodeCreateUtil method readQrCode.

/**
 * 读二维码并输出携带的信息
 */
public static void readQrCode(InputStream inputStream) throws IOException {
    // 从输入流中获取字符串信息
    BufferedImage image = ImageIO.read(inputStream);
    // 将图像转换为二进制位图源
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    Result result = null;
    try {
        result = reader.decode(bitmap);
    } catch (ReaderException e) {
        e.printStackTrace();
    }
    System.out.println(result.getText());
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) 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) ReaderException(com.google.zxing.ReaderException)

Example 17 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project elastic-core-maven by OrdinaryDude.

the class DecodeQRCode method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest request) throws NxtException {
    String qrCodeBase64 = Convert.nullToEmpty(request.getParameter("qrCodeBase64"));
    JSONObject response = new JSONObject();
    try {
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(qrCodeBase64))))));
        Map hints = new HashMap();
        hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.of(BarcodeFormat.QR_CODE));
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result qrCodeData = new MultiFormatReader().decode(binaryBitmap, hints);
        response.put("qrCodeData", qrCodeData.getText());
    } catch (IOException ex) {
        String errorMessage = "Error reading base64 byte stream";
        Logger.logErrorMessage(errorMessage, ex);
        JSONData.putException(response, ex, errorMessage);
    } catch (NullPointerException ex) {
        String errorMessage = "Invalid base64 image";
        Logger.logErrorMessage(errorMessage, ex);
        JSONData.putException(response, ex, errorMessage);
    } catch (NotFoundException ex) {
        response.put("qrCodeData", "");
    }
    return response;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) HashMap(java.util.HashMap) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) JSONObject(org.json.simple.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project RxTools by vondear.

the class RxQrBarTool method decodeFromPhoto.

/**
 * 解析图片中的 二维码 或者 条形码
 *
 * @param photo 待解析的图片
 * @return Result 解析结果,解析识别时返回NULL
 */
public static Result decodeFromPhoto(Bitmap photo) {
    Result rawResult = null;
    if (photo != null) {
        // 为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存
        Bitmap smallBitmap = RxImageTool.zoomBitmap(photo, photo.getWidth() / 2, photo.getHeight() / 2);
        // 释放原始图片占用的内存,防止out of memory异常发生
        photo.recycle();
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        // 解码的参数
        Hashtable<DecodeHintType, Object> hints = new Hashtable<>(2);
        // 可以解析的编码类型
        Vector<BarcodeFormat> decodeFormats = new Vector<>();
        if (decodeFormats.isEmpty()) {
            decodeFormats = new Vector<>();
            Vector<BarcodeFormat> PRODUCT_FORMATS = new Vector<>(5);
            PRODUCT_FORMATS.add(BarcodeFormat.UPC_A);
            PRODUCT_FORMATS.add(BarcodeFormat.UPC_E);
            PRODUCT_FORMATS.add(BarcodeFormat.EAN_13);
            PRODUCT_FORMATS.add(BarcodeFormat.EAN_8);
            // PRODUCT_FORMATS.add(BarcodeFormat.RSS14);
            Vector<BarcodeFormat> ONE_D_FORMATS = new Vector<>(PRODUCT_FORMATS.size() + 4);
            ONE_D_FORMATS.addAll(PRODUCT_FORMATS);
            ONE_D_FORMATS.add(BarcodeFormat.CODE_39);
            ONE_D_FORMATS.add(BarcodeFormat.CODE_93);
            ONE_D_FORMATS.add(BarcodeFormat.CODE_128);
            ONE_D_FORMATS.add(BarcodeFormat.ITF);
            Vector<BarcodeFormat> QR_CODE_FORMATS = new Vector<>(1);
            QR_CODE_FORMATS.add(BarcodeFormat.QR_CODE);
            Vector<BarcodeFormat> DATA_MATRIX_FORMATS = new Vector<>(1);
            DATA_MATRIX_FORMATS.add(BarcodeFormat.DATA_MATRIX);
            // 这里设置可扫描的类型,我这里选择了都支持
            decodeFormats.addAll(ONE_D_FORMATS);
            decodeFormats.addAll(QR_CODE_FORMATS);
            decodeFormats.addAll(DATA_MATRIX_FORMATS);
        }
        hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
        // 设置继续的字符编码格式为UTF8
        // hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
        // 设置解析配置参数
        multiFormatReader.setHints(hints);
        // 开始对图像资源解码
        try {
            rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(smallBitmap))));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return rawResult;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) BarcodeFormat(com.google.zxing.BarcodeFormat) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) BitmapLuminanceSource(com.vondear.rxtools.module.scaner.BitmapLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) Vector(java.util.Vector)

Example 19 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project portal by ixinportal.

the class parseQRCodeTool method parseQRCode.

public static String[] parseQRCode(InputStream imageInputStream) throws Exception {
    BufferedImage image = ImageIO.read(imageInputStream);
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    Binarizer binarizer = new HybridBinarizer(source);
    BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
    Map<DecodeHintType, Object> hints = new HashMap<>();
    hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
    MultiFormatReader formatReader = new MultiFormatReader();
    Result result = formatReader.decode(binaryBitmap, hints);
    String[] eInvoiceInfo = getEinvoiceInfo(result.getText());
    return eInvoiceInfo;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) HashMap(java.util.HashMap) 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) Binarizer(com.google.zxing.Binarizer) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 20 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project react-native-camera by react-native-community.

the class BarCodeScannerAsyncTask method doInBackground.

@Override
protected Result doInBackground(Void... ignored) {
    if (isCancelled() || mDelegate == null) {
        return null;
    }
    Result result = null;
    try {
        BinaryBitmap bitmap = generateBitmapFromImageData(mImageData, mWidth, mHeight);
        result = mMultiFormatReader.decodeWithState(bitmap);
    } catch (NotFoundException e) {
    // No barcode found, result is already null.
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return result;
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) Result(com.google.zxing.Result)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)78 Result (com.google.zxing.Result)62 HybridBinarizer (com.google.zxing.common.HybridBinarizer)61 ReaderException (com.google.zxing.ReaderException)39 Message (android.os.Message)24 Bundle (android.os.Bundle)23 BufferedImage (java.awt.image.BufferedImage)21 MultiFormatReader (com.google.zxing.MultiFormatReader)20 NotFoundException (com.google.zxing.NotFoundException)20 LuminanceSource (com.google.zxing.LuminanceSource)19 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)19 DecodeHintType (com.google.zxing.DecodeHintType)15 Handler (android.os.Handler)14 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)13 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)11 Hashtable (java.util.Hashtable)11 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)10 Test (org.junit.Test)10 BitArray (com.google.zxing.common.BitArray)8 Bitmap (android.graphics.Bitmap)7