需要的jar包:QRCode.jar
效果图:
以下为我封装的辅助类,提供的API只有:
(1)encode(String str);
(2)encode(String str,File f);
(3)encode(String str,OutputStream out);
(4)decode(File f);
(5)decode(InputStream in);
package org.xiazdong; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.InputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import jp.sourceforge.qrcode.QRCodeDecoder; import jp.sourceforge.qrcode.data.QRCodeImage; import com.swetake.util.Qrcode; public final class QRCodeUtils { private QRCodeUtils() { } public static BufferedImage encode(String str) throws Exception { // 1. 创建Qrcode Qrcode qrcode = new Qrcode(); qrcode.setQrcodeErrorCorrect('M'); qrcode.setQrcodeEncodeMode('B'); qrcode.setQrcodeVersion(7); // 2.数字以UTF-8编码 byte[] d = str.getBytes("UTF-8"); BufferedImage image = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setBackground(Color.WHITE); g.clearRect(0, 0, 139, 139); g.setColor(Color.BLACK); // 3.构造二维码图片 if (d.length > 0 && d.length < 123) { boolean[][] b = qrcode.calQrcode(d); for (int i = 0; i < b.length; i++) { for (int j = 0; j < b.length; j++) { if (b[j][i]) { g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3); } } } } g.dispose(); image.flush(); return image; } public static void encode(String str, File f) throws Exception { // 1. 创建Qrcode Qrcode qrcode = new Qrcode(); qrcode.setQrcodeErrorCorrect('M'); qrcode.setQrcodeEncodeMode('B'); qrcode.setQrcodeVersion(7); // 2.数字以UTF-8编码 byte[] d = str.getBytes("UTF-8"); BufferedImage image = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setBackground(Color.WHITE); g.clearRect(0, 0, 139, 139); g.setColor(Color.BLACK); // 3.构造二维码图片 if (d.length > 0 && d.length < 123) { boolean[][] b = qrcode.calQrcode(d); // 计算二位布尔数组 for (int i = 0; i < b.length; i++) { for (int j = 0; j < b.length; j++) { if (b[j][i]) { g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3); // 根据布尔数组填充图片的各点 } } } } g.dispose(); image.flush(); ImageIO.write(image, "png", f); } public static void encode(String str, OutputStream out) throws Exception { // 1. 创建Qrcode Qrcode qrcode = new Qrcode(); qrcode.setQrcodeErrorCorrect('M'); qrcode.setQrcodeEncodeMode('B'); qrcode.setQrcodeVersion(7); // 2.数字以UTF-8编码 byte[] d = str.getBytes("UTF-8"); BufferedImage image = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setBackground(Color.WHITE); g.clearRect(0, 0, 139, 139); g.setColor(Color.BLACK); // 3.构造二维码图片 if (d.length > 0 && d.length < 123) { boolean[][] b = qrcode.calQrcode(d); for (int i = 0; i < b.length; i++) { for (int j = 0; j < b.length; j++) { if (b[j][i]) { g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3); } } } } g.dispose(); image.flush(); ImageIO.write(image, "png", out); } public static String decode(File f) throws Exception { QRCodeDecoder decoder = new QRCodeDecoder(); BufferedImage image = null; image = ImageIO.read(f); // 获得BufferedImage String decodedData = new String(decoder.decode(new MyImage(image)), // 解码,并生成字符串 "UTF-8"); return decodedData; } public static String decode(InputStream in) throws Exception { QRCodeDecoder decoder = new QRCodeDecoder(); BufferedImage image = null; image = ImageIO.read(in); String decodedData = new String(decoder.decode(new MyImage(image)), "UTF-8"); return decodedData; } } class MyImage implements QRCodeImage { BufferedImage image; public MyImage(BufferedImage image) { this.image = image; } public int getWidth() { return image.getWidth(); } public int getHeight() { return image.getHeight(); } public int getPixel(int x, int y) { return image.getRGB(x, y); } }
测试类:
package test; import java.io.File; import org.xiazdong.QRCodeUtils; public class Test { public static void main(String[] args) throws Exception { QRCodeUtils.encode("xiazdong", new File("1.png")); String str = QRCodeUtils.decode(new File("1.png")); System.out.println(str); } }
(1) http://blog.csdn.net/wangpeng047/article/details/7181217
(2) http://zjgzpc.iteye.com/blog/1478106