595231ea52193484ade0446955b7440068694af0.svn-base 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package UIB.COM;
  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.UnsupportedEncodingException;
  8. import javax.imageio.ImageIO;
  9. import jp.sourceforge.qrcode.QRCodeDecoder;
  10. import jp.sourceforge.qrcode.data.QRCodeImage;
  11. import jp.sourceforge.qrcode.exception.DecodingFailedException;
  12. import com.swetake.util.Qrcode;
  13. public class QRCodeHandler {
  14. public static void main(String[] args) throws Exception {
  15. StringBuffer context =new StringBuffer();
  16. context.append("制造商:新余钢铁股份有限公司\n");
  17. context.append("牌号:").append("50").append("\n");
  18. context.append("标准号:").append("GB/T710-2008 GB/T699-1999").append("\n");
  19. context.append("交货状态:").append("AR").append("\n");
  20. context.append("发货日期:").append("2015-09-19").append("\n");
  21. context.append("合同号:").append("302015080042").append("\n");
  22. context.append("证明书编号:").append("XR5-042382").append("\n");
  23. QRCodeHandler.QRCodeEncoder(context.toString(),"D:\\Program Files\\Tomcat5.5.23\\webapps\\CoreFS\\WEB-INF\\excelConfig\\qr.png");
  24. }
  25. /**
  26. * 生成二维码
  27. * @throws Exception
  28. */
  29. public static void QRCodeEncoder(String context,String path) throws Exception {
  30. Qrcode qrcode = new Qrcode();
  31. qrcode.setQrcodeErrorCorrect('M');
  32. qrcode.setQrcodeEncodeMode('B');
  33. qrcode.setQrcodeVersion(10);
  34. byte[] d = context.getBytes("gb2312");
  35. BufferedImage bi = new BufferedImage(180, 180,BufferedImage.TYPE_INT_RGB);
  36. // createGraphics
  37. Graphics2D g = bi.createGraphics();
  38. // set background
  39. g.setBackground(Color.WHITE);
  40. g.clearRect(0, 0, 180, 180);
  41. g.setColor(Color.BLACK);
  42. // 设置偏移量 不设置可能导致解析出错
  43. int pixoff = 2;
  44. // 输出内容> 二维码
  45. //System.out.println(d.length);
  46. if (d.length > 0 && d.length < 214) {
  47. boolean[][] codeOut = qrcode.calQrcode(d);
  48. for (int i = 0; i < codeOut.length; i++) {
  49. for (int j = 0; j < codeOut.length; j++) {
  50. if (codeOut[j][i]) {
  51. g.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
  52. }
  53. }
  54. }
  55. } else {
  56. throw new Exception("内容过长,生成二维码失败.请与技术中心联系.");
  57. // System.err.println("QRCode content bytes length = "
  58. // + d.length + " not in [ 0,214 ]. ");
  59. }
  60. g.dispose();
  61. bi.flush();
  62. File f = new File(path);
  63. ImageIO.write(bi, "png", f);
  64. }
  65. /**
  66. * 解析二维码
  67. * @throws Exception
  68. */
  69. public static void QRCodeDecoderTest() {
  70. QRCodeDecoder decoder = new QRCodeDecoder();
  71. File imageFile = new File("TestQRCode.png");
  72. BufferedImage image = null;
  73. try {
  74. image = ImageIO.read(imageFile);
  75. } catch (IOException e) {
  76. System.out.println("Error: " + e.getMessage());
  77. }
  78. try {
  79. String decodedData = new String(
  80. decoder.decode(new J2SEImage(image)), "GBK");
  81. System.out.println(decodedData);
  82. } catch (DecodingFailedException dfe) {
  83. System.out.println("Error: " + dfe.getMessage());
  84. } catch (UnsupportedEncodingException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. class J2SEImage implements QRCodeImage {
  90. BufferedImage image;
  91. public J2SEImage(BufferedImage image) {
  92. this.image = image;
  93. }
  94. public int getWidth() {
  95. return image.getWidth();
  96. }
  97. public int getHeight() {
  98. return image.getHeight();
  99. }
  100. public int getPixel(int x, int y) {
  101. return image.getRGB(x, y);
  102. }
  103. }