打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Google开源项目ZXing(二维条码编解码)简单使用(Java版)

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上面提供的一个关于条码编解码的开源项目。

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

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

你需要使用里面的Core和J2SE文件夹。然后导入到你在Eclipse中创建的工程,目录形式如下:



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

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

  1. package com.google.zxing.client.j2se;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.Hashtable;  
  6.   
  7. import com.google.zxing.BarcodeFormat;  
  8. import com.google.zxing.EncodeHintType;  
  9. import com.google.zxing.MultiFormatWriter;  
  10. import com.google.zxing.WriterException;  
  11. import com.google.zxing.common.BitMatrix;  
  12.   
  13. public class Encoder {  
  14.   
  15.     public static void main(String[] args) {  
  16.   
  17.         String contents = "今天,我们来简单聊聊google开源项目——ZXing(二维条码编解码)";  
  18.   
  19.         Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
  20.   
  21.         hints.put(EncodeHintType.CHARACTER_SET, "GBK");  
  22.   
  23.         BitMatrix matrix = null;  
  24.   
  25.         try {  
  26.   
  27.             matrix = new MultiFormatWriter().encode(contents,  
  28.                     BarcodeFormat.QR_CODE, 300, 300, hints);  
  29.   
  30.         } catch (WriterException e) {  
  31.   
  32.             e.printStackTrace();  
  33.   
  34.         }  
  35.   
  36.         File file = new File("D://qrcodeImage.png");  
  37.   
  38.         try {  
  39.   
  40.             MatrixToImageWriter.writeToFile(matrix, "png", file);  
  41.   
  42.         } catch (IOException e) {  
  43.   
  44.             e.printStackTrace();  
  45.   
  46.         }  
  47.   
  48.     }  
  49. }  

  1. package com.google.zxing.client.j2se;  
  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.common.HybridBinarizer;  
  17.   
  18. public class Decoder {  
  19.       
  20.     public static void main(String[] args) {  
  21.   
  22.         File file = new File("D://qrcodeImage.png");  
  23.   
  24.         BufferedImage bufferedImage = null;  
  25.   
  26.         try {  
  27.   
  28.             bufferedImage = ImageIO.read(file);  
  29.   
  30.         } catch (IOException e) {  
  31.   
  32.             e.printStackTrace();  
  33.   
  34.         }  
  35.   
  36.         LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);  
  37.   
  38.         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
  39.   
  40.         Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();  
  41.   
  42.         hints.put(DecodeHintType.CHARACTER_SET, "GBK");  
  43.   
  44.         Result result = null;  
  45.   
  46.         try {  
  47.   
  48.             result = new MultiFormatReader().decode(bitmap, hints);  
  49.   
  50.         } catch (NotFoundException e) {  
  51.   
  52.             e.printStackTrace();  
  53.   
  54.         }  
  55.   
  56.         System.out.println(result.toString());  
  57.   
  58.     }  
  59.   
  60. }  

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java Zxing生成二维码解决中文编码问题
一步一步教你用java生成二维码
使用zxing生成嵌图片的二维码【java】
java 二维码+条形码解码+生成 zxing
java生成二维码的三个工具
java实现二维码的生成和解析:QRCode、zxing 两种方式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服