使用日本人的库QRCode编解码二维码

使用日本人的库QRCode编解码二维码

慕课网讲解

http://www.imooc.com/learn/531




首先导入其jar包。

1.首先建立一个实现QRCodeImage的类

  1. /**
  2. * @FileName: QRCodeImageBean.java
  3. * @Author
  4. * @Description:
  5. * @Date 2016年7月1日 下午11:05:10
  6. * @CopyRight ZTE Corporation
  7. */
  8. package com.xsy.qrcode.jpqrcode.bean;
  9. import java.awt.image.BufferedImage;
  10. import jp.sourceforge.qrcode.data.QRCodeImage;
  11. public class QRCodeImageBean implements QRCodeImage{
  12. BufferedImage bufImg;
  13. public QRCodeImageBean(BufferedImage bufImg){
  14. this.bufImg = bufImg;
  15. }
  16. public int getHeight(){
  17. return bufImg.getHeight();
  18. }
  19. public int getPixel(int x, int y){
  20. return bufImg.getRGB(x, y);
  21. }
  22. public int getWidth(){
  23. return bufImg.getWidth();
  24. }
  25. }

2.在建立一个编解码的工具类

  1. package com.xsy.qrcode.jpqrcode;
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import javax.imageio.ImageIO;
  8. import com.swetake.util.Qrcode;
  9. import com.xsy.qrcode.jpqrcode.bean.QRCodeImageBean;
  10. import jp.sourceforge.qrcode.QRCodeDecoder;
  11. import jp.sourceforge.qrcode.exception.DecodingFailedException;
  12. public class QRCode{
  13. /**
  14. * 解析二维码(QRCode)
  15. *
  16. * @param imgPath
  17. * @return
  18. */
  19. public static String decoderQRCode(String imgPath){
  20. // QRCode 二维码图片的文件
  21. File imageFile = new File(imgPath);
  22. BufferedImage bufImg = null;
  23. String content = null;
  24. try{
  25. bufImg = ImageIO.read(imageFile);
  26. QRCodeDecoder decoder = new QRCodeDecoder();
  27. content = new String(decoder.decode(new QRCodeImageBean(bufImg)), "utf-8");
  28. }
  29. catch(IOException e){
  30. System.out.println("Error: " + e.getMessage());
  31. e.printStackTrace();
  32. }
  33. catch(DecodingFailedException dfe){
  34. System.out.println("Error: " + dfe.getMessage());
  35. dfe.printStackTrace();
  36. }
  37. return content;
  38. }
  39. /**
  40. * 生成二维码(QRCode)图片
  41. *
  42. * @param content
  43. * 存储内容
  44. * @param imgPath
  45. * 图片路径
  46. * @param imgType
  47. * 图片类型
  48. */
  49. public static void encoderQRCode(String content, String imgPath, String imgType){
  50. encoderQRCode(content, imgPath, imgType, 7);
  51. }
  52. /**
  53. * 生成二维码(QRCode)图片
  54. *
  55. * @param content
  56. * 存储内容
  57. * @param imgPath
  58. * 图片路径
  59. * @param imgType
  60. * 图片类型
  61. * @param size
  62. * 二维码尺寸
  63. */
  64. public static void encoderQRCode(String content, String imgPath, String imgType, int size){
  65. try{
  66. BufferedImage bufImg = qRCodeCommon(content, imgType, size);
  67. File imgFile = new File(imgPath);
  68. // 生成二维码QRCode图片
  69. ImageIO.write(bufImg, imgType, imgFile);
  70. }
  71. catch(Exception e){
  72. e.printStackTrace();
  73. }
  74. }
  75. /**
  76. * 生成二维码(QRCode)图片的公共方法
  77. *
  78. * @param content
  79. * 存储内容
  80. * @param imgType
  81. * 图片类型
  82. * @param size
  83. * 二维码尺寸
  84. * @return
  85. */
  86. private static BufferedImage qRCodeCommon(String content, String imgType, int size){
  87. BufferedImage bufImg = null;
  88. try{
  89. Qrcode qrcodeHandler = new Qrcode();
  90. // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
  91. qrcodeHandler.setQrcodeErrorCorrect('M');
  92. qrcodeHandler.setQrcodeEncodeMode('B');
  93. // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
  94. qrcodeHandler.setQrcodeVersion(size);
  95. // 获得内容的字节数组,设置编码格式
  96. byte[] contentBytes = content.getBytes("utf-8");
  97. // 图片尺寸
  98. int imgSize = 67 + 12 * (size - 1);
  99. bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
  100. Graphics2D gs = bufImg.createGraphics();
  101. // 设置背景颜色
  102. gs.setBackground(Color.WHITE);
  103. gs.clearRect(0, 0, imgSize, imgSize);
  104. // 设定图像颜色> BLACK
  105. gs.setColor(Color.BLACK);
  106. // 设置偏移量,不设置可能导致解析出错
  107. int pixoff = 2;
  108. // 输出内容> 二维码
  109. if(contentBytes.length > 0 && contentBytes.length < 800){
  110. boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
  111. for(int i = 0; i < codeOut.length; i++){
  112. for(int j = 0; j < codeOut.length; j++){
  113. if(codeOut[j][i]){
  114. gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
  115. }
  116. }
  117. }
  118. } else{
  119. throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
  120. }
  121. gs.dispose();
  122. bufImg.flush();
  123. }
  124. catch(Exception e){
  125. e.printStackTrace();
  126. }
  127. return bufImg;
  128. }
  129. }

3.测试

  1. /**
  2. * @FileName: QRCodeTest.java
  3. * @Author
  4. * @Description:
  5. * @Date 2016年7月1日 下午11:03:24
  6. * @CopyRight ZTE Corporation
  7. */
  8. package com.xsy.qrcode.jpqrcode;
  9. import org.junit.Test;
  10. public class QRCodeTest{
  11. @Test
  12. public void testEncodeQR(){
  13. String imgPath = "d:/xsy/Test.png";
  14. String encoderContent = "这是我的电话号码:15208384257";
  15. QRCode.encoderQRCode(encoderContent, imgPath, "png");
  16. System.out.println("encoder success!!!");
  17. }
  18. @Test
  19. public void testDecodeQR(){
  20. String imgPath = "d:/xsy/46647064.jpg";
  21. String qrCon = QRCode.decoderQRCode(imgPath);
  22. System.out.println("decoder success!!!");
  23. System.out.println("二维码内容为:" + qrCon);
  24. }
  25. }


测试通过的,可以放心使用


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值