首先将需要的jar加到工程中
QRCode.jar、zxingcore-2.2.jar、javase-2.2.jar(也可不加,增加一个MatrixToImageWriter类也可以)
package com.demo;
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;
/**
*
* @ClassName: MatrixToImageWriter
* @Description: 如果不想增加javase-2.2.jar,新增此类即可
* @date 2015年4月30日 上午10:05:51
* @since AR1.0
*/
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 com.demo;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.swetake.util.Qrcode;
public class QRCodeDemo {
public static void main(String[] args) {
QRCodeDemo.encoderQRCode("这是用Qrcode生成的二维码", "C:/Users/liyy/Desktop/TestQRCode.png");
QRCodeDemo.zXingTest("这是用zXing生成的二维码", "C:/Users/liyy/Desktop/TestZXing.png");
}
/**
*
* @Title: encoderQRCode
* @Description: 使用Qrcode生成二维码
* @param @param content
* @param @param imgPath
* @return void 返回类型
* @throws
*/
public static void encoderQRCode(String content, String imgPath) {
try {
Qrcode qrcodeHandler = new Qrcode();
qrcodeHandler.setQrcodeErrorCorrect(‘M‘);// 纠错
qrcodeHandler.setQrcodeEncodeMode(‘B‘);
qrcodeHandler.setQrcodeVersion(7);
byte[] contentBytes = content.getBytes("gb2312");
BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, 140, 140);
// 设定图像颜色> BLACK
gs.setColor(Color.BLACK);
// 设置偏移量
int pixoff = 2;
// 输出内容> 二维码
if ((contentBytes.length > 0) && (contentBytes.length < 120)) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect((j * 3) + pixoff, (i * 3) + pixoff, 3, 3);
}
}
}
} else {
System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");
}
gs.dispose();
bufImg.flush();
File imgFile = new File(imgPath);
// 生成二维码QRCode图片
ImageIO.write(bufImg, "png", imgFile);
System.out.println("Qrcode已输出二维码!");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @Title: zXingTest
* @Description: 使用google的zXing生成二维码
* @param
* @return void 返回类型
* @throws
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void zXingTest(String content, String imgPath) {
try {
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 file1 = new File(imgPath);
MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
System.out.println("zXing已输出二维码!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
原文:http://my.oschina.net/u/2292000/blog/408426