今天忙了一天的二维码,用了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生成和解析二维码的更多相关文章

  1. ZXing 生成、解析二维码图片的小示例

    概述 ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME ...

  2. Java使用Zxing生成、解析二维码工具类

    Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法. 1.二维码的生成 (1).将Zxing-core.jar 包加入到classpath下. (2). ...

  3. 使用zxing生成和解析二维码

    二维码: 是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的: 在代码编制上巧妙的利用构成计算机内部逻辑基础的0和1比特流的概念,使用若干个与二进制相对应的几何 ...

  4. 使用Google ZXing生成和解析二维码

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  5. 使用Google提供的ZXing Core,Java生成、解析二维码

    1.maven项目中,pom.xml中引入ZXing Core工具包: <!-- https://mvnrepository.com/artifact/com.google.zxing/core ...

  6. Java生成、解析二维码

    今天遇到需求,使用Java生成二维码图片,网搜之后,大神们早就做过,个人总结一下. 目标:借助Google提供的ZXing Core工具包,使用Java语言实现二维码的生成和解析. 步骤如下: 1.m ...

  7. JAVA中生成、解析二维码图片的方法

    JAVA中生成.解析二维码的方法并不复杂,使用google的zxing包就可以实现.下面的方法包含了生成二维码.在中间附加logo.添加文字功能,并有解析二维码的方法. 一.下载zxing的架包,并导 ...

  8. java生成和解析二维码

    前言 现在,二维码的应用已经非常广泛,在线生成器也是诸多,随手生成. 所以就和大家分享一个小案例,用zxing来做一个的二维码生成器,当然这个例子是比较简单,若是写的不好请多多包涵. ZXING项目是 ...

  9. Java使用QRCode.jar生成与解析二维码

    原文V:http://www.cnblogs.com/bigroc/p/7496995.html#3797682 正题:Java使用QRCode.jar生成与解析二维码demo 欢迎新手共勉,大神监督 ...

随机推荐

  1. Solr4.8.0源码分析(16)之SolrCloud索引深入(3)

    Solr4.8.0源码分析(16)之SolrCloud索引深入(3) 前面两节学习了SolrCloud索引过程以及索引链的前两步,LogUpdateProcessorFactory和Distribut ...

  2. 使用Protractor进行AngularJS e2e测试案例

    环境: y@y:karma-t01$ protractor --version Version y@y:karma-t01$ node -v v4.2.2 y@y:karma-t01$ y@y:kar ...

  3. angular2 学习笔记 ( Form 表单 )

    refer : https://angular.cn/docs/ts/latest/guide/forms.html https://angular.cn/docs/ts/latest/cookboo ...

  4. Milking Grid

    poj2185:http://poj.org/problem?id=2185 题意:在一个字符矩阵中,找一个最小的字符子矩阵,使其能够覆盖整个矩阵. 题解:在KMP中i-next[i]是这能够覆盖这个 ...

  5. Multiplication Table

    CF#256D:http://codeforces.com/contest/448/problem/D 题意:给你一个n*m的表格,第i行第j列的数据是i*j,然后询问第k小的数,这里的排序是不去重的 ...

  6. h.264 率失真优化

    Rate Distortion Optimization 搜索时,一个不可避免的问题就是如何对mv进行比较,从而得到最优 对于同一压缩算法来说,码率越高表示图像质量越好.失真越小,但是码率越高要求更大 ...

  7. Struts2 文件的上传与下载

    1. Struts2的文件上传需要Apache的commons-io-Version.jar和commons-fileupload-Version.jar两个jar包.2. 页面中的<s:fil ...

  8. 【HDOJ】2395 Alarm Clock

    水题. /* 2395 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MAX ...

  9. <<开源硬件创客 15个酷应用玩转树莓派>>

    本书共分18章,前3章是本书的基础章节,主要介绍了树莓派的一些基本情况和基本操作,来让读者了解树莓派的前世今生,掌握树莓派基本的使用方法.第4~18章主要介绍15个以树莓派为载体的酷炫应用,大家可以按 ...

  10. COJN 0486 800401反质数 呵呵呵呵呵

    800401反质数 难度级别:A: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 将正整数 x 的约数个数表示为 g(x).例如,g(1)=1 ...