使用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 欢迎新手共勉,大神监督 ...
随机推荐
- 拷贝构造函数[c++]
拷贝构造函数何时会被调用? 1. 对象以值传递的方式传入函数参数 2.对象以值传递的方式从函数返回 3.对象需要通过另外一个对象进行初始化 下面我们来看代码: //#include <iostr ...
- 腾讯一shell试题.
腾讯一shell试题. 假设qq.tel文件内容: 12334:13510014336 12345:12334555666 12334:12343453453 12099:13598989899 12 ...
- OpenMP笔记(四)
个人博客地址:http://www.bearoom.xyz/2019/02/22/openmp4/ 一.private private子句用于将一个或多个变量声明成线程私有的变量,这样每个线程都有该变 ...
- 3. laravel 5.5 多子域名 + dingo + jwt 简单环境搭建
环境介绍 laravel 5.5.* + php 7.2 + mysql 5.7.27 1. 创建 laravel 项目 (自行 配置一下域名 如果 不会 请参考laravel 的第一篇文章) com ...
- 感知机分类(perceptron classification)
概述 在机器学习中,感知机(perceptron)是二分类的线性分类模型,属于监督学习算法.输入为实例的特征向量,输出为实例的类别(取+1和-1). 感知机对应于输入空间中将实例划分为两类的分离超平面 ...
- c# 之循环 ,while 和do---while还有for
㈠while循环 循环条件 是个bool值,为true时执行循环,为false退出循环.break一般不单独的使用,而是跟着if判断一起使用,表示,当满足某些条件的时候,就退出循环了. 循环体 一般总 ...
- PAT Basic 1075 链表元素分类(25) [链表]
题目 给定⼀个单链表,请编写程序将链表元素进⾏分类排列,使得所有负值元素都排在⾮负值元素的前⾯,⽽[0, K]区间内的元素都排在⼤于K的元素前⾯.但每⼀类内部元素的顺序是不能改变的.例如:给定链表为 ...
- NGINX常用模块(二)
5.Nginx日志配置 Nginx有非常灵活的日志记录模式.每个级别的配置可以有各自独立的访问日志.日志格式 通过log_format命令定义格式 1.log_format指令 # 配置语法:包括:e ...
- php 接口中常用的aes加密
<?php /** * Aes 对称加密 */ class Aes { public $key; public function __construct($key){ $this->key ...
- c语言中对字段宽度的理解?
/************************************************************************* > File Name: printf.c ...