7387da43f681506d76ba405640d9d058c5cf2ee1.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package cn.com.goldenwater.dcproj.utils;
  2. import com.google.zxing.BarcodeFormat;
  3. import com.google.zxing.EncodeHintType;
  4. import com.google.zxing.MultiFormatWriter;
  5. import com.google.zxing.common.BitMatrix;
  6. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  7. import javax.imageio.ImageIO;
  8. import java.awt.*;
  9. import java.awt.geom.RoundRectangle2D;
  10. import java.awt.image.BufferedImage;
  11. import java.io.File;
  12. import java.util.Hashtable;
  13. /**
  14. * 二维码工具类
  15. */
  16. public class QRCodeUtil {
  17. private static final String CHARSET = "utf-8";
  18. private static final String FORMAT_NAME = "JPG";
  19. // 二维码尺寸
  20. private static final int QRCODE_SIZE = 300;
  21. // LOGO宽度
  22. private static final int WIDTH = 60;
  23. // LOGO高度
  24. private static final int HEIGHT = 60;
  25. private static BufferedImage createImage(String content, String imgPath,
  26. boolean needCompress) throws Exception {
  27. Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  28. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  29. hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
  30. hints.put(EncodeHintType.MARGIN, 1);
  31. BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
  32. BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
  33. int width = bitMatrix.getWidth();
  34. int height = bitMatrix.getHeight();
  35. BufferedImage image = new BufferedImage(width, height,
  36. BufferedImage.TYPE_INT_RGB);
  37. for (int x = 0; x < width; x++) {
  38. for (int y = 0; y < height; y++) {
  39. image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
  40. : 0xFFFFFFFF);
  41. }
  42. }
  43. if (imgPath == null || "".equals(imgPath)) {
  44. return image;
  45. }
  46. // 插入图片
  47. QRCodeUtil.insertImage(image, imgPath, needCompress);
  48. return image;
  49. }
  50. /**
  51. * 插入LOGO
  52. *
  53. * @param source 二维码图片
  54. * @param imgPath LOGO图片地址
  55. * @param needCompress 是否压缩
  56. * @throws Exception
  57. */
  58. private static void insertImage(BufferedImage source, String imgPath,
  59. boolean needCompress) throws Exception {
  60. File file = new File(imgPath);
  61. if (!file.exists()) {
  62. System.err.println("" + imgPath + " 该文件不存在!");
  63. return;
  64. }
  65. Image src = ImageIO.read(new File(imgPath));
  66. int width = src.getWidth(null);
  67. int height = src.getHeight(null);
  68. if (needCompress) { // 压缩LOGO
  69. if (width > WIDTH) {
  70. width = WIDTH;
  71. }
  72. if (height > HEIGHT) {
  73. height = HEIGHT;
  74. }
  75. Image image = src.getScaledInstance(width, height,
  76. Image.SCALE_SMOOTH);
  77. BufferedImage tag = new BufferedImage(width, height,
  78. BufferedImage.TYPE_INT_RGB);
  79. Graphics g = tag.getGraphics();
  80. g.drawImage(image, 0, 0, null); // 绘制缩小后的图
  81. g.dispose();
  82. src = image;
  83. }
  84. // 插入LOGO
  85. Graphics2D graph = source.createGraphics();
  86. int x = (QRCODE_SIZE - width) / 2;
  87. int y = (QRCODE_SIZE - height) / 2;
  88. graph.drawImage(src, x, y, width, height, null);
  89. Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
  90. graph.setStroke(new BasicStroke(3f));
  91. graph.draw(shape);
  92. graph.dispose();
  93. }
  94. /**
  95. * 生成二维码(内嵌LOGO)
  96. *
  97. * @param content 内容
  98. * @param imgPath LOGO地址
  99. * @param destPath 存放目录
  100. * @param fileName 文件名称
  101. * @param needCompress 是否压缩LOGO
  102. * @throws Exception
  103. */
  104. public static void encode(String content, String imgPath, String destPath, String fileName,
  105. boolean needCompress) throws Exception {
  106. BufferedImage image = QRCodeUtil.createImage(content, imgPath,
  107. needCompress);
  108. mkdirs(destPath);
  109. String file = fileName + ".jpg";
  110. ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));
  111. }
  112. /**
  113. * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
  114. *
  115. * @param destPath 存放目录
  116. * @author lanyuan
  117. * Email: mmm333zzz520@163.com
  118. * @date 2013-12-11 上午10:16:36
  119. */
  120. public static void mkdirs(String destPath) {
  121. File file = new File(destPath);
  122. //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
  123. if (!file.exists() && !file.isDirectory()) {
  124. file.mkdirs();
  125. }
  126. }
  127. /**
  128. * 生成二维码
  129. *
  130. * @param content 内容
  131. * @param destPath 存储地址
  132. * @param destPath 存储地址
  133. * @param fileName 文件名称
  134. * @throws Exception
  135. */
  136. public static void encodeQR(String content, String imgPath, String destPath, String fileName,
  137. boolean needCompress) throws Exception {
  138. QRCodeUtil.encode(content, imgPath, destPath, fileName, needCompress);
  139. }
  140. }