ITeye博客

条形码/二维码之开源利器ZXing图文介绍(转)

langyan · 2012-05-03 11:18 · java · 编程语言 · Markdown

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

 

全文目录:

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

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

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

 

2-2. 解码示例:

Java代码  收藏代码

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

 

 

运行结果如下:

 

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

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

 

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

                                          

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

3-2. 解码示例:

Java代码  收藏代码

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

 运行结果如下:

写道

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

 

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

评论(2)

  1. #2 楼 · gentlesong88 · 2013-12-26 14:50

    赞一个 基础的操作都有了

  2. #1 楼 · 苇间风语 · 2012-12-05 17:22

    :idea:

评论功能维护中,当前仅展示历史评论。