java的二维码的生成和解析_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java的二维码的生成和解析

java的二维码的生成和解析

 2017/9/26 12:43:21  stuming_zeng  程序员俱乐部  我要评论(0)
  • 摘要:一,本文目的:为了研究对支付宝和微信支付的统一路口管理(一张二维码同时支持微信和支付宝扫码支付);本文不将对支付宝和微信的支付开发进行讲解,主要是二维码的生成和解析;步骤:1,准备jar包,在java中用到了两个jar包;一个用于生成二维码,一个用户解析二维码;a,生成二维码jar包路径:http://www.swetake.com/qr/java/qr_java.html(注意:下载qrcode_java0.50beta10.tar.gz,解压后在lib目录下的qrcode
  • 标签:Java 解析 二维码

一,本文目的:?
?? 为了研究支付宝和微信支付的统一路口管理(一张二维码同时支持微信和支付宝扫码支付);本文不将对支付宝和微信的支付开发进行讲解,主要是二维码的生成和解析;?
步骤:?
1,准备jar包,在java中用到了两个jar包;一个用于生成二维码,一个用户解析二维码;?
? a,生成二维码jar包路径:http:// www.swetake.com/qr/java/qr_java.html(注意:下载qrcode_java0.50beta10.tar.gz,解压后在lib目录下的qrcode.jar即为用于生成二维码的库)?
? b,解析二维码jar包路径:http://sourceforge.jp/projects/qrcode/releases/(注意:解压qrcode包后lib目录下的qrcode.jar包,把其重命名,避免与生成二维码jar包冲突即可)?
2,java代码?
?? a,TwoDimensionCodeImage类:二维码图片对象?
??

的Java代码??收藏代码class="star" src="/Upload/Images/2017092612/40B102E0EF997EA6.png">
  1. ?com.ppfuns.aaa.entity;??
  2. ??
  3. import??jp.sourceforge.qrcode.data.QRCodeImage;??
  4. ??
  5. import??java.awt.image.BufferedImage;??
  6. ??
  7. / **?
  8. ?*创建IntelliJ IDEA。?
  9. ?*用户:jony?
  10. ?*日期:2017/9/26?
  11. ?*时间:9:53?
  12. ?*要更改此模板,请使用File |?设置|?文件模板?
  13. ?* /??
  14. public?class??TwoDimensionCodeImage???实现?QRCodeImage {???
  15. ??
  16. ??
  17. ??
  18. ??
  19. ????????BufferedImage bufImg;??
  20. ??
  21. ????????public??TwoDimensionCodeImage(BufferedImage bufImg){??
  22. ????????????这个.bufImg = bufImg;??
  23. ????????}??
  24. ??
  25. ????????@覆盖??
  26. ????????public?int??getHeight(){???
  27. ????????????return??bufImg.getHeight();??
  28. ????????}??
  29. ??
  30. ????????@覆盖??
  31. ????????public?int??getPixel(int??x,??int??y){???
  32. ????????????return??bufImg.getRGB(x,y);??
  33. ????????}??
  34. ??
  35. ????????@覆盖??
  36. ????????public?int??getWidth(){???
  37. ????????????return??bufImg.getWidth();??
  38. ????????}??
  39. ??
  40. }??
  41. ?????


?? B,CommonUtils类:二维码操作核心类?
?

的Java代码??收藏代码
  1. ?com.ppfuns.aaa.common;??
  2. ??
  3. import??com.ppfuns.aaa.entity.TwoDimensionCodeImage;??
  4. import??com.swetake.util.Qrcode;??
  5. import??jp.sourceforge.qrcode.QRCodeDecoder;??
  6. import??jp.sourceforge.qrcode.exception.DecodingFailedException;??
  7. ??
  8. import??javax.imageio.ImageIO;??
  9. import??java.awt。*;??
  10. import??java.awt.image.BufferedImage;??
  11. import??java.io.File;??
  12. import??java.io.IOException;??
  13. import??java.io.InputStream;??
  14. import??java.io.OutputStream;??
  15. ??
  16. / **?
  17. ?*创建IntelliJ IDEA。?
  18. ?*用户:简德群?
  19. ?*日期:2017/9/26?
  20. ?*时间:9:54?
  21. ?*要更改此模板,请使用File |?设置|?文件模板?
  22. ?* /??
  23. public?class??CommonUtils {???
  24. ????/ **?
  25. ?????*生成二维码(QRCode)图片?
  26. ?????* @param content存储内容?
  27. ?????* @param imgPath图片路径?
  28. ?????* /??
  29. ????public?static?void??encoderQRCode(String content,String imgPath){????
  30. ????????encoderQRCode(content,imgPath,??“png”?,??7?);??
  31. ????}??
  32. ??
  33. ????/ **?
  34. ?????*生成二维码(QRCode)图片?
  35. ?????* @param content存储内容?
  36. ?????* @param输出输出流?
  37. ?????* /??
  38. ????public?static?void??encoderQRCode(String content,OutputStream output){????
  39. ????????encoderQRCode(内容,输出,??“png”?,??7?);??
  40. ????}??
  41. ??
  42. ????/ **?
  43. ?????*生成二维码(QRCode)图片?
  44. ?????* @param content存储内容?
  45. ?????* @param imgPath图片路径?
  46. ?????* @param imgType图片类型?
  47. ?????* /??
  48. ????public?static?void??encoderQRCode(String content,String imgPath,String imgType){????
  49. ????????encoderQRCode(content,imgPath,imgType,??7?);??
  50. ????}??
  51. ??
  52. ????/ **?
  53. ?????*生成二维码(QRCode)图片?
  54. ?????* @param content存储内容?
  55. ?????* @param输出输出流?
  56. ?????* @param imgType图片类型?
  57. ?????* /??
  58. ????public?static?void??encoderQRCode(String content,OutputStream output,String imgType){????
  59. ????????encoderQRCode(content,output,imgType,??7?);??
  60. ????}??
  61. ??
  62. ????/ **?
  63. ?????*生成二维码(QRCode)图片?
  64. ?????* @param content存储内容?
  65. ?????* @param imgPath图片路径?
  66. ?????* @param imgType图片类型?
  67. ?????* @param size二维码尺寸?
  68. ?????* /??
  69. ????public?static?void??encoderQRCode(String content,String imgPath,String imgType,??int??size){????
  70. ????????尝试?{??
  71. ????????????BufferedImage bufImg = qRCodeCommon(content,imgType,size);??
  72. ??
  73. ????????????File imgFile =??new??File(imgPath);??
  74. ????????????//生成二维码QRCode图片??
  75. ????????????ImageIO.write(bufImg,imgType,imgFile);??
  76. ????????}??catch??(Exception e){??
  77. ????????????e.printStackTrace();??
  78. ????????}??
  79. ????}??
  80. ??
  81. ????/ **?
  82. ?????*生成二维码(QRCode)图片?
  83. ?????* @param content存储内容?
  84. ?????* @param输出输出流?
  85. ?????* @param imgType图片类型?
  86. ?????* @param size二维码尺寸?
  87. ?????* /??
  88. ????public?static?void??encoderQRCode(String content,OutputStream output,String imgType,??int??size){????
  89. ????????尝试?{??
  90. ????????????BufferedImage bufImg = qRCodeCommon(content,imgType,size);??
  91. ????????????//生成二维码QRCode图片??
  92. ????????????ImageIO.write(bufImg,imgType,output);??
  93. ????????}??catch??(Exception e){??
  94. ????????????e.printStackTrace();??
  95. ????????}??
  96. ????}??
  97. ??
  98. ????/ **?
  99. ?????*生成二维码(QRCode)图片的公共方法?
  100. ?????* @param content存储内容?
  101. ?????* @param imgType图片类型?
  102. ?????* @param size二维码尺寸?
  103. ?????* @返回?
  104. ?????* /??
  105. ????private?static??BufferedImage qRCodeCommon(String content,String imgType,??int??size){???
  106. ????????BufferedImage bufImg =??null?;??
  107. ????????尝试?{??
  108. ????????????Qrcode qrcodeHandler =??new??Qrcode();??
  109. ????????????//设置二维码排错率,可选L(7%),M(15%),Q(25%),H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小??
  110. ????????????qrcodeHandler.setQrcodeErrorCorrect('M'?);??
  111. ????????????qrcodeHandler.setQrcodeEncodeMode('B'?);??
  112. ????????????//设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大??
  113. ????????????qrcodeHandler.setQrcodeVersion(大小);??
  114. ????????????//获得内容的字节数组,设置编码格式??
  115. ????????????byte?[] contentBytes = content.getBytes(“utf-8”?);??
  116. ????????????//图片尺寸??
  117. ????????????int??imgSize =??67??+??12??*(size-??1?);??
  118. ????????????bufImg =??new??BufferedImage(imgSize,imgSize,BufferedImage.TYPE_INT_RGB);??
  119. ????????????Graphics2D gs = bufImg.createGraphics();??
  120. ????????????//设置背景颜色??
  121. ????????????gs.setBackground(Color.WHITE);??
  122. ????????????gs.clearRect(0?,??0?,imgSize,imgSize);??
  123. ??
  124. ????????????//设定图像颜色>黑色??
  125. ????????????gs.setColor(Color.BLACK);??
  126. ????????????//设置偏移量,不设置可能导致解析出错??
  127. ????????????int??pixoff =??2?;??
  128. ????????????//输出内容>二维码??
  129. ????????????if??(contentBytes.length>??0??&& contentBytes.length <??800?){??
  130. ????????????????boolean?[] [] codeOut = qrcodeHandler.calQrcode(contentBytes);??
  131. ????????????????for??(int??i =??0?; i <codeOut.length; i ++){??
  132. ????????????????????for??(int??j =??0?; j <codeOut.length; j ++){??
  133. ????????????????????????if??(codeOut [j] [i]){??
  134. ????????????????????????????gs.fillRect(j *??3??+ pixoff I *??3??+ pixoff,??3?,??3?);??
  135. ????????????????????????}??
  136. ????????????????????}??
  137. ????????????????}??
  138. ????????????}??else??{??
  139. ????????????????抛出?的异常“QRCode的内容的字节长度=”??+ contentBytes.length +??“不是在[0,800]。”?);???
  140. ????????????}??
  141. ????????????gs.dispose();??
  142. ????????????bufImg.flush();??
  143. ????????}??catch??(Exception e){??
  144. ????????????e.printStackTrace();??
  145. ????????}??
  146. ????????返回?bufImg;??
  147. ????}??
  148. ????/ **?
  149. ?????*解析二维码(QRCode)?
  150. ?????* @param imgPath图片路径?
  151. ?????* @返回?
  152. ?????* /??
  153. ????public?static??String decoderQRCode(String imgPath){???
  154. ????????// QRCode二维码图片的文件??
  155. ????????文件imageFile =??new??File(imgPath);??
  156. ????????BufferedImage bufImg =??null?;??
  157. ????????String content =??null?;??
  158. ????????尝试?{??
  159. ????????????bufImg = ImageIO.read(imageFile);??
  160. ????????????QRCodeDecoder decoder =??new??QRCodeDecoder();??
  161. ????????????content =??new??String(decode.decode(new??TwoDimensionCodeImage(bufImg)),??“utf-8”?);??
  162. ????????}??catch??(IOException e){??
  163. ????????????System.out.println(“Error:”??+ e.getMessage());??
  164. ????????????e.printStackTrace();??
  165. ????????}??catch??(DecodingFailedException dfe){??
  166. ????????????System.out.println(“Error:”??+ dfe.getMessage());??
  167. ????????????dfe.printStackTrace();??
  168. ????????}??
  169. ????????返回?内容;??
  170. ????}??
  171. ????/ **?
  172. ?????*解析二维码(QRCode)?
  173. ?????* @param input输入流?
  174. ?????* @返回?
  175. ?????* /??
  176. ????public??String decoderQRCode(InputStream input){??
  177. ????????BufferedImage bufImg =??null?;??
  178. ????????String content =??null?;??
  179. ????????尝试?{??
  180. ????????????bufImg = ImageIO.read(input);??
  181. ????????????QRCodeDecoder decoder =??new??QRCodeDecoder();??
  182. ????????????content =??new??String(decode.decode(new??TwoDimensionCodeImage(bufImg)),??“utf-8”?);??
  183. ????????}??catch??(IOException e){??
  184. ????????????System.out.println(“Error:”??+ e.getMessage());??
  185. ????????????e.printStackTrace();??
  186. ????????}??catch??(DecodingFailedException dfe){??
  187. ????????????System.out.println(“Error:”??+ dfe.getMessage());??
  188. ????????????dfe.printStackTrace();??
  189. ????????}??
  190. ????????返回?内容;??
  191. ????}??
  192. ??
  193. ????public?static?void??main(String [] args){????
  194. ????????String content =??“https://k.87.re/2014/07/150724ix4.jpg”?;??
  195. ????????//图片要求是已存在的二维码图片,这里的生成二维码只是对二维码图片中内容的更新??
  196. ????????encoderQRCode(content,“C:\\ Users \\ lenovo \\ Desktop \\ 121.jpg”?);??
  197. ????????String path =??“C:\\ Users \\ lenovo \\ Desktop \\ 121.jpg”?;??
  198. ????????String content1 = decodeQRCode(path);??
  199. ????????的System.out.println(内容1);??
  200. //??
  201. ????}??
  202. }??
  203. ??
  204. ? ??

?

发表评论
用户名: 匿名