基於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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章