ZXing二维码编解码的使用

Google开源项目ZXing的网方网址: http://code.google.com/p/zxing/

  ZXing (pronounced "zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. Our focus is on using the built-in camera on mobile phones to scan and decode barcodes on the device, without communicating with a server. However the project can be used to encode and decode barcodes on desktops and servers as well.

  ZXing项目是google code上面提供的一个关于条码编解码的开源项目。

  要对本项目进行二次开发,首先你需要在源码下载列表中下载ZXing-x.x.x.zip源代码

  解压文件,你会看到里面有很多文件夹,包括J2SE的,Android的,J2ME,C#等等。我们以J2SE为例。

  J2SE包中,提供了编码和解码的.Java文件,但是由于写的过于复杂,还没弄清楚这个开源项目源码之前,还是不推荐使用。

  自己在j2se包中创建两个Java文件Encoder和Decoder。代码如下:

Encoder.java

Java代码   收藏代码
  1. package com.bijian.study;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7. import java.util.Hashtable;  
  8.   
  9. import com.google.zxing.BarcodeFormat;  
  10. import com.google.zxing.EncodeHintType;  
  11. import com.google.zxing.MultiFormatWriter;  
  12. import com.google.zxing.WriterException;  
  13. import com.google.zxing.client.j2se.MatrixToImageWriter;  
  14. import com.google.zxing.common.BitMatrix;  
  15.   
  16. public class Encoder {  
  17.   
  18.     public static void main(String[] args) throws IOException {  
  19.   
  20.         String contents = "今天,我们来简单聊聊google开源项目——ZXing(二维条码编解码)";  
  21.         Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
  22.         hints.put(EncodeHintType.CHARACTER_SET, "GBK");  
  23.         BitMatrix matrix = null;  
  24.         try {  
  25.             matrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, 300300, hints);  
  26.         } catch (WriterException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.         File file = new File("D://qrcodeImage.png");  
  30.         OutputStream out = new FileOutputStream(file);  
  31.         MatrixToImageWriter.writeToStream(matrix, "png", out);  
  32.     }  
  33. }  

Decoder.java

Java代码   收藏代码
  1. package com.bijian.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.util.Hashtable;  
  7.   
  8. import javax.imageio.ImageIO;  
  9.   
  10. import com.google.zxing.BinaryBitmap;  
  11. import com.google.zxing.DecodeHintType;  
  12. import com.google.zxing.LuminanceSource;  
  13. import com.google.zxing.MultiFormatReader;  
  14. import com.google.zxing.NotFoundException;  
  15. import com.google.zxing.Result;  
  16. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;  
  17. import com.google.zxing.common.HybridBinarizer;  
  18.   
  19. public class Decoder {  
  20.   
  21.     public static void main(String[] args) {  
  22.   
  23.         File file = new File("D://qrcodeImage.png");  
  24.         BufferedImage bufferedImage = null;  
  25.         try {  
  26.             bufferedImage = ImageIO.read(file);  
  27.         } catch (IOException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.         LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);  
  31.         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
  32.         Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();  
  33.         hints.put(DecodeHintType.CHARACTER_SET, "GBK");  
  34.         Result result = null;  
  35.         try {  
  36.             result = new MultiFormatReader().decode(bitmap, hints);  
  37.         } catch (NotFoundException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.         System.out.println(result.toString());  
  41.     }  
  42. }  

        之后你就可以开始进行条码编辑和识别了,这里是支持中文的。

        执行Encoder中的main方法,即在D盘下生成了qrcodeImage.png文件,打开文件,如下所示:


  可以用微信的“扫一扫”扫此二维码,获取内容“今天,我们来简单聊聊google开源项目——ZXing(二维条码编解码)”。

  接下来,再执行Decoder的main方法,运行输出内容“今天,我们来简单聊聊google开源项目——ZXing(二维条码编解码)”。

猜你喜欢

转载自blog.csdn.net/arno_dzl/article/details/76683483
今日推荐