JAVA使用com.google.zxing生成二维码和解析二维码案例

本文介绍了如何在JAVA中利用com.google.zxing库生成和解析二维码。首先,展示了所需的XML依赖,然后详细讲解了生成二维码的步骤,包括设置纠错级别、编码信息、绘制并保存图像。接着,提供了解析二维码的代码,通过读取图像并进行解码。示例代码可生成和解析包含特定信息的二维码图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA使用com.google.zxing生成二维码和解析二维码案例

1. 需要使用到的xml配置
	<!-- 二维码工具 -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.3</version>
        </dependency>
2. 生成二维码
public class test {
    /**
     * 生成包含字符串信息的二维码图片
     * @param outputStream 文件输出流路径
     * @param content 二维码携带信息
     */
    public static boolean createQrCode(OutputStream outputStream, String content) throws WriterException, IOException{
        //设置二维码纠错级别MAP
        Hashtable hintMap = new Hashtable();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  // 矫错级别
        //设置UTF-8, 防止中文乱码
        hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //设置二维码四周白色区域的大小
        //hintMap.put(EncodeHintType.MARGIN,0);

        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //创建比特矩阵(位矩阵)的QR码编码的字符串
        BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 900, 900, hintMap);
        // 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
        int matrixWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, matrixWidth, matrixWidth);
        // 使用比特矩阵画并保存图像
        graphics.setColor(Color.BLACK);
        for (int i = 0; i < matrixWidth; i++){
            for (int j = 0; j < matrixWidth; j++){
                if (byteMatrix.get(i, j)){
                    graphics.fillRect(i-100, j-100, 1, 1);
                }
            }
        }
        return ImageIO.write(image, "JPEG", outputStream);
    }

	// 调用生成二维码
    public static void main(String[] args) throws IOException, WriterException {
        OutputStream out = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\123456.png"));
        String content = "HELLO CDSDN";
        createQrCode(out,content);
    }
}
3. 解析二维码
public class test2{
    public static void getResult(String path) {
        try {
            BufferedImage image3 = ImageIO.read(new File(path));
            LuminanceSource source = new BufferedImageLuminanceSource(image3);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            QRCodeMultiReader reader = new QRCodeMultiReader();
            //可以一次性解析一张图片的多个二维码,并返回结果数组
            Result[] results = reader.decodeMultiple(bitmap);
            System.out.println();
        } catch (IOException | NotFoundException e) {
            e.printStackTrace();
        }
    }
    
	//调用解析二维码
    public static void main(String[] args) {
        String path = "D:\\Users\\27532\\Desktop\\ee.png";
        getResult(path);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值