ZxingCodeUtil.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package jnpf.util;
  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.image.BufferedImage;
  10. import java.io.File;
  11. import java.util.HashMap;
  12. import java.util.Hashtable;
  13. import java.util.Map;
  14. /**
  15. *
  16. * @author JNPF开发平台组
  17. * @version V3.1.0
  18. * @copyright 引迈信息技术有限公司
  19. * @date 2021/3/16 10:58
  20. */
  21. public class ZxingCodeUtil {
  22. //-----------------------------------条形码-----------------------------------
  23. /**
  24. * 条形码的颜色
  25. */
  26. private static final int BLACK = 0xff000000;
  27. /**
  28. * 背景色
  29. */
  30. private static final int WHITE = 0xFFFFFFFF;
  31. /**
  32. * 生成一维码(128)
  33. *
  34. * @param message 内容
  35. * @param width 宽度
  36. * @param height 高度
  37. * @return
  38. */
  39. public static BufferedImage getBarcode(String message, int width, int height) {
  40. try {
  41. Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
  42. hints.put(EncodeHintType.CHARACTER_SET, "code");
  43. BitMatrix bitMatrix = new MultiFormatWriter().encode(message,BarcodeFormat.CODE_128, width, height, hints);
  44. return toBufferedImage(bitMatrix);
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. return null;
  49. }
  50. /**
  51. * 转换成图片
  52. *
  53. * @param matrix
  54. * @return
  55. */
  56. private static BufferedImage toBufferedImage(BitMatrix matrix) {
  57. int width = matrix.getWidth();
  58. int height = matrix.getHeight();
  59. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  60. for (int x = 0; x < width; x++) {
  61. for (int y = 0; y < height; y++) {
  62. image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
  63. }
  64. }
  65. return image;
  66. }
  67. //--------------------------二维码-----------------------------------
  68. /**
  69. * 二维码颜色
  70. */
  71. private static final int QRCOLOR = 0xFF000000;
  72. /**
  73. * 背景色
  74. */
  75. private static final int BGCOLOR = 0xFFFFFFFF;
  76. /**
  77. * 生成普通的二维码
  78. * @param message 二维码内容
  79. * @param width 宽度
  80. * @param height 高度
  81. * @return
  82. */
  83. public static BufferedImage createCode(String message, int width, int height) {
  84. MultiFormatWriter multiFormatWriter = null;
  85. BitMatrix bm = null;
  86. BufferedImage image = null;
  87. Map<EncodeHintType, Object> hints = getDecodeHintType();
  88. try {
  89. multiFormatWriter = new MultiFormatWriter();
  90. // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
  91. bm = multiFormatWriter.encode(message, BarcodeFormat.QR_CODE, width, height, hints);
  92. int w = bm.getWidth();
  93. int h = bm.getHeight();
  94. image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  95. // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
  96. for (int x = 0; x < w; x++) {
  97. for (int y = 0; y < h; y++) {
  98. image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGCOLOR);
  99. }
  100. }
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. }
  104. return image;
  105. }
  106. /**
  107. * 生成带logo的二维码
  108. * @param message 二维码内容
  109. * @param logoPath log路径
  110. * @param width 宽度
  111. * @param height 高度
  112. * @return
  113. */
  114. public static BufferedImage createCodeWithLogo(String message, String logoPath, int width, int height) {
  115. BufferedImage bim = createCode(message,width,height);
  116. try {
  117. // 读取二维码图片,并构建绘图对象
  118. BufferedImage image = bim;
  119. Graphics2D g = image.createGraphics();
  120. // 读取Logo图片
  121. BufferedImage logo = ImageIO.read(new File(XSSEscape.escapePath(logoPath)));
  122. //设置logo的大小,这里设置为二维码图片的20%,过大会盖掉二维码
  123. int widthLogo = logo.getWidth(null) > image.getWidth() * 3 / 10 ? (image.getWidth() * 3 / 10) : logo.getWidth(null),
  124. heightLogo = logo.getHeight(null) > image.getHeight() * 3 / 10 ? (image.getHeight() * 3 / 10) : logo.getWidth(null);
  125. // logo放在中心
  126. int x = (image.getWidth() - widthLogo) / 2;
  127. int y = (image.getHeight() - heightLogo) / 2;
  128. //开始绘制图片
  129. g.drawImage(logo, x, y, widthLogo, heightLogo, null);
  130. g.dispose();
  131. logo.flush();
  132. image.flush();
  133. return image;
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. return null;
  138. }
  139. /**
  140. * 生成带logo和文字的二维码
  141. * @param message 二维码内容
  142. * @param logoPath log路径
  143. * @param text 文字
  144. * @param width 宽度
  145. * @param height 高度
  146. * @return
  147. */
  148. public static BufferedImage createCodeWithLogoAndText(String message, String logoPath, String text, int width, int height) {
  149. BufferedImage image = createCodeWithLogo(message, logoPath,width,height);
  150. try {
  151. if (text != null && !"".equals(text)) {
  152. //新的图片,把带logo的二维码下面加上文字
  153. BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
  154. Graphics2D outg = outImage.createGraphics();
  155. //画二维码到新的面板
  156. outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
  157. //画文字到新的面板
  158. outg.setColor(Color.BLACK);
  159. //字体、字型、字号
  160. outg.setFont(new Font("宋体", Font.BOLD, 30));
  161. int strWidth = outg.getFontMetrics().stringWidth(text);
  162. if (strWidth > 399) {
  163. String productName1 = text.substring(0, text.length() / 2);
  164. String productName2 = text.substring(text.length() / 2, text.length());
  165. int strWidth1 = outg.getFontMetrics().stringWidth(productName1);
  166. int strWidth2 = outg.getFontMetrics().stringWidth(productName2);
  167. outg.drawString(productName1, 200 - strWidth1 / 2, image.getHeight() + (outImage.getHeight() - image.getHeight()) / 2 + 12);
  168. BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
  169. Graphics2D outg2 = outImage2.createGraphics();
  170. outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
  171. outg2.setColor(Color.BLACK);
  172. //字体、字型、字号
  173. outg2.setFont(new Font("宋体", Font.BOLD, 30));
  174. outg2.drawString(productName2, 200 - strWidth2 / 2, outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
  175. outg2.dispose();
  176. outImage2.flush();
  177. outImage = outImage2;
  178. } else {
  179. //画文字
  180. outg.drawString(text, 200 - strWidth / 2, image.getHeight() + (outImage.getHeight() - image.getHeight()) / 2 + 12);
  181. }
  182. outg.dispose();
  183. outImage.flush();
  184. image = outImage;
  185. image.flush();
  186. }
  187. } catch (Exception e) {
  188. e.printStackTrace();
  189. }
  190. return image;
  191. }
  192. /**
  193. * 设置二维码的格式参数
  194. * @return
  195. */
  196. private static Map<EncodeHintType, Object> getDecodeHintType() {
  197. Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(16);
  198. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  199. hints.put(EncodeHintType.CHARACTER_SET, Constants.UTF8);
  200. hints.put(EncodeHintType.MARGIN, 0);
  201. hints.put(EncodeHintType.MAX_SIZE, 350);
  202. hints.put(EncodeHintType.MIN_SIZE, 100);
  203. return hints;
  204. }
  205. }