二维码生成器

一、介绍:本文为二维码不在服务器生成图片直接输出在客户端;访问方式可以为img标签中src为“aa/getQCode?text=”;

二、二维码生成器分为两种,日本的qCord导入谷歌zxing的jar包;其中zxing的maven依赖是:

  1.  <dependency>  
  2.             <groupId>com.google.zxing</groupId>  
  3.             <artifactId>core</artifactId>  
  4.             <version>3.0.0</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>com.google.zxing</groupId>  
  8.             <artifactId>javase</artifactId>  
  9.             <version>3.0.0</version>     
  10.         </dependency> 

三、代码部分:

[java]   view plain  copy
  1.         @RequestMapping(value = "getQRCoder")  
  2.     @ResponseBody  
  3.     public void getQRCoder(HttpServletResponse response,String text){  
  4.         //设置页面不缓存  
  5.         response.setHeader("Pragma","No-cache");  
  6.         response.setHeader("Cache-Control","no-cache");  
  7.         response.setDateHeader("Expires"0);  
  8.   
  9.         int width2 = 233;   
  10.         int height2 = 233;   
  11.         //二维码的图片格式  
  12.         Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();   
  13.         //内容所使用编码   
  14.         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   
  15.         try {  
  16.             BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, width2, height2, hints);  
  17.             bitMatrix = deleteWhite(bitMatrix);  
  18.             int width = bitMatrix.getWidth();   
  19.             int height = bitMatrix.getHeight();   
  20.             BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   
  21.             for (int x = 0; x < width; x++) {   
  22.                 for (int y = 0; y < height; y++) {   
  23.                     image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); //二维码图片为黑白两色  
  24.                 }   
  25.             }  
  26.             ImageIO.write(image,"gif",response.getOutputStream());  
  27.             //生成二维码   
  28.             //File outputFile = new File("d:"+File.separator+"new.gif");   
  29.             //MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);  
  30.         } catch (Exception e) {  
  31.             // TODO Auto-generated catch block  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35.       
  36.     private static BitMatrix deleteWhite(BitMatrix matrix) {  
  37.         int[] rec = matrix.getEnclosingRectangle();  
  38.         int resWidth = rec[2] + 1;  
  39.         int resHeight = rec[3] + 1;  
  40.   
  41.         BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);  
  42.         resMatrix.clear();  
  43.         for (int i = 0; i < resWidth; i++) {  
  44.             for (int j = 0; j < resHeight; j++) {  
  45.                 if (matrix.get(i + rec[0], j + rec[1]))  
  46.                     resMatrix.set(i, j);  
  47.             }  
  48.         }  
  49.         return resMatrix;  
  50.     } 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值