TwoDimensionCode.java 6.8 KB

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