ZXing实现条形码、二维码
来源:互联网 发布:sql字符串函数有哪些 编辑:程序博客网 时间:2024/06/10 18:29
一、 基本介绍
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等多种应用的类库,具体详情可以参考下载的源码包中。
本文主要是在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.");
- }
- }
运行后生成的二维码图片如下:
和前篇介绍一样,用手机的二维码扫描软件(本人用的: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.
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.");
- }
- }
运行后生成的条形码图片如下:
用手机的扫描软件,识别成功的截图如下:
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.
6923450657713
Michael ,you have finished zxing decode.
从测试结果可见:解码出的内容和之前编码的内容是一致。
0 0
- ZXing实现条形码、二维码
- java二维码,条形码,【ZXing】
- 条形码+二维码,开源ZXing
- Zxing生成二维码 条形码
- Android Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Android基于Zxing实现二维码,条形码扫描和生成二维码
- 在Android中使用 Google ZXing 实现二维码、条形码扫描
- 在Android中使用 Google ZXing 实现二维码、条形码扫描
- Android 基于google Zxing实现二维码、条形码扫描
- Android 利用精简Zxing实现条形码/二维码 扫描
- Android基于Google Zxing实现二维码、条形码的扫
- Android 基于google Zxing实现二维码、条形码扫描
- Android基于Google Zxing实现二维码/条形码扫描、生成二维码/条形码
- 二维码、条形码扫描—ZXing
- ios ZXing 二维码、条形码扫描
- zxing生成二维码或者条形码
- Zxing二维码、条形码扫描/生成
- ZXing 生成二维码和条形码
- 金蝶KIS专业版实现'条码标签打印'功能对物料多个条码打印
- 常用JavaScript验证小工具集,以及部分jQuery 扩展再总结
- 常用机器学习算法分类概述
- 一个从阿里面试回来的程序员的感想
- iOS推送详解(一)基本集成操作
- ZXing实现条形码、二维码
- gitlab密码重置
- Android中Activity启动模式
- JS闭包问题?
- MSER最大稳定极值区域
- CentOS 7.0安装配置LAMP服务器(Apache+PHP+MariaDB)
- 微信小程序时代,我来了
- 对Apple ProRes编解码器的学习
- 12.json文件的格式