随着微信在市场上的占有率不对升高,二维码开始逐渐进入人们的视野,扫码支付,扫码关注,扫码打开连接...
究竟二维码是个什么东西我就不在这里赘述了,关于这方面大家可以去上搜索引擎
我在这里就简单介绍一下一个Java的生成二维码工具,是Google提供的,zxing
上代码,
1 package com.xxxx.xx;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics2D;
6 import java.awt.image.BufferedImage;
7 import java.io.File;
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.Objects;
11
12 import javax.imageio.ImageIO;
13
14 import org.apache.commons.lang3.StringUtils;
15
16 import com.google.zxing.BarcodeFormat;
17 import com.google.zxing.EncodeHintType;
18 import com.google.zxing.MultiFormatWriter;
19 import com.google.zxing.WriterException;
20 import com.google.zxing.common.BitMatrix;
21 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
22
23 /**
24 * 画制定logo和制定描述的二维码
25 *
26 * @author songyz
27 *
28 */
29 public class ZXingCode {
30 private static final int QRCOLOR = 0xFF000000; // 默认是黑色
31 private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色
32
33 private static final int WIDTH = 400; // 二维码宽
34 private static final int HEIGHT = 400; // 二维码高
35
36 // 用于设置QR二维码参数
37 private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
38 private static final long serialVersionUID = 1L;
39 {
40 put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别(H为*别)具体级别信息
41 put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
42 put(EncodeHintType.MARGIN, 0);
43 }
44 };
45
46 public static void main(String[] args) throws WriterException {
47 File logoFile = new File("D://QrCode/logo3.png");
48 File QrCodeFile = new File("D://QrCode/05.png");
49 String url = "https://www.baidu.com/";
50 String note = "访问百度连接";
51 drawLogoQRCode(logoFile, QrCodeFile, url, note);
52 }
53
54 // 生成带logo的二维码图片
55 public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) {
56 try {
57 MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
58 // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
59 BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
60 BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
61
62 // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
63 for (int x = 0; x < WIDTH; x++) {
64 for (int y = 0; y < HEIGHT; y++) {
65 image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
66 }
67 }
68
69 int width = image.getWidth();
70 int height = image.getHeight();
71 if (Objects.nonNull(logoFile) && logoFile.exists()) {
72 // 构建绘图对象
73 Graphics2D g = image.createGraphics();
74 // 读取Logo图片
75 BufferedImage logo = ImageIO.read(logoFile);
76 // 开始绘制logo图片
77 g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
78 g.dispose();
79 logo.flush();
80 }
81
82 // 自定义文本描述
83 if (StringUtils.isNotEmpty(note)) {
84 // 新的图片,把带logo的二维码下面加上文字
85 BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
86 Graphics2D outg = outImage.createGraphics();
87 // 画二维码到新的面板
88 outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
89 // 画文字到新的面板
90 outg.setColor(Color.BLACK);
91 outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
92 int strWidth = outg.getFontMetrics().stringWidth(note);
93 if (strWidth > 399) {
94 // //长度过长就截取前面部分
95 // 长度过长就换行
96 String note1 = note.substring(0, note.length() / 2);
97 String note2 = note.substring(note.length() / 2, note.length());
98 int strWidth1 = outg.getFontMetrics().stringWidth(note1);
99 int strWidth2 = outg.getFontMetrics().stringWidth(note2);
100 outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
101 BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
102 Graphics2D outg2 = outImage2.createGraphics();
103 outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
104 outg2.setColor(Color.BLACK);
105 outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
106 outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
107 outg2.dispose();
108 outImage2.flush();
109 outImage = outImage2;
110 } else {
111 outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
112 }
113 outg.dispose();
114 outImage.flush();
115 image = outImage;
116 }
117
118 image.flush();
119
120 ImageIO.write(image, "png", codeFile); // TODO
121 } catch (Exception e) {
122 e.printStackTrace();
123 }
124 }
125
126 }