QRCode.js 生成二维码

之前朋友问我关于二维码的,当时为了帮他我就去问问了前端的怎么编写的二维码。现在刚好我的项目里也需要设置二维码,刚好用上了。嘻嘻!

QRCode.js 是一个用于生成二维码图片的插件。

http://code.ciaoca.com/javascript/qrcode/  去这里看吧!

我朋友用的是java编写的。很棒的!

  1 package twoDimensionCode;  
  2   
  3 import java.awt.Color;  
  4 import java.awt.Graphics2D;  
  5 import java.awt.image.BufferedImage;  
  6 import java.io.File;  
  7 import java.io.FileOutputStream;
  8 import java.io.IOException;  
  9 import java.io.InputStream;  
 10 import java.io.OutputStream;  
 11   
 12 import javax.imageio.ImageIO;  
 13   
 14 import jp.sourceforge.qrcode.QRCodeDecoder;  
 15 import jp.sourceforge.qrcode.exception.DecodingFailedException;  
 16   
 17 import com.swetake.util.Qrcode;  
 18   
 19 public class TwoDimensionCode {  
 20       
 21     /** 
 22      * 生成二维码(QRCode)图片 
 23      * @param content 存储内容 
 24      * @param imgPath 图片路径 
 25      */  
 26     public void encoderQRCode(String content, String imgPath) {  
 27         this.encoderQRCode(content, imgPath, "png", 7);  
 28     }  
 29       
 30     /** 
 31      * 生成二维码(QRCode)图片 
 32      * @param content 存储内容 
 33      * @param output 输出流 
 34      */  
 35     public void encoderQRCode(String content, OutputStream output) {  
 36         this.encoderQRCode(content, output, "png", 7);  
 37     }  
 38       
 39     /** 
 40      * 生成二维码(QRCode)图片 
 41      * @param content 存储内容 
 42      * @param imgPath 图片路径 
 43      * @param imgType 图片类型 
 44      */  
 45     public void encoderQRCode(String content, String imgPath, String imgType) {  
 46         this.encoderQRCode(content, imgPath, imgType, 7);  
 47     }  
 48       
 49     /** 
 50      * 生成二维码(QRCode)图片 
 51      * @param content 存储内容 
 52      * @param output 输出流 
 53      * @param imgType 图片类型 
 54      */  
 55     public void encoderQRCode(String content, OutputStream output, String imgType) {  
 56         this.encoderQRCode(content, output, imgType, 7);  
 57     }  
 58   
 59     /** 
 60      * 生成二维码(QRCode)图片 
 61      * @param content 存储内容 
 62      * @param imgPath 图片路径 
 63      * @param imgType 图片类型 
 64      * @param size 二维码尺寸 
 65      */  
 66     public void encoderQRCode(String content, String imgPath, String imgType, int size) {  
 67         try {  
 68             BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);  
 69               
 70             File imgFile = new File(imgPath);  
 71             // 生成二维码QRCode图片  
 72             ImageIO.write(bufImg, imgType, imgFile);  
 73         } catch (Exception e) {  
 74             e.printStackTrace();  
 75         }  
 76     }  
 77   
 78     /** 
 79      * 生成二维码(QRCode)图片 
 80      * @param content 存储内容 
 81      * @param output 输出流 
 82      * @param imgType 图片类型 
 83      * @param size 二维码尺寸 
 84      */  
 85     public void encoderQRCode(String content, OutputStream output, String imgType, int size) {  
 86         try {  
 87             BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);  
 88             // 生成二维码QRCode图片  
 89             ImageIO.write(bufImg, imgType, output);  
 90         } catch (Exception e) {  
 91             e.printStackTrace();  
 92         }  
 93     }  
 94       
 95     /** 
 96      * 生成二维码(QRCode)图片的公共方法 
 97      * @param content 存储内容 
 98      * @param imgType 图片类型 
 99      * @param size 二维码尺寸 
100      * @return 
101      */  
102     private BufferedImage qRCodeCommon(String content, String imgType, int size) {  
103         BufferedImage bufImg = null;  
104         try {  
105             Qrcode qrcodeHandler = new Qrcode();    
106             // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小  
107             qrcodeHandler.setQrcodeErrorCorrect('M');  
108             qrcodeHandler.setQrcodeEncodeMode('B');  
109             // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大  
110             qrcodeHandler.setQrcodeVersion(size);  
111             // 获得内容的字节数组,设置编码格式  
112             byte[] contentBytes = content.getBytes("utf-8");  
113             // 图片尺寸  
114             int imgSize = 67 + 12 * (size - 1);  
115             bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);  
116             Graphics2D gs = bufImg.createGraphics();  
117             // 设置背景颜色  
118             gs.setBackground(Color.WHITE);  
119             gs.clearRect(0, 0, imgSize, imgSize);  
120   
121             // 设定图像颜色> BLACK  
122             gs.setColor(Color.BLACK);  
123             // 设置偏移量,不设置可能导致解析出错  
124             int pixoff = 2;  
125             // 输出内容> 二维码  
126             if (contentBytes.length > 0 && contentBytes.length < 800) {  
127                 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);  
128                 for (int i = 0; i < codeOut.length; i++) {  
129                     for (int j = 0; j < codeOut.length; j++) {  
130                         if (codeOut[j][i]) {  
131                             gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);  
132                         }  
133                     }  
134                 }  
135             } else {  
136                 throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");  
137             }  
138             gs.dispose();  
139             bufImg.flush();  
140         } catch (Exception e) {  
141             e.printStackTrace();  
142         }  
143         return bufImg;  
144     }  
145       
146     /** 
147      * 解析二维码(QRCode) 
148      * @param imgPath 图片路径 
149      * @return 
150      */  
151     public String decoderQRCode(String imgPath) {  
152         // QRCode 二维码图片的文件  
153         File imageFile = new File(imgPath);  
154         BufferedImage bufImg = null;  
155         String content = null;  
156         try {  
157             bufImg = ImageIO.read(imageFile);  
158             QRCodeDecoder decoder = new QRCodeDecoder();  
159             content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8");   
160         } catch (IOException e) {  
161             System.out.println("Error: " + e.getMessage());  
162             e.printStackTrace();  
163         } catch (DecodingFailedException dfe) {  
164             System.out.println("Error: " + dfe.getMessage());  
165             dfe.printStackTrace();  
166         }  
167         return content;  
168     }  
169       
170     /** 
171      * 解析二维码(QRCode) 
172      * @param input 输入流 
173      * @return 
174      */  
175     public String decoderQRCode(InputStream input) {  
176         BufferedImage bufImg = null;  
177         String content = null;  
178         try {  
179             bufImg = ImageIO.read(input);  
180             QRCodeDecoder decoder = new QRCodeDecoder();  
181             content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8");   
182         } catch (IOException e) {  
183             System.out.println("Error: " + e.getMessage());  
184             e.printStackTrace();  
185         } catch (DecodingFailedException dfe) {  
186             System.out.println("Error: " + dfe.getMessage());  
187             dfe.printStackTrace();  
188         }  
189         return content;  
190     }  
191   
192     public static void main(String[] args) {  
193         String imgPath = "C:/zcpr.bmp";   //生成二维码图片存放的地址和名称
194         //String encoderContent = "Hello 大大、小小,welcome to QRCode!" + "\nMyblog [ http://sjsky.iteye.com ]" + "\nEMail [ sjsky007@gmail.com ]";  
195         String encoderContent = "http://www.baidu.com";  //二维码显示的内容
196         
197         TwoDimensionCode handler = new TwoDimensionCode();  
198         handler.encoderQRCode(encoderContent, imgPath, "png");  
199       try {  
200           OutputStream output = new FileOutputStream(imgPath);  
201           handler.encoderQRCode(encoderContent, output);  
202       } catch (Exception e) {  
203           e.printStackTrace();  
204       }  
205         System.out.println("========encoder success");  
206           
207           
208         String decoderContent = handler.decoderQRCode(imgPath);  
209         System.out.println("解析结果如下:");  
210         System.out.println(decoderContent);  
211         System.out.println("========decoder success!!!");  
212     }  
213 }  

 

posted @ 2017-07-18 15:48  NAYNEHC  阅读(915)  评论(1编辑  收藏  举报