Search in sources :

Example 16 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project protools by SeanDragon.

the class MatrixToImageWriterEx method decode.

/**
 * 解码
 *
 * @param filePath
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String decode(String filePath) throws IOException, NotFoundException {
    BufferedImage image = ImageIO.read(new File(filePath));
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
    hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
    Result result = new MultiFormatReader().decode(bitmap, hints);
    return result.getText();
}
Also used : BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) Hashtable(java.util.Hashtable) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) File(java.io.File) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage)

Example 17 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project wechat by dllwh.

the class QrcodeUtilHelper method decode.

/**
 * @方法描述: 解析二维码
 * @创建者: 独泪了无痕
 * @创建时间: 2016年2月26日 下午6:03:54
 * @param imgPath
 *            二维码路径
 * @return 返回解析后的内容
 */
public static String decode(String imgPath) {
    if (StringUtils.isBlank(imgPath)) {
        System.out.println("尚未找到想要解析的图片");
        return "";
    }
    BufferedImage image;
    Result result;
    try {
        image = ImageIO.read(new File(imgPath));
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        // 解码设置编码方式为:GBK
        hints.put(DecodeHintType.CHARACTER_SET, "GBK");
        result = new MultiFormatReader().decode(bitmap, hints);
        // 对图像进行解码
        String resultStr = result.getText();
        return resultStr;
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (NotFoundException nfe) {
        nfe.printStackTrace();
    }
    return "";
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) 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) File(java.io.File)

Example 18 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 NotFoundException {
    if (image == null) {
        throw new IllegalStateException("can not load qrCode!");
    }
    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);
    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 19 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 20 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)

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