zoukankan      html  css  js  c++  java
  • 条形码/二维码之开源利器ZXing图文介绍(转)

    继前面介绍的一个日本开源软件(该软件只能实现QRCode)原文: Java实现二维码QRCode的编码和解码(http://sjsky.iteye.com/blog/1136934 ),今发现又一优秀的开源利器-- ZXing,相比而言它更加灵活方便,可以实现多种编码格式。

    全文目录:

    • 基本介绍
    • 二维码(比如:QRCode)的编码和解码演示
    • 条形码(比如:EAN-13)的编码和解码演示 

    【一】、 基本介绍 :

     1-1. ZXing是一个开源Java类库用于解析多种格式的条形码和二维码.

    官网:http://code.google.com/p/zxing/

    截止目前为止最新版本为1.7,提供以下编码格式的支持:

    • UPC-A and UPC-E
    • EAN-8 and EAN-13
    • Code 39
    • Code 93
    • Code 128
    • QR Code
    • ITF
    • Codabar
    • RSS-14 (all variants)
    • Data Matrix
    • PDF 417 ('alpha' quality)
    • Aztec ('alpha' quality)

    同时官网提供了 Android、cpp、C#、iPhone、j2me、j2se、jruby、objc、rim、symbian等多种应用的类库,具体详情可以参考下载的源码包中。

    1-2. 本文和之前的那篇文章一样,主要是在PC上实现条形码(EAN-13)和二维码(QRCode) 的编码和解码的示例,以供大家参考,用到了源码中core和javase下面的相关源代码,附件提供自己编译之后的lib包:

    有关各种手机系统的应用,有兴趣的朋友可以下载官方源码包,包下有具体详细的应用介绍。

    【二】、 二维码(QRCode)的编码和解码演示:

     2-1. 编码示例:

    Java代码  收藏代码
    1. package michael.zxing;  
    2.   
    3. import java.io.File;  
    4. import java.util.Hashtable;  
    5.   
    6. import com.google.zxing.BarcodeFormat;  
    7. import com.google.zxing.EncodeHintType;  
    8. import com.google.zxing.MultiFormatWriter;  
    9. import com.google.zxing.client.j2se.MatrixToImageWriter;  
    10. import com.google.zxing.common.BitMatrix;  
    11. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;  
    12.   
    13. /** 
    14.  * @blog http://sjsky.iteye.com 
    15.  * @author Michael 
    16.  */  
    17. public class ZxingEncoderHandler {  
    18.   
    19.     /** 
    20.      * 编码 
    21.      * @param contents 
    22.      * @param width 
    23.      * @param height 
    24.      * @param imgPath 
    25.      */  
    26.     public void encode(String contents, int width, int height, String imgPath) {  
    27.         Hashtable<Object, Object> hints = new Hashtable<Object, Object>();  
    28.         // 指定纠错等级  
    29.         hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  
    30.         // 指定编码格式  
    31.         hints.put(EncodeHintType.CHARACTER_SET, "GBK");  
    32.         try {  
    33.             BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,  
    34.                     BarcodeFormat.QR_CODE, width, height, hints);  
    35.   
    36.             MatrixToImageWriter  
    37.                     .writeToFile(bitMatrix, "png", new File(imgPath));  
    38.   
    39.         } catch (Exception e) {  
    40.             e.printStackTrace();  
    41.         }  
    42.     }  
    43.   
    44.     /** 
    45.      * @param args 
    46.      */  
    47.     public static void main(String[] args) {  
    48.         String imgPath = "d:/test/twocode/michael_zxing.png";  
    49.         String contents = "Hello Michael(大大),welcome to Zxing!"  
    50.                 + " Michael’s blog [ http://sjsky.iteye.com ]"  
    51.                 + " EMail [ sjsky007@gmail.com ]" + " Twitter [ @suncto ]";  
    52.         int width = 300, height = 300;  
    53.         ZxingEncoderHandler handler = new ZxingEncoderHandler();  
    54.         handler.encode(contents, width, height, imgPath);  
    55.   
    56.         System.out.println("Michael ,you have finished zxing encode.");  
    57.     }  
    58. }  

     运行后生成的二维码图片如下:

                                      和前篇介绍一样,用手机的二维码扫描软件(本人用的:android 快拍二维码 )来测试下,识别成功的截图如下:

    2-2. 解码示例:

    Java代码  收藏代码
    1. package michael.zxing;  
    2.   
    3. import java.awt.image.BufferedImage;  
    4. import java.io.File;  
    5. import java.util.Hashtable;  
    6.   
    7. import javax.imageio.ImageIO;  
    8.   
    9. import com.google.zxing.BinaryBitmap;  
    10. import com.google.zxing.DecodeHintType;  
    11. import com.google.zxing.LuminanceSource;  
    12. import com.google.zxing.MultiFormatReader;  
    13. import com.google.zxing.Result;  
    14. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;  
    15. import com.google.zxing.common.HybridBinarizer;  
    16.   
    17. /** 
    18.  * @blog http://sjsky.iteye.com 
    19.  * @author Michael 
    20.  */  
    21. public class ZxingDecoderHandler {  
    22.   
    23.     /** 
    24.      * @param imgPath 
    25.      * @return String 
    26.      */  
    27.     public String decode(String imgPath) {  
    28.         BufferedImage image = null;  
    29.         Result result = null;  
    30.         try {  
    31.             image = ImageIO.read(new File(imgPath));  
    32.             if (image == null) {  
    33.                 System.out.println("the decode image may be not exit.");  
    34.             }  
    35.             LuminanceSource source = new BufferedImageLuminanceSource(image);  
    36.             BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
    37.   
    38.             Hashtable<Object, Object> hints = new Hashtable<Object, Object>();  
    39.             hints.put(DecodeHintType.CHARACTER_SET, "GBK");  
    40.   
    41.             result = new MultiFormatReader().decode(bitmap, hints);  
    42.             return result.getText();  
    43.         } catch (Exception e) {  
    44.             e.printStackTrace();  
    45.         }  
    46.         return null;  
    47.     }  
    48.   
    49.     /** 
    50.      * @param args 
    51.      */  
    52.     public static void main(String[] args) {  
    53.         String imgPath = "d:/test/twocode/michael_zxing.png";  
    54.         ZxingDecoderHandler handler = new ZxingDecoderHandler();  
    55.         String decodeContent = handler.decode(imgPath);  
    56.         System.out.println("解码内容如下:");  
    57.         System.out.println(decodeContent);  
    58.         System.out.println("Michael ,you have finished zxing decode.");  
    59.   
    60.     }  
    61. }  
     

    运行结果如下:

    解码内容如下: Hello Michael(大大),welcome to Zxing! Michael’s blog [ http://sjsky.iteye.com ] EMail [ sjsky007@gmail.com ] Twitter [ @suncto ]  Michael ,you have finished zxing decode.

      从测试结果可见:解码出的内容和之前编码的内容是一致

    【三】、 条形码(EAN-13)的编码和解码演示:

     3-1. 编码示例:

    Java代码  收藏代码
    1. package michael.zxing;  
    2.   
    3. import java.io.File;  
    4.   
    5. import com.google.zxing.BarcodeFormat;  
    6. import com.google.zxing.MultiFormatWriter;  
    7. import com.google.zxing.client.j2se.MatrixToImageWriter;  
    8. import com.google.zxing.common.BitMatrix;  
    9.   
    10. /** 
    11.  * @blog http://sjsky.iteye.com 
    12.  * @author Michael 
    13.  */  
    14. public class ZxingEAN13EncoderHandler {  
    15.   
    16.     /** 
    17.      * 编码 
    18.      * @param contents 
    19.      * @param width 
    20.      * @param height 
    21.      * @param imgPath 
    22.      */  
    23.     public void encode(String contents, int width, int height, String imgPath) {  
    24.         int codeWidth = 3 + // start guard  
    25.                 (7 * 6) + // left bars  
    26.                 5 + // middle guard  
    27.                 (7 * 6) + // right bars  
    28.                 3; // end guard  
    29.         codeWidth = Math.max(codeWidth, width);  
    30.         try {  
    31.             BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,  
    32.                     BarcodeFormat.EAN_13, codeWidth, height, null);  
    33.   
    34.             MatrixToImageWriter  
    35.                     .writeToFile(bitMatrix, "png", new File(imgPath));  
    36.   
    37.         } catch (Exception e) {  
    38.             e.printStackTrace();  
    39.         }  
    40.     }  
    41.   
    42.     /** 
    43.      * @param args 
    44.      */  
    45.     public static void main(String[] args) {  
    46.         String imgPath = "d:/test/twocode/zxing_EAN13.png";  
    47.         // 益达无糖口香糖的条形码  
    48.         String contents = "6923450657713";  
    49.   
    50.         int width = 105, height = 50;  
    51.         ZxingEAN13EncoderHandler handler = new ZxingEAN13EncoderHandler();  
    52.         handler.encode(contents, width, height, imgPath);  
    53.   
    54.         System.out.println("Michael ,you have finished zxing EAN13 encode.");  
    55.     }  
    56. }  

    6 923450 657713 对应的是益达无糖口香糖:

    运行后生成的条形码图片如下:

                                               

    用手机的扫描软件,识别成功的截图如下:

    3-2. 解码示例:

    Java代码  收藏代码
    1. package michael.zxing;  
    2.   
    3. import java.awt.image.BufferedImage;  
    4. import java.io.File;  
    5.   
    6. import javax.imageio.ImageIO;  
    7.   
    8. import com.google.zxing.BinaryBitmap;  
    9. import com.google.zxing.LuminanceSource;  
    10. import com.google.zxing.MultiFormatReader;  
    11. import com.google.zxing.Result;  
    12. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;  
    13. import com.google.zxing.common.HybridBinarizer;  
    14.   
    15. /** 
    16.  * @blog http://sjsky.iteye.com 
    17.  * @author Michael 
    18.  */  
    19. public class ZxingEAN13DecoderHandler {  
    20.   
    21.     /** 
    22.      * @param imgPath 
    23.      * @return String 
    24.      */  
    25.     public String decode(String imgPath) {  
    26.         BufferedImage image = null;  
    27.         Result result = null;  
    28.         try {  
    29.             image = ImageIO.read(new File(imgPath));  
    30.             if (image == null) {  
    31.                 System.out.println("the decode image may be not exit.");  
    32.             }  
    33.             LuminanceSource source = new BufferedImageLuminanceSource(image);  
    34.             BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
    35.   
    36.             result = new MultiFormatReader().decode(bitmap, null);  
    37.             return result.getText();  
    38.         } catch (Exception e) {  
    39.             e.printStackTrace();  
    40.         }  
    41.         return null;  
    42.     }  
    43.   
    44.     /** 
    45.      * @param args 
    46.      */  
    47.     public static void main(String[] args) {  
    48.         String imgPath = "d:/test/twocode/zxing_EAN13.png";  
    49.         ZxingEAN13DecoderHandler handler = new ZxingEAN13DecoderHandler();  
    50.         String decodeContent = handler.decode(imgPath);  
    51.         System.out.println("解码内容如下:");  
    52.         System.out.println(decodeContent);  
    53.         System.out.println("Michael ,you have finished zxing EAN-13 decode.");  
    54.   
    55.     }  
    56. }  

     运行结果如下:

    写道
    解码内容如下: 6923450657713  Michael ,you have finished zxing decode.
     

    从测试结果可见:解码出的内容和之前编码的内容是一致。

    转载请注明来自:Michael's blog @ http://sjsky.iteye.com 
    ----------------------------- 分 ------------------------------ 隔 ------------------------------ 线 ------------------------------

     http://sjsky.iteye.com/blog/1142177

     

  • 相关阅读:
    手写PE文件(不借助编译器,用十六进制数进行编写)
    浅谈代码段加密原理(防止静态分析)
    PE文件动态加载执行过程
    静态数据连接库(.lib)和动态连接库(.dll)
    关于普通断点/内存断点/硬件断点
    x64dbg零基础使用教程
    HOOK相关原理与例子
    socket,实现服务器和客户端对话
    解析PE文件
    python正则表达式---基于re模块
  • 原文地址:https://www.cnblogs.com/softidea/p/4606209.html
Copyright © 2011-2022 走看看