0%

java二维码工具google.zxing的使用

引入依赖

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>
</dependency>

编写工具类

public class QRCodeUtil {
    // 设置二维码长宽
    private static int width = 400;
    private static int height = 400;
    private static String format = "png";

    // 将文本信息封装到二维码中,并返回图片的Base64编码后的字符串
    public static String encodeQRCode(String text) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String result = null;
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            // 设置字符集编码
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            // ErrorCorrectionLevel:误差校正等级,L = ~7% correction、M = ~15% correction、Q = ~25% correction、H = ~30% correction
            // 不设置时,默认为 L 等级,等级不一样,生成的图案不同,但扫描的结果是一样的
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            // 设置二维码边距,单位像素,值越小,二维码距离四周越近
            hints.put(EncodeHintType.MARGIN, 1);
            // 生成二维码矩阵
            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
            // 写入文件
            MatrixToImageWriter.writeToStream(bitMatrix, format, outputStream);
            bitMatrix.clear();
            result = "data:image/png;base64," + Base64.getEncoder().encodeToString(outputStream.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
            }
        }
        return result;

    }

    // 通过base64编码后的图片,解析出二维码中的文本信息
    public static String decodeQRCode(String base64) {
        Result result = null;
        BufferedImage image = null;
        ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
        try {
            // 读取图片
            image = ImageIO.read(inputStream);
            // 多步解析
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            // 设置字符集编码
            Map<DecodeHintType, String> hints = new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            // 对图像进行解码
            result = new MultiFormatReader().decode(binaryBitmap, hints);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            image.getGraphics().dispose();
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
        return result.getText();
    }
}

测试

    // 测试准确率还是非常高的
    public static void main(String[] args) {
        // 生成二维码
        System.out.println(encodeQRCode("我叫XX勇~~~"));
        // 解码二维码
        // 注意:需要去掉data:image/png;base64,
        System.out.println(decodeQRCode("xxxxxxxxxx.....xxxxxxxx"));
    }