Search in sources :

Example 11 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project ngtesting-platform by aaronchen2k.

the class QrcodeServiceImpl method decode.

@Override
public JSONObject decode(String filePath) {
    BufferedImage image;
    try {
        image = ImageIO.read(new File(filePath));
        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);
        System.out.println(result.getText());
        JSONObject content = JSONObject.parseObject(result.getText());
        System.out.println("encode: " + result.getBarcodeFormat());
        return content;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
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) JSONObject(com.alibaba.fastjson.JSONObject) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) JSONObject(com.alibaba.fastjson.JSONObject) BinaryBitmap(com.google.zxing.BinaryBitmap) File(java.io.File) Binarizer(com.google.zxing.Binarizer) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 12 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 13 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project springboot-demo by small-rose.

the class QRCodeUtil method decode.

/**
 * 解码,将二维码里的信息解码出来
 * @param path
 * @return
 * @throws Exception
 */
public static String decode(String path) throws Exception {
    File file = new File(path);
    BufferedImage image;
    image = ImageIO.read(file);
    if (image == null) {
        return null;
    }
    BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Result result;
    Hashtable hints = new Hashtable();
    hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
    result = new MultiFormatReader().decode(bitmap, hints);
    String resultStr = result.getText();
    return resultStr;
}
Also used : 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 14 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project DevKits by qmjy.

the class CodecFrame method decodeBufferedImage.

@SuppressWarnings({ "rawtypes", "unchecked" })
private Optional<Result> decodeBufferedImage(BufferedImage bufferedImage) {
    try {
        /**
         * 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 用于读取二进制位图数据
         */
        return Optional.of(new MultiFormatReader().decode(bitmap, hints));
    } catch (NotFoundException e) {
        LOGGER.error(" Any errors which occurred...");
    }
    return Optional.empty();
}
Also used : BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 15 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project mica by lets-mica.

the class QrCode method readRawBytes.

/**
 * 从指定的 QRCode 图像对象中解析出其内容。
 *
 * @param qrCodeImage QRCode 图像对象
 * @return QRCode 中的内容
 */
public static byte[] readRawBytes(BufferedImage qrCodeImage) {
    LuminanceSource source = new BufferedImageLuminanceSource(qrCodeImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        Result result = new QRCodeReader().decode(bitmap);
        return result.getRawBytes();
    } catch (NotFoundException | ChecksumException | FormatException e) {
        throw Exceptions.unchecked(e);
    } finally {
        qrCodeImage.getGraphics().dispose();
    }
}
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)

Aggregations

BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)44 HybridBinarizer (com.google.zxing.common.HybridBinarizer)44 BufferedImage (java.awt.image.BufferedImage)31 BinaryBitmap (com.google.zxing.BinaryBitmap)23 Result (com.google.zxing.Result)18 MultiFormatReader (com.google.zxing.MultiFormatReader)16 HashMap (java.util.HashMap)15 DecodeHintType (com.google.zxing.DecodeHintType)10 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)9 File (java.io.File)9 LuminanceSource (com.google.zxing.LuminanceSource)8 IOException (java.io.IOException)8 Hashtable (java.util.Hashtable)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 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