歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java實現二維碼QRCode的編碼和解碼

Java實現二維碼QRCode的編碼和解碼

日期:2017/3/1 10:12:30   编辑:Linux編程

試用下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.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/8月/16日/Java實現二維碼QRCode的編碼和解碼

後來發現一個更好的條形碼和二維碼的開源軟件(ZXing),詳細介紹見:http://www.linuxidc.com/Linux/2012-08/68359.htm

【一】、編碼:

QRCodeEncoderHandler.java

添加如下代碼:

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

運行後生成的二維碼圖片如下:

此時就可用手機的二維碼掃描軟件(本人用的:android 快拍二維碼 )來測試下,識別成功的截圖如下:

喜歡的朋友可以下載後試一試,做一些名片或者自己喜歡的東西。當然Java也可以對二維碼圖片解碼,具體看下面關於解碼的內容。

Copyright © Linux教程網 All Rights Reserved