一、zxing 的 jar 包及源码下载
http://code.google.com/p/zxing/downloads/list
本示例选择的是ZXing-2.0.zip
二、将下载的 ZXing-2.0.zip 解压,取出 core 及 javase ,将 core 包下的
com/google/zxing/qrcode/encoder/Encoder.java 的
static final String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";
改成
static final String DEFAULT_BYTE_MODE_ENCODING = "utf-8";
以支持中文
三、生成代码
1. GenerateQRCode.java
其中的 DateUtils.getStringDate1() 是一个时间格式的String类型(yyyyMMddHH:mm:ss)
import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import org.apache.log4j.Logger; import cn.com.do1.component.util.DateUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; /** * @ClassName: GenerateQRCode * @Description: 生成二维码 * @author * @company * @date 2012-11-9 * @version V1.0 */ public final class GenerateQRCode { private static final Logger log = Logger.getLogger(GenerateQRCode.class); private static final GenerateQRCode instance = new GenerateQRCode(); private GenerateQRCode() { } public static GenerateQRCode getInstance() { return instance; } private static final int BLACK = 0xff000000; private static final int WHITE = 0xFFFFFFFF; /** * @Title: generate * @Description: 生成二维码 * @param assetsName * 二维码图片名称 * @param params * 二维码信息 * @param width * 生成的图片的宽 * @param height * 生成的图片的高 * @param path * 二维码图片存放目录 * @throws Exception * @return String 二维码图片名称 * @author * @date 2012-11-9 */ public String generate(String assetsName, String params, String path, int width, int height) throws Exception { log.info("GenerateQRCode-->start to generate qrcode."); log.info("the qrcode's save path is:" + path); File file = new File(path); if (!file.exists()) { file.mkdirs(); } // 二维码图片名称 String fileName = assetsName.concat("_") .concat(DateUtils.getStringDate1()).concat(".png"); // 二维码图片存放路径 path = path.concat(fileName); log.info("the qrcode's path is:" + path); file = new File(path); if (!file.exists()) { file.createNewFile(); } BitMatrix bitMatrix = new MultiFormatWriter().encode(params, BarcodeFormat.QR_CODE, width, height); writeToFile(bitMatrix, "png", file); log.info("GenerateQRCode-->end to generate qrcode."); return fileName; } public static void writeToFile(BitMatrix matrix, String format, File file) throws Exception { BufferedImage image = toBufferedImage(matrix); ImageIO.write(image, format, file); } public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE); } } return image; } }
四、调用
其中,Constants.CHANGE_LINE 表示换行符 \r\n
public void encode() { // 二维码内容 StringBuffer params = new StringBuffer(); params.append("资产名称:").append("你好").append(Constants.CHANGE_LINE); params.append("资产编号:").append("adfadf").append(Constants.CHANGE_LINE); params.append("资产类型:").append("网络设备").append(Constants.CHANGE_LINE); params.append("所属部门:").append("综合部").append(Constants.CHANGE_LINE); params.append("制码时间:").append(DateUtils.getStringDate()).append(Constants.CHANGE_LINE); params.append("审核人:").append("管理员"); // 二维码图片生成路径 String path = "D:\\Project\\foshan_tobacco\\code\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\fsycsms\\images\\qrcodeImg\\"; try { String imgPath = GenerateQRCode.getInstance().generate( "你好",params.toString(), path, 200, 200); System.out.println(imgPath); System.out.println("生成二维码成功"); } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
五、附件
具体的代码请查看附件 fsycsms-qrcode.rar
附件的工程是一个maven工程。
请导入 eclipse 中,eclipse 我使用的版本是 eclipse-jee-indigo-SR2-win32 (3.7的)
工程中的代码结构如下:
其中的源包(Source Folder) src/qrcode/core 和 src/qrcode/javase ,就是在上面第2步中取出的包