使用JAVA生成以及解析二维码

jar

 
生成和解析二维码需要用到第三方的包: QRCODE.jar :http://download.how2j.cn/1629/qrcode.rar
 

代码

 
二维码如何实现不用关心,只用关心两个方法:
把字符串写进二维码,并且生成图片到destFile
 
public static void qrCodeEncode(String encodeddata, File destFile)
 


把二维码信息从imageFile中读取出来
 
public static String qrCodeDecode(File imageFile)
 


主方法里就是实例,把字符串写进了qrcode.png,同时从qrcode.png里解析出字符串来
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
import javax.imageio.ImageIO;
 
import com.swetake.util.Qrcode;
 
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
 
public class QRCodeUtil {
 
     public static void qrCodeEncode(String encodeddata, File destFile) throws IOException { 
            Qrcode qrcode = new Qrcode(); 
            qrcode.setQrcodeErrorCorrect('M');  // 纠错级别(L 7%、M 15%、Q 25%、H 30%)和版本有关 
            qrcode.setQrcodeEncodeMode('B');     
            qrcode.setQrcodeVersion(7);     // 设置Qrcode包的版本 
               
            byte[] d = encodeddata.getBytes("GBK"); // 字符集 
            BufferedImage bi = new BufferedImage(139139, BufferedImage.TYPE_INT_RGB); 
            // createGraphics   // 创建图层 
            Graphics2D g = bi.createGraphics(); 
               
            g.setBackground(Color.WHITE);   // 设置背景颜色(白色) 
            g.clearRect(00139139);    // 矩形 X、Y、width、height 
            g.setColor(Color.BLACK);    // 设置图像颜色(黑色) 
       
            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 233); 
                        
                    
                
            
               
//        Image img = ImageIO.read(new File("D:/tt.png"));  logo 
//        g.drawImage(img, 25, 55,60,50, null); 
                       
            g.dispose(); // 释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象 
            bi.flush(); // 刷新此 Image 对象正在使用的所有可重构的资源 
       
            ImageIO.write(bi, "png", destFile); 
//          System.out.println("Input Encoded data is:" + encodeddata); 
        
       
        /**
         * 解析二维码,返回解析内容
         
         * @param imageFile
         * @return
         */ 
        public static String qrCodeDecode(File imageFile) { 
            String decodedData = null
            QRCodeDecoder decoder = new QRCodeDecoder(); 
            BufferedImage image = null
            try 
                image = ImageIO.read(imageFile); 
            catch (IOException e) { 
                System.out.println("Error: " + e.getMessage()); 
            
       
            try 
                decodedData = new String(decoder.decode(new J2SEImage(image)), "GBK"); 
//              System.out.println("Output Decoded Data is:" + decodedData); 
            catch (DecodingFailedException dfe) { 
                System.out.println("Error: " + dfe.getMessage()); 
            catch (UnsupportedEncodingException e) { 
                e.printStackTrace(); 
            
            return decodedData; 
        
       
        static class J2SEImage implements QRCodeImage { 
            BufferedImage image; 
           
            public J2SEImage(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); 
            
        
         
        public static void main(String[] args) { 
            String filePath = "d:/qrcode.png"
            File qrFile = new File(filePath); 
       
            // 二维码内容 
            String encodeddata = "http://how2j.cn"
            try 
                QRCodeUtil.qrCodeEncode(encodeddata, qrFile); 
            catch (IOException e) { 
                e.printStackTrace(); 
            
       
            // 解码 
            String reText = QRCodeUtil.qrCodeDecode(qrFile); 
            System.out.println(reText); 
        
}
下载地址:http://download.how2j.cn/1630/QRCode.jar

posted on 2019-01-04 14:47  我是司  阅读(335)  评论(0)    收藏  举报

导航