- 浏览: 28156 次
- 性别:
- 来自: 厦门
文章分类
最新评论
-
gentlesong88:
赞一个 基础的操作都有了
条形码/二维码之开源利器ZXing图文介绍(转) -
苇间风语:
...
条形码/二维码之开源利器ZXing图文介绍(转) -
songfantasy:
学习了,今天刚好遇到这样的困惑。3Q
将变量置入循环
摘选: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. 编码示例:
- package michael.zxing;
- import java.io.File;
- import java.util.Hashtable;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.client.j2se.MatrixToImageWriter;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- public class ZxingEncoderHandler {
- /**
- * 编码
- * @param contents
- * @param width
- * @param height
- * @param imgPath
- */
- public void encode(String contents, int width, int height, String imgPath) {
- Hashtable<Object, Object> hints = new Hashtable<Object, Object>();
- // 指定纠错等级
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
- // 指定编码格式
- hints.put(EncodeHintType.CHARACTER_SET, "GBK" );
- try {
- BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
- BarcodeFormat.QR_CODE, width, height, hints);
- MatrixToImageWriter
- .writeToFile(bitMatrix, "png" , new File(imgPath));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- String imgPath = "d:/test/twocode/michael_zxing.png" ;
- String contents = "Hello Michael(大大),welcome to Zxing!"
- + "\nMichael’s blog [ http://sjsky.iteye.com ]"
- + "\nEMail [ sjsky007@gmail.com ]" + "\nTwitter [ @suncto ]" ;
- int width = 300 , height = 300 ;
- ZxingEncoderHandler handler = new ZxingEncoderHandler();
- handler.encode(contents, width, height, imgPath);
- System.out.println("Michael ,you have finished zxing encode." );
- }
- }
运行后生成的二维码图片如下:
和前篇介绍一样,用手机的二维码扫描软件(本人用的:android 快拍二维码
)来测试下,识别成功的截图如下:
2-2. 解码示例:
- package michael.zxing;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.util.Hashtable;
- import javax.imageio.ImageIO;
- import com.google.zxing.BinaryBitmap;
- import com.google.zxing.DecodeHintType;
- import com.google.zxing.LuminanceSource;
- import com.google.zxing.MultiFormatReader;
- import com.google.zxing.Result;
- import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
- import com.google.zxing.common.HybridBinarizer;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- public class ZxingDecoderHandler {
- /**
- * @param imgPath
- * @return String
- */
- public String decode(String imgPath) {
- BufferedImage image = null ;
- Result result = null ;
- try {
- image = ImageIO.read(new File(imgPath));
- if (image == null ) {
- System.out.println("the decode image may be not exit." );
- }
- LuminanceSource source = new BufferedImageLuminanceSource(image);
- BinaryBitmap bitmap = new BinaryBitmap( new HybridBinarizer(source));
- Hashtable<Object, Object> hints = new Hashtable<Object, Object>();
- hints.put(DecodeHintType.CHARACTER_SET, "GBK" );
- result = new MultiFormatReader().decode(bitmap, hints);
- return result.getText();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null ;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- String imgPath = "d:/test/twocode/michael_zxing.png" ;
- ZxingDecoderHandler handler = new ZxingDecoderHandler();
- String decodeContent = handler.decode(imgPath);
- System.out.println("解码内容如下:" );
- System.out.println(decodeContent);
- System.out.println("Michael ,you have finished zxing decode." );
- }
- }
运行结果如下:
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. 编码示例:
- package michael.zxing;
- import java.io.File;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.client.j2se.MatrixToImageWriter;
- import com.google.zxing.common.BitMatrix;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- public class ZxingEAN13EncoderHandler {
- /**
- * 编码
- * @param contents
- * @param width
- * @param height
- * @param imgPath
- */
- public void encode(String contents, int width, int height, String imgPath) {
- int codeWidth = 3 + // start guard
- (7 * 6 ) + // left bars
- 5 + // middle guard
- (7 * 6 ) + // right bars
- 3 ; // end guard
- codeWidth = Math.max(codeWidth, width);
- try {
- BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
- BarcodeFormat.EAN_13, codeWidth, height, null );
- MatrixToImageWriter
- .writeToFile(bitMatrix, "png" , new File(imgPath));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- String imgPath = "d:/test/twocode/zxing_EAN13.png" ;
- // 益达无糖口香糖的条形码
- String contents = "6923450657713" ;
- int width = 105 , height = 50 ;
- ZxingEAN13EncoderHandler handler = new ZxingEAN13EncoderHandler();
- handler.encode(contents, width, height, imgPath);
- System.out.println("Michael ,you have finished zxing EAN13 encode." );
- }
- }
6 923450 657713 对应的是益达无糖口香糖:
运行后生成的条形码图片如下:
用手机的扫描软件,识别成功的截图如下:
3-2. 解码示例:
- package michael.zxing;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import javax.imageio.ImageIO;
- import com.google.zxing.BinaryBitmap;
- import com.google.zxing.LuminanceSource;
- import com.google.zxing.MultiFormatReader;
- import com.google.zxing.Result;
- import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
- import com.google.zxing.common.HybridBinarizer;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- public class ZxingEAN13DecoderHandler {
- /**
- * @param imgPath
- * @return String
- */
- public String decode(String imgPath) {
- BufferedImage image = null ;
- Result result = null ;
- try {
- image = ImageIO.read(new File(imgPath));
- if (image == null ) {
- System.out.println("the decode image may be not exit." );
- }
- LuminanceSource source = new BufferedImageLuminanceSource(image);
- BinaryBitmap bitmap = new BinaryBitmap( new HybridBinarizer(source));
- result = new MultiFormatReader().decode(bitmap, null );
- return result.getText();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null ;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- String imgPath = "d:/test/twocode/zxing_EAN13.png" ;
- ZxingEAN13DecoderHandler handler = new ZxingEAN13DecoderHandler();
- String decodeContent = handler.decode(imgPath);
- System.out.println("解码内容如下:" );
- System.out.println(decodeContent);
- System.out.println("Michael ,you have finished zxing EAN-13 decode." );
- }
- }
运行结果如下:
6923450657713
Michael ,you have finished zxing decode.
从测试结果可见:解码出的内容和之前编码的内容是一致。
- javase.jar (31.2 KB)
- 下载次数: 105
发表评论
-
SMSLib实现Java短信收发的功能(转)
2012-05-03 11:31 2023摘选:http://sjsky.iteye.com/blog/ ... -
log4j使用详解
2012-04-25 13:30 534log4j使用详解 ... -
读取条形码
2012-04-23 10:54 633(摘选: http://www.ibm.com/deve ... -
用 Java 实现断点续传 (HTTP)
2012-04-23 10:23 453断点续传的原理 其实断点续传的原理很简单,就是在 H ... -
用 Servlet 进行上载的原理和实现
2012-04-20 14:45 567用 Servlet 进行上载的原理和实现 (引用:h ... -
尽可能使用堆栈变量
2012-04-20 13:58 400如果您频繁存取变量,就需要考虑从何处存取这些变量。变量是 ... -
finally 块
2012-04-20 11:29 475finally 块必须与 try 或 ... -
mx参数
2012-04-20 11:25 479在大多数情况下,如果 ...
相关推荐
zxing.dll asp.net C#可以使用.net2.0,.net3.0条形码/二维码生成、识别、版本0.16.2
在Android上使用ZXing识别条形码/二维码 . 来自http://blog.csdn.net/hellogv/ 从pudn移过来的,好多人没有pudn的帐号。
Android 利用精简Zxing实现条形码/二维码 扫描Demo
android zxing简洁的条码/二维码扫描功能的实现
zxing.dll asp.net C#可以使用.net2.0 条形码/二维码生成、识别、V0.16.2
通过谷歌Zxing实现二维码扫一扫功能,生成二维码和条形吗并自定义扫一扫界面
android上使用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...
因为包的名称是android.google.*不能放在项目中运行,所以本人改了一下包名android.g.*这样就可以运行了,但源码没有改动。源码希望强大 大家可以导入进自己的项目里开发了
Google ZXing制作的条形码、二维码的生成、扫描Demo 源码,可以做为你的学习设计参考。
使用google开源zxing工具自己整理的二维码和条形码成功和解析代码, 新增加了生成带logo小图片的彩色二维码例子。 附加图片: 把img下面的图片放到c盘下做测试用
Google二维码生成解析zxing库 Android 代码,可以实现二维码生成,亲测可用。 代码为2016年6月9日从GitHub下载的,版本为3.2.1 GitHub对应地址代码为: https://github.com/zxing/zxing jar包原始下载地址为:...
与其相比的 https://github.com/codebude/QRCoder 开源项目,我经过尝试发现,QRCoder 在生成较长的字符串二维码时会失败,在识别图片中二维码质量较差或是较小时无法正常识别。于是最终选择了 ZXing.Net 开源库。 ...
zxing.delphi是最新3.9.5版,支持到delphi 12,好用的条形码与二维码扫描源代码ZXing.Delphi-v_3.0 - 副本.rar
快速存储电子名片越来越多的手机具备自动对焦的拍摄功能,这也意味着这些手机可以具备条码扫描的功能.......手机具备条码扫描的功能,可以优化购物流程,快速存储电子名片(二维码)等。(二维码)等。
vb生成二维码QRCODE,使用zxing; 方便简单地在vs form上生成二维码; 使用于.NET2.0以上。
zxing.jar用于二维码技术,zxing.jar用于二维码技术,zxing.jar用于二维码技术
直接添加引用zxing.dll,调用如下方法生成Bitmap然后可以打印或显示出来: private Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height) { Bitmap result = null; try { BarcodeWriter ...
里面包含全部需要的代码与jar包,下载后可直接运行,不需要额外其他资源。