使用zxing生成和解析二维码
二维码:
是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的;
在代码编制上巧妙的利用构成计算机内部逻辑基础的0和1比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图像输入设备或光电扫描设备自动识读以实现信息自动处理;
二维码能够在横向和纵向两个方位同时表达信息,因此能在很小的面积内表达大量的信息;
二维码相对于条形码的优势就是省空间;
zxing简介:
zxing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的接口。
zxing可以实现使用手机的内置的摄像头完成条形码和二维码的扫描与解码。
zxing可以实现条形码和二维码的编码与解码。
zxing目前支持的的格式如下:
UPC-A,UPC-E
EAN-8,EAN-13
39码
93码
代码128
QR码
项目环境:
jdk1.6 myEclipse10 zxing-core-2.2.jar(如果版本太高,出现版本不支持错误)
项目目录树

一:二维码生成
二维码生成需要借助MatrixToImageWriter类,该类是由Google提供
package com.dlab.xzing; import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream; import javax.imageio.ImageIO; import com.google.zxing.common.BitMatrix; public final class MatrixToImageWriter { private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF; private MatrixToImageWriter() { } public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth();
int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
} return image; } public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
} public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
} }
使用CreateImage类生成二维码:
package com.dlab.xzing; import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix; public class CreateImage { public static void main(String[] args) { try {
String content = "http://www.baidu.com"; MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints); File file = new File("cjz.jpg");
MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file); } catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} } }
生成的二维码如下:

二:解析二维码
二维码解析需要借助BufferedImageLuminanceSource类,该类是由Google提供
package com.dlab.xzing; import com.google.zxing.LuminanceSource; import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage; public final class BufferedImageLuminanceSource extends LuminanceSource { private final BufferedImage image;
private final int left;
private final int top; public BufferedImageLuminanceSource(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight());
} public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
super(width, height); int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
} for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
}
}
} this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top;
} @Override
public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException("Requested row is outside the image: " + y);
}
int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row;
} @Override
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix;
} @Override
public boolean isCropSupported() {
return true;
} @Override
public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
} @Override
public boolean isRotateSupported() {
return true;
} @Override
public LuminanceSource rotateCounterClockwise() { int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight(); AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth); BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose(); int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
} }
使用ResolveImage类解析二维码:
package com.dlab.xzing; import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer; public class ResolveImage { public static void main(String[] args) { try {
MultiFormatReader multiFormatReader = new MultiFormatReader(); File file = new File("cjz.jpg"); BufferedImage image = ImageIO.read(file); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); Result result = multiFormatReader.decode(binaryBitmap, hints); System.out.println("result: " + result.toString());
System.out.println("resultFormat: " + result.getBarcodeFormat());
System.out.println("resultText: " + result.getText()); } catch (IOException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
} } }
解析的内容如下:

使用zxing生成和解析二维码的更多相关文章
- ZXing 生成、解析二维码图片的小示例
概述 ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME ...
- Java使用Zxing生成、解析二维码工具类
Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法. 1.二维码的生成 (1).将Zxing-core.jar 包加入到classpath下. (2). ...
- zxing生成和解析二维码
今天忙了一天的二维码,用了QRcode和ZXing两个开源包.结果发现 ZXing比QRcode更好用一些,它直接可以定义二维码生成图案的大小,而QRcode生成的二维码是根据二维码包含的内容多少来定 ...
- 使用Google ZXing生成和解析二维码
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- 使用Google提供的ZXing Core,Java生成、解析二维码
1.maven项目中,pom.xml中引入ZXing Core工具包: <!-- https://mvnrepository.com/artifact/com.google.zxing/core ...
- Java生成、解析二维码
今天遇到需求,使用Java生成二维码图片,网搜之后,大神们早就做过,个人总结一下. 目标:借助Google提供的ZXing Core工具包,使用Java语言实现二维码的生成和解析. 步骤如下: 1.m ...
- JAVA中生成、解析二维码图片的方法
JAVA中生成.解析二维码的方法并不复杂,使用google的zxing包就可以实现.下面的方法包含了生成二维码.在中间附加logo.添加文字功能,并有解析二维码的方法. 一.下载zxing的架包,并导 ...
- java生成和解析二维码
前言 现在,二维码的应用已经非常广泛,在线生成器也是诸多,随手生成. 所以就和大家分享一个小案例,用zxing来做一个的二维码生成器,当然这个例子是比较简单,若是写的不好请多多包涵. ZXING项目是 ...
- Java使用QRCode.jar生成与解析二维码
原文V:http://www.cnblogs.com/bigroc/p/7496995.html#3797682 正题:Java使用QRCode.jar生成与解析二维码demo 欢迎新手共勉,大神监督 ...
随机推荐
- Flink(四) —— 数据流编程模型
分层抽象 The lowest level abstraction simply offers stateful streaming. It is embedded into the DataStre ...
- TCP/IP通信过程
一.参考网址 1.以太网帧格式.IP数据报格式.TCP段格式+UDP段格式 详解 2. 二.TCP的建立过程 1.例子: 192.168.22.66 telenet到192.168.22.74的tcp ...
- ImageTag,图片左上侧有一个小标签
这实现思路其实很简单 需求:1. 图片比原来的<div>大,需要切割图片.2. 图片左上角显示标签 解决思路: 1. 把照片设置成div的backgroundImage,然后用CSS3切割 ...
- Python合成GIF图片 -- imageio库
pip install imageio import imageio # 需要合在一起的图片 image_list = [r'C:\Users\Hlzy\Desktop\\' + str(x) + & ...
- Xcode7以后访问http加密
Xcode7 在Info.plist中add Row添加NSAppTransportSecurity类型Dictionary. 在NSAppTransportSecurity下添加NSAllowsAr ...
- 京东云数据库RDS SQL Server高可用概述
数据库的高可用是指在硬件.软件故障发生时,可以将业务从发生故障的数据库节点迁移至备用节点.本文主要讲述SQL Server高可用方案,以及京东云RDS数据库的高可用实现. 一.高可用解决方案总览 1. ...
- texshop 使用技巧
mac osX上使用latex 的技巧 指定编译器, 通过宏定义, 也就是在文件开头, 加上类似命令 % !TEX TS-program = pdflatex 多个文件设定主文件, 通过宏定义, 加上 ...
- Sort - Merge Sort
归并排序思想 归并排序时间复杂度分析 归并排序特点 归并排序实现 递归 #include <iostream> using namespace std; void merge(int *a ...
- 单个body|简单解释|复杂解释|反面解释
单个body有三种方法简单解释.复杂解释和反面解释 ========================================================================== ...
- JavaSE--Java 的基本程序设计结构
Java 对大小写敏感 Java 中定义类名的规则很宽松.名字必须以字母开头,后面可以跟字母和数字的任意组合.长度基本上没有限制.但是不能使用 Java 保留字作为类名. 标准的命名规范为:类名是以大 ...