Java实现二维码QRCode的编码和解码

Java实现二维码QRCode的编码和解码

编码:

 

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

 

运行后生成的二维码图片如下:

 

解码:

 

[java] view plaincopyprint?
  1. QRCodeDecoderHandler.java
  2. Java代码
  3. package michael.qrcode;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import javax.imageio.ImageIO;
  8. import jp.sourceforge.qrcode.QRCodeDecoder;
  9. import jp.sourceforge.qrcode.data.QRCodeImage;
  10. import jp.sourceforge.qrcode.exception.DecodingFailedException;
  11. /**
  12. * @blog http://sjsky.iteye.com
  13. * @author Michael
  14. */
  15. publicclass QRCodeDecoderHandler {
  16. /**
  17. * 解码二维码
  18. * @param imgPath
  19. * @return String
  20. */
  21. public String decoderQRCode(String imgPath) {
  22. // QRCode 二维码图片的文件
  23. File imageFile = new File(imgPath);
  24. BufferedImage bufImg = null;
  25. String decodedData = null;
  26. try {
  27. bufImg = ImageIO.read(imageFile);
  28. QRCodeDecoder decoder = new QRCodeDecoder();
  29. decodedData = new String(decoder.decode(new J2SEImage(bufImg)));
  30. // try {
  31. // System.out.println(new String(decodedData.getBytes("gb2312"),
  32. // "gb2312"));
  33. // } catch (Exception e) {
  34. // // TODO: handle exception
  35. // }
  36. } catch (IOException e) {
  37. System.out.println("Error: " + e.getMessage());
  38. e.printStackTrace();
  39. } catch (DecodingFailedException dfe) {
  40. System.out.println("Error: " + dfe.getMessage());
  41. dfe.printStackTrace();
  42. }
  43. return decodedData;
  44. }
  45. /**
  46. * @param args the command line arguments
  47. */
  48. publicstaticvoid main(String[] args) {
  49. QRCodeDecoderHandler handler = new QRCodeDecoderHandler();
  50. String imgPath = "d:/test/twocode/Michael_QRCode.png";
  51. String decoderContent = handler.decoderQRCode(imgPath);
  52. System.out.println("解析结果如下:");
  53. System.out.println(decoderContent);
  54. System.out.println("========decoder success!!!");
  55. }
  56. class J2SEImage implements QRCodeImage {
  57. BufferedImage bufImg;
  58. public J2SEImage(BufferedImage bufImg) {
  59. this.bufImg = bufImg;
  60. }
  61. publicint getWidth() {
  62. return bufImg.getWidth();
  63. }
  64. publicint getHeight() {
  65. return bufImg.getHeight();
  66. }
  67. publicint getPixel(int x, int y) {
  68. return bufImg.getRGB(x, y);
  69. }
  70. }
  71. }

运行结果如下(解码出的内容和之前输入的内容一致 ):

解析结果如下:

Hello 大大、小小,welcome to QRCode!

Myblog [ www.cdtarena.com ]

EMail [ sjsk4554@gmail.com ]

Twitter [ @suncto ]

========decoder success!!!

posted @ 2013-03-14 15:48  代码单点  阅读(495)  评论(0编辑  收藏  举报