Search in sources :

Example 6 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project pancm_project by xuwujing.

the class QrCodeCreateUtil method readQrCode.

/**
 * 读二维码并输出携带的信息
 *
 * @param inputStream the input stream
 * @throws IOException the io exception
 */
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) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage)

Example 7 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project pancm_project by xuwujing.

the class QrCodeCreateUtil method decodeQRInfoFromImage.

/**
 * 解析图片中的二维码文本信息
 *
 * @param imagePath 含二维码图片的路径
 * @param cachePath 缓存目录路径
 * @return 解析出的二维码文本信息
 */
public static String decodeQRInfoFromImage(String imagePath, String cachePath) {
    String qrInfo = null;
    try {
        // 判断图片路径是否为空
        if (imagePath == null || "".equals(imagePath.trim())) {
            System.err.println("Parameter \'imagePath\' cannot be empty");
            qrInfo = null;
            return qrInfo;
        }
        // 判断图片文件是否存在
        File imageFile = new File(imagePath);
        if (!imageFile.exists()) {
            System.err.println("The image file is not exits");
            qrInfo = null;
            return qrInfo;
        }
        // 判断是否为图片
        try {
            Image image = ImageIO.read(imageFile);
            if (image == null) {
                System.err.println("The image file is not real picture");
                qrInfo = null;
                return qrInfo;
            }
        } catch (IOException ex) {
            System.err.println("The image file is not real picture");
            qrInfo = null;
            return qrInfo;
        }
        // 判断缓存目录是否为空
        if (cachePath == null || "".equals(cachePath.trim())) {
            System.err.println("Parameter \'cachePath\' cannot be empty");
            qrInfo = null;
            return qrInfo;
        }
        // 判断缓存目录是否存在
        File cacheFile = new File(cachePath);
        if (!cacheFile.exists() || !cacheFile.isDirectory()) {
            System.err.println("cachePath is not exits or is not directory");
            qrInfo = null;
            return qrInfo;
        }
        ImageIO.setCacheDirectory(new File(cachePath));
        BufferedImage image = ImageIO.read(new File(imagePath));
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
        Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result result = new MultiFormatReader().decode(binaryBitmap, hints);
        qrInfo = result.getText();
        return qrInfo;
    } catch (Exception e) {
        qrInfo = null;
        return qrInfo;
    }
}
Also used : HashMap(java.util.HashMap) BufferedImage(java.awt.image.BufferedImage) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) JSONObject(com.alibaba.fastjson.JSONObject) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 8 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project pancm_project by xuwujing.

the class QrCodeMaxTest method decode.

// qrcode 解码
static void decode(String file) {
    try {
        Result result = null;
        BufferedImage image = null;
        image = ImageIO.read(new File(file));
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        result = new MultiFormatReader().decode(bitmap, hints);
        String rtn = result.getText();
        System.out.println(rtn);
        System.out.println(rtn.length());
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}
Also used : Hashtable(java.util.Hashtable) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) File(java.io.File)

Example 9 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project quick-media by liuyueyi.

the class QrCodeDeWrapper method decode.

public static String decode(BufferedImage image) throws FormatException, ChecksumException, NotFoundException {
    if (image == null) {
        throw new IllegalStateException("can not load qrCode!");
    }
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    // QRCodeReader qrCodeReader = new QRCodeReader();
    // Result result = qrCodeReader.decode(bitmap);
    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);
    return result.getText();
}
Also used : BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) HybridBinarizer(com.google.zxing.common.HybridBinarizer) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project tephra by heisedebaise.

the class QrCodeImpl method read.

@Override
public String read(InputStream inputStream) {
    try {
        String string = reader.decode(new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(inputStream))))).getText();
        inputStream.close();
        return string;
    } catch (Throwable e) {
        logger.warn(e, "读取二维码图片内容时发生异常!");
        return null;
    }
}
Also used : BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Aggregations

BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)19 HybridBinarizer (com.google.zxing.common.HybridBinarizer)19 BinaryBitmap (com.google.zxing.BinaryBitmap)13 BufferedImage (java.awt.image.BufferedImage)12 Result (com.google.zxing.Result)10 MultiFormatReader (com.google.zxing.MultiFormatReader)9 HashMap (java.util.HashMap)7 LuminanceSource (com.google.zxing.LuminanceSource)6 DecodeHintType (com.google.zxing.DecodeHintType)5 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)4 File (java.io.File)4 IOException (java.io.IOException)4 Binarizer (com.google.zxing.Binarizer)3 ReaderException (com.google.zxing.ReaderException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Hashtable (java.util.Hashtable)3 JSONObject (com.alibaba.fastjson.JSONObject)2 NotFoundException (com.google.zxing.NotFoundException)2 IndexController (alfio.controller.IndexController)1 AdditionalServiceApiController (alfio.controller.api.admin.AdditionalServiceApiController)1