Java实现二维码QRCode的编码和解码


试用下Android手机的二维码扫描软件,扫描了下火车票、名片等等,觉得非常不错很有意思的。当然Java也可以实现这些,现在就分享下如何简单用Java实现二维码中QRCode的编码和解码(可以手机扫描验证)。

涉及到的一些主要类库,方便大家下载:

编码 lib:Qrcode_swetake.jar   (官网介绍 -- http://www.swetake.com/qr/index-e.html)
解码 lib:qrcode.jar            (官网介绍 -- http://sourceforge.jp/projects/qrcode/)

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2012年资料/8月/16日/Java实现二维码QRCode的编码和解码

后来发现一个更好的条形码和二维码的开源软件(ZXing),详细介绍见:

【一】、编码:

QRCodeEncoderHandler.java

添加如下代码:

  1. package michael.qrcode;   
  2.   
  3. import java.awt.Color;   
  4. import java.awt.Graphics2D;   
  5. import java.awt.image.BufferedImage;   
  6. import java.io.File;   
  7.   
  8. import javax.imageio.ImageIO;   
  9.   
  10. import com.swetake.util.Qrcode;   
  11.   
  12. /**  
  13.  * 二维码生成器  
  14.  * @blog http://www.bkjia.com  
  15.  * @author Michael  
  16.  */  
  17. public class QRCodeEncoderHandler {   
  18.   
  19.     /**  
  20.      * 生成二维码(QRCode)图片  
  21.      * @param content  
  22.      * @param imgPath  
  23.      */  
  24.     public void encoderQRCode(String content, String imgPath) {   
  25.         try {   
  26.   
  27.             Qrcode qrcodeHandler = new Qrcode();   
  28.             qrcodeHandler.setQrcodeErrorCorrect('M');   
  29.             qrcodeHandler.setQrcodeEncodeMode('B');   
  30.             qrcodeHandler.setQrcodeVersion(7);   
  31.   
  32.             System.out.println(content);   
  33.             byte[] contentBytes = content.getBytes("gb2312");   
  34.   
  35.             BufferedImage bufImg = new BufferedImage(140140,   
  36.                     BufferedImage.TYPE_INT_RGB);   
  37.   
  38.             Graphics2D gs = bufImg.createGraphics();   
  39.   
  40.             gs.setBackground(Color.WHITE);   
  41.             gs.clearRect(00140140);   
  42.   
  43.             // 设定图像颜色 > BLACK   
  44.             gs.setColor(Color.BLACK);   
  45.   
  46.             // 设置偏移量 不设置可能导致解析出错   
  47.             int pixoff = 2;   
  48.             // 输出内容 > 二维码   
  49.             if (contentBytes.length > 0 && contentBytes.length < 120) {   
  50.                 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);   
  51.                 for (int i = 0; i < codeOut.length; i++) {   
  52.                     for (int j = 0; j < codeOut.length; j++) {   
  53.                         if (codeOut[j][i]) {   
  54.                             gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 33);   
  55.                         }   
  56.                     }   
  57.                 }   
  58.             } else {   
  59.                 System.err.println("QRCode content bytes length = "  
  60.                         + contentBytes.length + " not in [ 0,120 ]. ");   
  61.             }   
  62.   
  63.             gs.dispose();   
  64.             bufImg.flush();   
  65.   
  66.             File imgFile = new File(imgPath);   
  67.   
  68.             // 生成二维码QRCode图片   
  69.             ImageIO.write(bufImg, "png", imgFile);   
  70.   
  71.         } catch (Exception e) {   
  72.             e.printStackTrace();   
  73.         }   
  74.   
  75.     }   
  76.   
  77.     /**  
  78.      * @param args the command line arguments  
  79.      */  
  80.     public static void main(String[] args) {   
  81.         String imgPath = "D:/test/twocode/Michael_QRCode.png";   
  82.   
  83.         String content = "Hello 大大、小小,welcome to QRCode!"  
  84.                 + "\nMyblog [ http://www.bkjia.com ]"  
  85.                 + "\nEMail [ sjsky007@gmail.com ]" + "\nTwitter [ @suncto ]";   
  86.   
  87.         QRCodeEncoderHandler handler = new QRCodeEncoderHandler();   
  88.         handler.encoderQRCode(content, imgPath);   
  89.   
  90.         System.out.println("encoder QRcode success");   
  91.     }   
  92. }  

运行后生成的二维码图片如下:

此时就可用手机的二维码扫描软件(本人用的:android 快拍二维码 )来测试下,识别成功的截图如下:

喜欢的朋友可以下载后试一试,做一些名片或者自己喜欢的东西。当然Java也可以对二维码图片解码,具体看下面关于解码的内容。

  • 1
  • 2
  • 下一页

相关内容