1 public class QRCode {
2 /**
3 * 解析二维码(QRCode)
4 * @param imgPath
5 * @return
6 */
7 public static String decoderQRCode(String imgPath) {
8 // QRCode 二维码图片的文件
9 File imageFile = new File(imgPath);
10 BufferedImage bufImg = null;
11 String content = null;
12 try {
13 bufImg = ImageIO.read(imageFile);
14 QRCodeDecoder decoder = new QRCodeDecoder();
15 content = new String(decoder.decode(new QRCodeImageBean(bufImg)), "utf-8");
16 } catch (IOException e) {
17 System.out.println("Error: " + e.getMessage());
18 e.printStackTrace();
19 } catch (DecodingFailedException dfe) {
20 System.out.println("Error: " + dfe.getMessage());
21 dfe.printStackTrace();
22 }
23 return content;
24 }
25 /**
26 * 生成二维码(QRCode)图片
27 * @param content 存储内容
28 * @param imgPath 图片路径
29 * @param imgType 图片类型
30 */
31 public static void encoderQRCode(String content, String imgPath, String imgType) {
32 encoderQRCode(content, imgPath, imgType, 7);
33 }
34 /**
35 * 生成二维码(QRCode)图片
36 * @param content 存储内容
37 * @param imgPath 图片路径
38 * @param imgType 图片类型
39 * @param size 二维码尺寸
40 */
41 public static void encoderQRCode(String content, String imgPath, String imgType, int size) {
42 try {
43 BufferedImage bufImg = qRCodeCommon(content, imgType, size);
44
45 File imgFile = new File(imgPath);
46 // 生成二维码QRCode图片
47 ImageIO.write(bufImg, imgType, imgFile);
48 } catch (Exception e) {
49 e.printStackTrace();
50 }
51 }
52
53 /**
54 * 生成二维码(QRCode)图片的公共方法
55 * @param content 存储内容
56 * @param imgType 图片类型
57 * @param size 二维码尺寸
58 * @return
59 */
60 private static BufferedImage qRCodeCommon(String content, String imgType, int size) {
61 BufferedImage bufImg = null;
62 try {
63 Qrcode qrcodeHandler = new Qrcode();
64 // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
65 qrcodeHandler.setQrcodeErrorCorrect('M');
66 qrcodeHandler.setQrcodeEncodeMode('B');
67 // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
68 qrcodeHandler.setQrcodeVersion(size);
69 // 获得内容的字节数组,设置编码格式
70 byte[] contentBytes = content.getBytes("utf-8");
71 // 图片尺寸
72 int imgSize = 67 + 12 * (size - 1);
73 bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
74 Graphics2D gs = bufImg.createGraphics();
75 // 设置背景颜色
76 gs.setBackground(Color.WHITE);
77 gs.clearRect(0, 0, imgSize, imgSize);
78
79 // 设定图像颜色> BLACK
80 gs.setColor(Color.BLACK);
81 // 设置偏移量,不设置可能导致解析出错
82 int pixoff = 2;
83 // 输出内容> 二维码
84 if (contentBytes.length > 0 && contentBytes.length < 800) {
85 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
86 for (int i = 0; i < codeOut.length; i++) {
87 for (int j = 0; j < codeOut.length; j++) {
88 if (codeOut[j][i]) {
89 gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
90 }
91 }
92 }
93 } else {
94 throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
95 }
96 gs.dispose();
97 bufImg.flush();
98 } catch (Exception e) {
99 e.printStackTrace();
100 }
101 return bufImg;
102 }
103 public static void main(String[] args) {
104 //生成二维码
105 String imgPath = "d:/测试用二维码";
106 String encoderContent = "你好,这是测试用例"; 108 encoderQRCode(encoderContent, imgPath, "png");
109 System.out.println("encoder success!!!");
110
111 //解析二维码
112 // String imgPath = "D:/测试用二维码.jpg"; 114 // String qrCon = decoderQRCode(imgPath);
115 // System.out.println("decoder success!!!");
116 // System.out.println("二维码内容为:" + qrCon);
117 }
118
119 }