Java利用zxing生成/解析二维码

QRCode:全称Quick Response Code ,是二维码的一种

参考博客 http://www.cnblogs.com/jtmjx/archive/2012/06/18/2545209.html
慕课网视频 生成二维码 http://www.imooc.com/video/10315
慕课网视频 解析二维码 http://www.imooc.com/video/10316

第1步.引入jar包
源码路径 https://github.com/zxing/zxing/releases
没有现成的jar包,可以根据源码,自己建java project项目,把源码拷贝到项目中,然后导出jar包
导出jar包的时候,可能有错误,忽略

2. 编写生成二维码的实现代码

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class CreateQRCode {

    public static void main(String[] args) {

        /*定义二维码参数*/ 
        String content = "https://github.com/zxing/zxing/releases";// 二维码内容
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 纠错等级
        hints.put(EncodeHintType.MARGIN, 2);// 边距,默认是5

        /*生成二维码*/ 
        try {

            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();          
            /*二维码类型QR_CODE,是图片宽度和高度是300*300*/ 
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);

            Path path = new File("C:/Users/Administrator/Desktop/img.png").toPath();// 二维码存放的路径和名称
            MatrixToImageWriter.writeToPath(bitMatrix, "png", path);//图片是png类型

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

纠错能力越高,存储的数据越少,对二维码的清晰度要求越低。
L级:可纠错7%的数据码字 M:15% Q:25% H:30%

除了zxing, javascript,jquery.qrcode.js也可以生成二维码

3. 编写解析二维码的实现代码

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class ReadQRCode {

    public static void main(String[] args) throws IOException, NotFoundException {

        MultiFormatReader formatReader = new MultiFormatReader();

        File file = new File("C:/Users/Administrator/Desktop/img.png");     
        BufferedImage image = ImageIO.read(file);

        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        Result result = formatReader.decode(binaryBitmap,hints);

        System.out.println("解析结果:"+result.toString());
        System.out.println("二维码格式:"+result.getBarcodeFormat());
        System.out.println("二维码文本内容:"+result.getText());

    }

}
posted on 2017-06-07 15:30  dreamstar  阅读(293)  评论(0编辑  收藏  举报