java二维码生成与解析代码实现_第1页
java二维码生成与解析代码实现_第2页
java二维码生成与解析代码实现_第3页
java二维码生成与解析代码实现_第4页
java二维码生成与解析代码实现_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

java二维码生成与解析代码实现所需jar包:QRCode.jarTwoDimensionCode类:二维码操作核心类 javaview plaincopy1. packageqrcode;2. 3. importjava.awt.Color;4. importjava.awt.Graphics2D;5. importjava.awt.image.BufferedImage;6. importjava.io.File;7. importjava.io.IOException;8. importjava.io.InputStream;9. importjava.io.OutputStream;10. 11. importjavax.imageio.ImageIO;12. 13. importjp.sourceforge.qrcode.QRCodeDecoder;14. importjp.sourceforge.qrcode.exception.DecodingFailedException;15. 16. importcom.swetake.util.Qrcode;17. 18. publicclassTwoDimensionCode19. 20. /*21. *生成二维码(QRCode)图片22. *paramcontent存储内容23. *paramimgPath图片路径24. */25. publicvoidencoderQRCode(Stringcontent,StringimgPath)26. this.encoderQRCode(content,imgPath,png,7);27. 28. 29. /*30. *生成二维码(QRCode)图片31. *paramcontent存储内容32. *paramoutput输出流33. */34. publicvoidencoderQRCode(Stringcontent,OutputStreamoutput)35. this.encoderQRCode(content,output,png,7);36. 37. 38. /*39. *生成二维码(QRCode)图片40. *paramcontent存储内容41. *paramimgPath图片路径42. *paramimgType图片类型43. */44. publicvoidencoderQRCode(Stringcontent,StringimgPath,StringimgType)45. this.encoderQRCode(content,imgPath,imgType,7);46. 47. 48. /*49. *生成二维码(QRCode)图片50. *paramcontent存储内容51. *paramoutput输出流52. *paramimgType图片类型53. */54. publicvoidencoderQRCode(Stringcontent,OutputStreamoutput,StringimgType)55. this.encoderQRCode(content,output,imgType,7);56. 57. 58. /*59. *生成二维码(QRCode)图片60. *paramcontent存储内容61. *paramimgPath图片路径62. *paramimgType图片类型63. *paramsize二维码尺寸64. */65. publicvoidencoderQRCode(Stringcontent,StringimgPath,StringimgType,intsize)66. try67. BufferedImagebufImg=this.qRCodeCommon(content,imgType,size);68. 69. FileimgFile=newFile(imgPath);70. /生成二维码QRCode图片71. ImageIO.write(bufImg,imgType,imgFile);72. catch(Exceptione)73. e.printStackTrace();74. 75. 76. 77. /*78. *生成二维码(QRCode)图片79. *paramcontent存储内容80. *paramoutput输出流81. *paramimgType图片类型82. *paramsize二维码尺寸83. */84. publicvoidencoderQRCode(Stringcontent,OutputStreamoutput,StringimgType,intsize)85. try86. BufferedImagebufImg=this.qRCodeCommon(content,imgType,size);87. /生成二维码QRCode图片88. ImageIO.write(bufImg,imgType,output);89. catch(Exceptione)90. e.printStackTrace();91. 92. 93. 94. /*95. *生成二维码(QRCode)图片的公共方法96. *paramcontent存储内容97. *paramimgType图片类型98. *paramsize二维码尺寸99. *return100. */101. privateBufferedImageqRCodeCommon(Stringcontent,StringimgType,intsize)102. BufferedImagebufImg=null;103. try104. QrcodeqrcodeHandler=newQrcode();105. /设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小106. qrcodeHandler.setQrcodeErrorCorrect(M);107. qrcodeHandler.setQrcodeEncodeMode(B);108. /设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大109. qrcodeHandler.setQrcodeVersion(size);110. /获得内容的字节数组,设置编码格式111. bytecontentBytes=content.getBytes(utf-8);112. /图片尺寸113. intimgSize=67+12*(size-1);114. bufImg=newBufferedImage(imgSize,imgSize,BufferedImage.TYPE_INT_RGB);115. Graphics2Dgs=bufImg.createGraphics();116. /设置背景颜色117. gs.setBackground(Color.WHITE);118. gs.clearRect(0,0,imgSize,imgSize);119. 120. /设定图像颜色BLACK121. gs.setColor(Color.BLACK);122. /设置偏移量,不设置可能导致解析出错123. intpixoff=2;124. /输出内容二维码125. if(contentBytes.length0&contentBytes.length800)126. booleancodeOut=qrcodeHandler.calQrcode(contentBytes);127. for(inti=0;icodeOut.length;i+)128. for(intj=0;jcodeOut.length;j+)129. if(codeOutji)130. gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);131. 132. 133. 134. else135. thrownewException(QRCodecontentbyteslength=+contentBytes.length+notin0,800.);136. 137. gs.dispose();138. bufImg.flush();139. catch(Exceptione)140. e.printStackTrace();141. 142. returnbufImg;143. 144. 145. /*146. *解析二维码(QRCode)147. *paramimgPath图片路径148. *return149. */150. publicStringdecoderQRCode(StringimgPath)151. /QRCode二维码图片的文件152. FileimageFile=newFile(imgPath);153. BufferedImagebufImg=null;154. Stringcontent=null;155. try156. bufImg=ImageIO.read(imageFile);157. QRCodeDecoderdecoder=newQRCodeDecoder();158. content=newString(decoder.decode(newTwoDimensionCodeImage(bufImg),utf-8);159. catch(IOExceptione)160. System.out.println(Error:+e.getMessage();161. e.printStackTrace();162. catch(DecodingFailedExceptiondfe)163. System.out.println(Error:+dfe.getMessage();164. dfe.printStackTrace();165. 166. returncontent;167. 168. 169. /*170. *解析二维码(QRCode)171. *paraminput输入流172. *return173. */174. publicStringdecoderQRCode(InputStreaminput)175. BufferedImagebufImg=null;176. Stringcontent=null;177. try178. bufImg=ImageIO.read(input);179. QRCodeDecoderdecoder=newQRCodeDecoder();180. content=newString(decoder.decode(newTwoDimensionCodeImage(bufImg),utf-8);181. catch(IOExceptione)182. System.out.println(Error:+e.getMessage();183. e.printStackTrace();184. catch(DecodingFailedExceptiondfe)185. System.out.println(Error:+dfe.getMessage();186. dfe.printStackTrace();187. 188. returncontent;189. 190. 191. publicstaticvoidmain(Stringargs)192. StringimgPath=G:/TDDOWNLOAD/Michael_QRCode.png;193. StringencoderContent=Hello大大、小小,welcometoQRCode!+nMyblogaaa+nEMailbbb;194. TwoDimensionCodehandler=newTwoDimensionCode();195. handler.encoderQRCode(encoderContent,imgPath,png);196. /try197. /OutputStreamoutput=newFileOutputStream(imgPath);198. /handler.encoderQRCode(content,output);199. /catch(Exceptione)200. /e.printStackTrace();201. /202. System.out.println(=encodersuccess);203. StringdecoderContent=handler.decoderQRCode(imgPath);204. System.out.println(解析结果如下:);205. System.out.println(decoderContent);206. System.out.println(=decodersuccess!);207. 208. TwoDimensionCodeImage类:二维码图片对象javaview plaincopy1. packageqrcode;2. 3. importjava.awt.image.BufferedImage;4. 5. importjp.sourceforge.qrcode.data.QRCodeImage;6. 7. publicclassTwoDimensionCodeImageimplementsQRCodeImage8. 9. BufferedImagebufImg;10. 11. publicTwoDim

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论