天天看點

基于Google的zxing生成和解析QR Code基于Google的zxing生成和解析QR Code

基于Google的zxing生成和解析QR Code

介紹

二維碼的技術在當今應用相當的的廣泛,其中以QR Code應用最為廣泛和流行。 

在二維碼發展之前還有一維碼即條形碼,相比條形碼二維碼存儲容量更大,并且容錯性也更好。
           

優點

1、高密度編碼,資訊容量大(大概能存儲幾百上千個漢字)
2、編碼範圍廣
3、容錯能力強,具有糾錯功能
4、譯碼可靠性高
5、可引入加密措施
6、成本低,易制作,持久耐用
           

缺點

1、安全問題,可能成為一些病毒入侵新管道
備注:主要是安全問題
           

基于zxing生成和解析二維碼

maven pom依賴

<!-- 二維碼zxing元件 -->

    <!-- 核心元件 -->
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.2.1</version>
    </dependency>

    <!-- javase擴充元件 -->
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        <version>3.2.1</version>
    </dependency>

    備注:一定不能忘記javase擴充元件
           

封裝QrCodeUtils工具類

/**
 * <h3>QRCode工具類</h3>
 * 
 *      1、生成二維碼<br>
 *      2、解析二維碼<br>
 * 
 * @author xuyi3
 * @2016年8月30日 @上午8:49:21
 * @QrUtils
 * @功能說明:<br>
 * @春風十裡不如你
 * @備注
 */
public class QrCodeUtils
{

    /**
     * <h3>生成二維碼</h3>
     * 
     * @param width             寬度(像素)
     * @param height            高度(像素)
     * @param content           文本内容
     * @param charset           文本字元編碼
     * @param path              存放二維碼路徑
     * @param imageFormat       圖檔格式(jpg/png...)
     * @throws Exception
     */
    public static void createQRCode(int width, int height, String content, String charset, String path,
            String imageFormat) throws Exception
    {
        // 設定二維碼一些配置資訊
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, charset);//設定二維碼内容編碼格式
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//設定二維碼容錯級别(L、M、Q、H)

        //将内容編碼為指定的二維矩陣圖像
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        //使用MatrixToImageWriter類的靜态方法将二維矩陣寫入到輸出流或指定路徑path
        MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File(path)));
        //MatrixToImageWriter.writeToPath(bitMatrix, imageFormat, new File("").toPath());
    }

    /**
     * <h3>解析二維碼内容</h3>
     * 
     * @param imagePath         待解析的二維碼圖檔存放路徑
     * @return
     * @throws Exception
     */
    public static String decodeQRCode(String imagePath) throws Exception
    {
        // 其實下面這四步順着推是很難推出來的,反着推倒比較簡答,不過完全沒必要記住,知道大概就行了
        BufferedImage bufferedImage = ImageIO.read(new File(imagePath));
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);

        //使用MultiFormatReader将二維碼圖檔解析為内容對象
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

        Result result = multiFormatReader.decode(binaryBitmap, hints);
        return result.getText();
    }

}
           

main code

/**
 * 
 * @author xuyi3
 * @2016年8月30日 @上午9:51:44
 * @Main
 * @功能說明:<br>
 * @春風十裡不如你
 * @備注
 */
public class Main
{
    public static void main(String[] args) throws Exception
    {
        String content = "http://www.baidu.com";
        String path = "e:/test.png";
        //建立二維碼
        QrCodeUtils.createQRCode(300, 300, content, "utf-8", path, "png");

        //解析二維碼
        String string = QrCodeUtils.decodeQRCode(path);
        System.out.println(string);

    }
}
           

總結

借助于zxing生成和解析二維碼還是比較簡單的,有時間可以看看二維碼的發展和其實作原理。
本質上就是一種編碼和解碼,隻不過其有自己的規範。
           

參考

1、https://github.com/zxing/zxing