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" />

上一篇:以DefaultFuture为例,看类变量初始化顺序


下一篇:把dataTable数据转换为Html