zxing生成和解析二维码
今天忙了一天的二维码,用了QRcode和ZXing两个开源包。结果发现
ZXing比QRcode更好用一些,它直接可以定义二维码生成图案的大小,而QRcode生成的二维码是根据二维码包含的内容多少来定的。所以生成的二维码图片大小不好确定。
下面主要说一下ZXing生成二维码的方法:
ZXing是google的开源资源,但是由于中国的某些原因使得访问谷歌的资源比较难,所以在网上找到了一个可以下载的连接:http://www.cr173.com/soft/65529.html
下载好需要的核心jar包后我们还需要下载一个可以将资源输出到文件或流的工具类MatrixToImageWriter,以下是百度到的源码:
package zxingTest;
import com.google.zxing.common.BitMatrix; import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage; 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);
}
} }
接下来需要写一个测试类了,不过我已经测试过了,并进行了简单包装:
package zxingTest; import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix; public class TwoDimCodeUtil { /**
* @Title: writeToFile 将二维码输出到文件
* @Description: 将二维码输出到文件
* @param fileName 文件名称
* @param contents 二维码包含的内容
* @param encodeType 字符编码
* @param imgSize 二维码生成图片大小,单位像素
* @param imgType 图片格式,jpg,png等
* @throws
*/
public static void writeToFile(String fileName,String contents,String encodeType,int imgSize,String imgType){
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET,encodeType);
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, imgSize, imgSize, hints);
File file = new File(fileName);
MatrixToImageWriter.writeToFile(matrix, imgType, file);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* @Title: writeToFile 将二维码输出到流
* @Description: 将二维码输出到流
* @param outputStream 输出流
* @param contents 二维码包含的内容
* @param encodeType 字符编码
* @param imgSize 二维码生成图片大小
* @param imgType 图片格式,jpg,png等
* @throws
*/
public static void writeToStream(OutputStream outputStream,String contents,String encodeType,int imgSize,String imgType){
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET,encodeType);
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, imgSize, imgSize, hints);
MatrixToImageWriter.writeToStream(matrix, imgType, outputStream);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* @Title: writeToFile 将二维码输出到流,默认编码utf-8,文件类型jpg
* @Description: 将二维码输出到流
* @param outputStream 输出流
* @param contents 二维码包含的内容
* @param imgSize 二维码生成图片大小
* @throws
*/
public static void writeToStream(OutputStream outputStream,String contents,int imgSize){
TwoDimCodeUtil.writeToStream(outputStream, contents, "UTF-8", imgSize, "jpg");
}
}
下面是调用的一个例子:
public void showQRCode(){
HttpServletRequest request=this.getRequest();
HttpServletResponse response=this.getResponse();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
String content=request.getParameter("content");
String widthstr= request.getParameter("width");
int width=(widthstr==null||widthstr.equals(""))?140:Integer.valueOf(widthstr);
System.out.println(content);
try {
TwoDimCodeUtil.writeToStream(response.getOutputStream(), content, width);
} catch (IOException e) {
e.printStackTrace();
}
}
页面调用:
<img src="/showQRCode.do?content=helloworld&width=140" />
zxing生成和解析二维码的更多相关文章
- ZXing 生成、解析二维码图片的小示例
概述 ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME ...
- Java使用Zxing生成、解析二维码工具类
Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法. 1.二维码的生成 (1).将Zxing-core.jar 包加入到classpath下. (2). ...
- 使用zxing生成和解析二维码
二维码: 是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的: 在代码编制上巧妙的利用构成计算机内部逻辑基础的0和1比特流的概念,使用若干个与二进制相对应的几何 ...
- 使用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 欢迎新手共勉,大神监督 ...
随机推荐
- iOS常见内存泄漏解决
iOS常见内存泄漏解决 1 OC和CF转化出现的内存警告 CFStringRef cfString = CFURLCreateStringByAddingPercentEscapes(kCFA ...
- 理解java中的ThreadLocal(转)
一.对ThreadLocal概述 JDK API 写道: 该类提供了线程局部 (thread-local) 变量.这些变量不同于它们的普通对应物,因为访问某个变量(通过其 get 或 set 方法)的 ...
- OpenWrt+nginx+php安装discuz
下面这个图片是本次的硬件资源:一个无线路由器的开发板,一个8G的u盘,一条手机的数据线(可以作为串口和供电使用),一条网线,一个USB Hub. <ignore_js_op> ...
- POJ 1734 Sightseeing trip
题目大意: 求一个最小环. 用Floyd 求最小环算法. #include <iostream> #include <cstdlib> #include <cstdio& ...
- 【宽搜】Vijos P1051 送给圣诞夜的极光
题目链接: https://vijos.org/p/1051 题目大意: 给一张‘-’和‘#’的图,规定曼哈顿距离小于等于2的‘#’属于同一图案,求图案数.[曼哈顿距离:对于A(x1,y1)和B(x2 ...
- 64位操作系统 注册 capicom.dll
把capicom.dll 放到c:\windows\syswow64目录 以管理员身份运行c:\windows\syswow64\cmd.exe 执行 regsvr32 capicom.dll ...
- cryptopp开源库的使用(二):base64加密
很多时候我只是优秀工具的使用者,优秀的工具用好了才能发挥作用 最近使用cryptopp的base64对压缩后的zip文件内容进行加密遇到了问题. 首先zip压缩没问题,可是最后得到的base64字符串 ...
- C# 将数据集以excel的形式输出
private void SaveLastMonthAuthorPays() { string fileName = "LastMonthAuthorPa ...
- CSAPP:Binary Bomb
本篇文章参考了:http://www.cnblogs.com/remlostime/archive/2011/05/21/2052708.html大神的文章,有时候没思路了会来看一下,但是保证本文的每 ...
- HTTP学习笔记4-请求与响应结构例子
18,HTTP消息由客户端到服务器的请求和服务器到客户端的响应组成.请求消息和响应消息都是由开始行,消息报头(可选的),空行(只有CRLF的行),消息正文(可选的)组成. 19,对于请求消息,开始行就 ...