QR二维码Java开发笔记

QR二维码是目前最常用二维码

是把字符串编码后通过二维图片的黑白两色模块显示出来

可表示的字符串长度和 容错率(ECC) 显示编码模式(EncodeMode)及版本(Version)有关

 

 

  

 

容错率共四档:

L     7%

M    15%

Q     25%

H     30%

 

 

编码模式:

Numeric             数字

Alphanumeric        英文字母

Binary              二进制

Kanji             汉字

 

版本(Version):

1-40 共40个版本

1       21x21模块

40     177x177模块

每增加一个版本每边增加4个模块 ,如: 版本2 为25x25模块

以下两张图片是字符串'www.sohu.com'的二维码使用了不同的版本的效果

 

 版本1   版本7

 

 

 

 

每个版本不同容错率可表示长度可在这里查看http://www.qrcode.com/en/vertable1.html

 

  

 

通过开源Java程序生成二维码

 

ZXing

 

 

Java代码   收藏代码
  1. /** 
  2.      * @param contents 字符串内容 
  3.      * @param width    宽度 
  4.      * @param height   高度 
  5.      * @param imgPath  图片输出路径 
  6.      */  
  7.     public void encode(String contents, int width, int height, String imgPath) {    
  8.         Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();    
  9.         // 指定纠错等级    
  10.         hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);    
  11.         // 指定编码格式    
  12.         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");    
  13.         try {    
  14.             BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,    
  15.                     BarcodeFormat.QR_CODE, width, height, hints);    
  16.             MatrixToImageWriter    
  17.                     .writeToFile(bitMatrix, "png"new File(imgPath));    
  18.         } catch (Exception e) {    
  19.             e.printStackTrace();    
  20.         }    
  21.     }  

 

 

我用的ZXing2.0,发现只要指定了字符集,出来的二维码图片就不能被识别,释掉这句就可以识别了,但中文会乱码。
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

 

 

SwetakeQRCode

Java代码   收藏代码
  1. /** 
  2.      * @param sms_info  信息内容 
  3.      * @param filePath  输出路径 
  4.      * @param width     宽度 
  5.      * @param height    高度 
  6.      * @throws Exception 
  7.      */  
  8.     public static void createImage(String sms_info,String filePath,int width,int height) throws Exception {  
  9.         try {  
  10.             Qrcode testQrcode = new Qrcode();  
  11.             testQrcode.setQrcodeErrorCorrect('M');  
  12.             testQrcode.setQrcodeEncodeMode('B');  
  13.             testQrcode.setQrcodeVersion(7);  
  14.             String testString = sms_info;  
  15.             byte[] d = testString.getBytes("UTF-8");  
  16.             System.out.println(d.length);  
  17.   
  18.             // 限制最大字节数为120  
  19.             if (d.length > 0 && d.length < 120) {  
  20.                 boolean[][] s = testQrcode.calQrcode(d);  
  21.                 BufferedImage bi = new BufferedImage(s.length, s[0].length,  
  22.                         BufferedImage.TYPE_BYTE_BINARY);  
  23.                 Graphics2D g = bi.createGraphics();  
  24.                 g.setBackground(Color.WHITE);  
  25.                 g.clearRect(00, s.length, s[0].length);  
  26.                 g.setColor(Color.BLACK);  
  27.                 int mulriple=1;  
  28.                 for (int i = 0; i < s.length; i++) {  
  29.                     for (int j = 0; j < s.length; j++) {  
  30.                         if (s[j][i]) {  
  31.                             g.fillRect(j * mulriple, i * mulriple, mulriple, mulriple);  
  32.                         }  
  33.                     }  
  34.                 }  
  35.                 g.dispose();  
  36.                 bi.flush();  
  37.                 File f = new File(filePath);  
  38.                 if (!f.exists()) {  
  39.                     f.createNewFile();  
  40.                 }  
  41.                 bi=resize(bi,width,height);  
  42.                 // 创建图片  
  43.                 ImageIO.write(bi, "png", f);  
  44.             }  
  45.         }  
  46.         catch (Exception e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.           
  50.     }  
  51.       
  52.      /**   
  53.      * 图像缩放   
  54.      * @param source   
  55.      * @param targetW   
  56.      * @param targetH   
  57.      * @return   
  58.      */  
  59.     private static BufferedImage resize(BufferedImage source, int targetW,  
  60.             int targetH) {  
  61.         int type = source.getType();  
  62.         BufferedImage target = null;  
  63.         double sx = (double) targetW / source.getWidth();  
  64.         double sy = (double) targetH / source.getHeight();  
  65.         target = new BufferedImage(targetW, targetH, type);  
  66.         Graphics2D g = target.createGraphics();  
  67.         g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));  
  68.         g.dispose();  
  69.         return target;  
  70.     }  

 

可以通过以下方法设置容错、编码模式、版本

setQrcodeErrorCorrect       L M Q H

 

setQrcodeEncodeMode      N数字 A英文 其他为Binary

 

setQrcodeVersion              不设置会自动选择适当的版本

 

这个库只是通过calQrcode方法返回表示二维码的数组,图像需要自己处理

boolean[][] s = testQrcode.calQrcode(d);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值