条形码/二维码之开源利器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包:
- zxing.jar
- zxing-j2se.jar
有关各种手机系统的应用,有兴趣的朋友可以下载官方源码包,包下有具体详细的应用介绍。
【二】、 二维码(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.");
- }
- }
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. 解码示例:
</pre><img class="spinner" style="DISPLAY: none" src="http://sjsky.iteye.com/images/spinner.gif" alt="" /></div></div><ol class="dp-j"><li><span class="keyword">package</span> michael.zxing; </li><li> </li><li><span class="keyword">import</span> java.awt.image.BufferedImage; </li><li><span class="keyword">import</span> java.io.File; </li><li><span class="keyword">import</span> java.util.Hashtable; </li><li> </li><li><span class="keyword">import</span> javax.imageio.ImageIO; </li><li> </li><li><span class="keyword">import</span> com.google.zxing.BinaryBitmap; </li><li><span class="keyword">import</span> com.google.zxing.DecodeHintType; </li><li><span class="keyword">import</span> com.google.zxing.LuminanceSource; </li><li><span class="keyword">import</span> com.google.zxing.MultiFormatReader; </li><li><span class="keyword">import</span> com.google.zxing.Result; </li><li><span class="keyword">import</span> com.google.zxing.client.j2se.BufferedImageLuminanceSource; </li><li><span class="keyword">import</span> com.google.zxing.common.HybridBinarizer; </li><li> </li><li><span class="comment">/** </span> </li><li><span class="comment"> * @blog http://sjsky.iteye.com </span> </li><li><span class="comment"> * @author Michael </span> </li><li><span class="comment"> */</span> </li><li><span class="keyword">public</span> <span class="keyword">class</span> ZxingDecoderHandler { </li><li> </li><li> <span class="comment">/** </span> </li><li><span class="comment"> * @param imgPath </span> </li><li><span class="comment"> * @return String </span> </li><li><span class="comment"> */</span> </li><li> <span class="keyword">public</span> String decode(String imgPath) { </li><li> BufferedImage image = <span class="keyword">null</span>; </li><li> Result result = <span class="keyword">null</span>; </li><li> <span class="keyword">try</span> { </li><li> image = ImageIO.read(<span class="keyword">new</span> File(imgPath)); </li><li> <span class="keyword">if</span> (image == <span class="keyword">null</span>) { </li><li> System.out.println(<span class="string">"the decode image may be not exit."</span>); </li><li> } </li><li> LuminanceSource source = <span class="keyword">new</span> BufferedImageLuminanceSource(image); </li><li> BinaryBitmap bitmap = <span class="keyword">new</span> BinaryBitmap(<span class="keyword">new</span> HybridBinarizer(source)); </li><li> </li><li> Hashtable<Object, Object> hints = <span class="keyword">new</span> Hashtable<Object, Object>(); </li><li> hints.put(DecodeHintType.CHARACTER_SET, <span class="string">"GBK"</span>); </li><li> </li><li> result = <span class="keyword">new</span> MultiFormatReader().decode(bitmap, hints); </li><li> <span class="keyword">return</span> result.getText(); </li><li> } <span class="keyword">catch</span> (Exception e) { </li><li> e.printStackTrace(); </li><li> } </li><li> <span class="keyword">return</span> <span class="keyword">null</span>; </li><li> } </li><li> </li><li> <span class="comment">/** </span> </li><li><span class="comment"> * @param args </span> </li><li><span class="comment"> */</span> </li><li> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> main(String[] args) { </li><li> String imgPath = <span class="string">"d:/test/twocode/michael_zxing.png"</span>; </li><li> ZxingDecoderHandler handler = <span class="keyword">new</span> ZxingDecoderHandler(); </li><li> String decodeContent = handler.decode(imgPath); </li><li> System.out.println(<span class="string">"解码内容如下:"</span>); </li><li> System.out.println(decodeContent); </li><li> System.out.println(<span class="string">"Michael ,you have finished zxing decode."</span>); </li><li> </li><li> } </li><li>} </li></ol></div><pre title="条形码/二维码之开源利器ZXing图文介绍" class="java" style="DISPLAY: none" name="code" codeable_id="" codeable_type="BlogComment" source_url="http://sjsky.iteye.com/blog/1142177#" pre_index="1">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.");
- }
- }
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 bars5 + // middle guard(7 * 6) + // right bars3; // end guardcodeWidth = 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.");
- }
- }
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.
从测试结果可见:解码出的内容和之前编码的内容是一致。
转载请注明来自:Michael's blog @ http://sjsky.iteye.com
条形码/二维码之开源利器ZXing图文介绍相关推荐
- 二维码扫描开源库ZXing定制化【转】
转自:http://www.cnblogs.com/sickworm/p/4562081.html 最近在用ZXing这个开源库做二维码的扫描模块,开发过程的一些代码修改和裁剪的经验和大家分享一下. ...
- 二维码扫描开源库ZXing定制化
最近在用ZXing这个开源库做二维码的扫描模块,开发过程的一些代码修改和裁剪的经验和大家分享一下. 建议: 如果需要集成到自己的app上,而不是做一个demo,不推荐用ZXing的Android外围开 ...
- JS生成条形码/二维码 barcode.js、JsBarcode
JS生成条形码/二维码 barcode.JsBarcode JsBarcode Barcode.js 以下代码均非纯原创.新手小白.网上一搜一大把的概念也不写了,直接上可运行的代码及遇到的小坑. Js ...
- 条形码/二维码生成探索
条形码/二维码生成探索 所用依赖 <!--条形码生成依赖(轻量型,推荐使用这个)(生成条码的同时会把信息生成到条形码下)--><dependency><groupId&g ...
- 个人用户永久免费,可自动升级版Excel插件,使用VSTO开发,Excel催化剂功能第12波-快速生成、读取、导出条形码二维码...
根据指定的内容生成对应的条形码或二维码,在如今移动互联网时代,并不是一件什么新鲜事,随便百度一下,都能找到好多的软件或在线网站可以帮我们做到,但细想一下,如果很偶然地只是生成一个两这样的图形,百度一下 ...
- GM65条形码二维码扫描识别模块与STM32学习
目录 模块介绍: 简介: 原理介绍: 工作流程: 技术参数: 修改配置: 代码介绍: 串口函数: 主函数: 模块介绍: 简介: GM65 条形码二维码扫描识别模块是一种基于激光扫描头的识别设备 原理介 ...
- Opencv+Zbar二维码识别(标准条形码/二维码识别)
使用Opencv+Zbar组合可以很容易的识别图片中的二维码,特别是标准的二维码,这里标准指的是二维码成像清晰,图片中二维码的空间占比在40%~100%之间,这样标准的图片,Zbar识别起来很容易,不 ...
- 【C#】最全单据打印(打印模板、条形码二维码、字体样式、项目源码)
系列文章 [C#]编号生成器(定义单号规则.固定字符.流水号.业务单号) 本文链接:https://blog.csdn.net/youcheng_ge/article/details/12912978 ...
- 条码条形码二维码检错系统供应
条码条形码二维码检错系统可防止因人为误操作.设备损坏等原因导致的错误,减少产品的退货返工,避免客户的投诉,提高工作效率.减少人力成本.适用于各种需要对条码进行检查的场合,如标签打印检查.装箱前对箱型 ...
最新文章
- python安装MySQLdb出错解决方案
- Vue组件多次点击报错Avoided redundant navigation to current location: “/profile“.
- android 仿ios tabs,React Native兼容iOS Android的TabBar
- Mybatis逆向生成报错:.\mbg.xml (系统找不到指定的文件)
- centos双系统只有linux,Centos添加Windows/Centos双系统启动
- KDD 2019论文解读:异构信息网络上的对抗生成学习
- 容器编排技术 -- Kubernetes kubectl rollout history 命令详解
- JQ js选择节点操作
- python月球地球质量计算_我们如何计算一个行星的质量?
- 电脑不香吗?我在手机上装Python我图什么?
- matlab数字仿真实验,matlab数值仿真
- 线下实体零售门店如何做好会员运营管理系统?
- 我的超长综合面经 ---- 北京大学 黄晔
- Collecting package metadata (current_repodata.json)解决方法
- getmonth_日期getMonth()方法以及JavaScript中的示例
- 如何查Unity3D编辑器崩溃原因
- 奥巴马就职演说的语言艺术
- PHP伪造IP或来源地址
- End-to-End Object Detection with Transformers[DETR]
- 操作系统课程ucore实验 lab1
热门文章
- u盘制作大师 linux系统教程,制作用U盘启动的Linux系统的简单步骤详解
- 深入理解 Go Modules 的 go.mod 与 go.sum
- java实现神经网络算法,java调用神经网络模型
- 在photoshop里如何使用抠图,换背景.有几种方法?
- 查看debian 版本和KDE还是Gnome
- VR全景在教育领域发展潜力如何?有哪些启发意义?
- 希捷和西数移动硬盘哪个好_移动硬盘选择希捷还是西数?很多人的想法其实都错了!...
- ASO优化之竞品分析该怎么做
- 好人卡:别让需要你的人觉得你太好
- 华为联运游戏或应用审核驳回:应用检测到支付serviceCatalog:X6