├── .gitignore
├── README.md
└── java
├── README.md
├── pom.xml
└── src
└── org
└── nutz
└── qrcode
├── QRCode.java
└── QRCodeFormat.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /java/.settings
2 | /java/target
3 | /java/.classpath
4 | /java/.project
5 | /java/bin
6 |
7 | .DS_Store
8 | ._*
9 | /bin/
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### QRCode on Nutz
2 |
3 | 这是一个生成 QRCode 的项目,分为 Java 版与 C 版。
4 |
5 | 其中 Java 版基于 [zxing](http://code.google.com/p/zxing/) 项目进行了封装。
6 |
7 | QRCode 的详细介绍请参照 [维基百科](http://zh.wikipedia.org/zh-cn/QR%E7%A0%81)。
8 |
--------------------------------------------------------------------------------
/java/README.md:
--------------------------------------------------------------------------------
1 | ### QRCode on Nutz
2 |
3 | 这是一个生成 QRCode 的项目,基于 [zxing](http://code.google.com/p/zxing/) 项目进行了封装。
4 |
5 | ### How to ues
6 |
7 | #### 生成 QRCode
8 |
9 | ```java
10 | // 直接获得 QRCode 的图像对象
11 | BufferedImage image = QRCode.toQRCode("this's simple text.");
12 |
13 | // 或者通过构造器模式
14 | QRCode qrcode = QRCode.NEW("this's simple text by used build method.");
15 |
16 | // 获得 QRCode 的图像对象
17 | BufferedImage image = qrcode.getQrcodeImage();
18 |
19 | // 在指定路径的文件中生成一个 QRCode 的图片,图片格式为 png
20 | // 如果指定的文件没有扩展名,将生成一个扩展名为 png 的图片
21 | String filePath = "/PATH/TO/FILE.jpg";
22 | qrcode.toFile(filePath);
23 |
24 | // 或者直接指定一个 File 对象
25 | File file = new File(filePath);
26 | qrcode.toFile(file);
27 | ```
28 |
29 | 默认使用如下参数生成 QRCode,
30 |
31 | * 图片大小: 256 x 256 像素
32 | * 内容编码格式: UTF-8
33 | * 错误修正等级: Level M (有15% 的内容可被修正)
34 | * 前景色: 黑色
35 | * 背景色: 白色
36 | * 输出图片的文件格式: png
37 | * 图片空白区域大小: 0个单位
38 |
39 | 如需修改生成参数,请使用`QRCodeFormat`类来实现,比如
40 |
41 | ```java
42 | // 生成一个带有默认值的生成器格式
43 | QRCodeFormat format = QRCodeFormat.NEW();
44 |
45 | // 改变生成器格式的值
46 | format.setSize(400) // 设置图片大小
47 | .setEncode("GB18030") // 设置文字编码
48 | .setErrorCorrectionLevel('H') // 设置错误修正等级
49 | .setForeGroundColor("#2F4F4F") // 设置前景色
50 | .setBackGroundColor("#808080") // 设置背景色
51 | .setImageFormat("jpg") // 设置生成的图片格式
52 | .setMargin(0) // 设置图片空白区域, 单位 - 格(外填充)
53 | .setIcon(new File("/PATH/TO/ICON_FILE")); // 设置 icon
54 |
55 | // 然后
56 | // 使用指定的生成器格式生成一个 QRCode 的图像对象
57 | BufferedImage image = QRCode.toQRCode("this's simple text by used format.", format);
58 |
59 | // 或者
60 | // 使用指定的生成器格式生成一个 QRCode 构造器
61 | QRCode qrcode = QRCode.NEW("this's simple test text by used format.", format);
62 | ```
63 |
64 | 另外能给生成的 QRCode 的中添加上指定的图片
65 |
66 | ```java
67 | QRCode qrcode = QRCode.NEW("this's simple test text. and add icon file.");
68 |
69 | // 在指定路径的文件中生成 QRCode 的图片,以及需要添加的 icon 的图片路径。(添加的图片不支持 ico 格式)
70 | // 指定的 icon 的图片不存在时将不会在 QRCode 中添加图片
71 | String filePath = "/PATH/TO/FILE";
72 | String iconFilePath = "/PATH/TO/ICON_FILE";
73 | qrcode.toFile(filePath, iconFilePath);
74 |
75 | // 或者直接使用 File 对象
76 | File file = new File(filePath);
77 | File iconFile = new File(iconFilePath);
78 | qrcode.toFile(file, iconFile);
79 |
80 | // 或者使用生成器格式来设置
81 | QRCodeFormat format = QRCodeFormat.NEW().setIcon(new File(iconFilePath));
82 | QRCode qrcode = QRCode.NEW("this's simple test text. and add icon file form format.", format);
83 | File file = new File(filePath);
84 | qrcode.toFile(file, iconFile);
85 | ```
86 |
87 | 如果直接指定了添加的 icon 文件的时候,将忽略生成器格式中的 icon 文件。
88 |
89 | 为了保证生成的 QRCode 的图片能被正确识别,请按需设置 QRCode 的图片大小,以及 icon 图片的大小。
90 |
91 | #### 解析 QRCode
92 |
93 | ```java
94 | // 图片路径
95 | String filePath = "/PATH/TO/FILE";
96 | String content = QRCode.from(filePath);
97 |
98 | // 或者直接传入文件对象
99 | File file = new File(filePath);
100 | content = QRCode.from(file);
101 |
102 | // 或者 QRCode 图像对象
103 | BufferedImage image = ImageIO.read(file);
104 | content = QRCode.from(image);
105 |
106 | // 支持直接解析 QRCode 图片的 URL 地址(以「http」或者「https」开头)
107 | String url = "https://chart.googleapis.com/chart?chs=72x72&cht=qr&choe=UTF-8&chl=http%3A%2F%2Fwww.nutz.cn%2F";
108 | content = QRCode.from(url);
109 |
110 | // 或者该地址的对象
111 | URL imageUrl = new URL(url);
112 | content = QRCode.from(imageUrl);
113 | ```
114 |
115 | ### 其他
116 |
117 | 请使用`mvn package -Dmaven.test.skip=true`生成 jar 包使用。
118 |
119 | 如果想把代码当成一个工程导入到 eclipse 中,请运行`mvn eclipse:eclipse`。
120 |
121 | QRCode 的详细介绍请参照 [维基百科](http://zh.wikipedia.org/zh-cn/QR%E7%A0%81)。
122 |
--------------------------------------------------------------------------------
/java/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | org.nutz
6 | nutz-qrcode
7 | 1.r.57-SNAPSHOT
8 | jar
9 |
10 | nutz-qrcode
11 | http://maven.apache.org
12 |
13 |
14 | Qrcode for Nutz
15 |
16 |
17 |
18 | Github Issue
19 | http://github.com/nutzam/nutz-qrcode/issues
20 |
21 |
22 |
23 | The Apache Software License, Version 2.0
24 | http://apache.org/licenses/LICENSE-2.0.txt
25 |
26 |
27 |
28 |
29 | wendal
30 | Wendal Chen
31 | wendal1985@gmail.com
32 | http://wendal.net/
33 |
34 |
35 | ywjno
36 | ywjno
37 | ywjno@gmail.com
38 | https://github.com/ywjno
39 |
40 |
41 |
42 | scm:git:git://github.com/nutzam/nutz-qrcode.git
43 | scm:git:git://github.com/nutzam/nutz-qrcode.git
44 | git://github.com/nutzam/nutz-qrcode.git
45 |
46 |
47 |
48 | UTF-8
49 |
50 |
51 |
52 |
53 | com.google.zxing
54 | core
55 | 3.2.1
56 |
57 |
58 | com.google.zxing
59 | javase
60 | 3.2.1
61 |
62 |
63 |
64 |
65 | ${project.basedir}/src
66 |
67 |
68 | org.apache.maven.plugins
69 | maven-compiler-plugin
70 | 3.1
71 |
72 | 1.6
73 | 1.6
74 |
75 |
76 |
77 | org.apache.maven.plugins
78 | maven-surefire-plugin
79 |
80 | once
81 | -Dfile.encoding=UTF-8
82 |
83 |
84 |
85 | org.apache.maven.plugins
86 | maven-javadoc-plugin
87 |
88 | -Xdoclint:none
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/java/src/org/nutz/qrcode/QRCode.java:
--------------------------------------------------------------------------------
1 | package org.nutz.qrcode;
2 |
3 | import java.awt.AlphaComposite;
4 | import java.awt.Color;
5 | import java.awt.Graphics;
6 | import java.awt.Graphics2D;
7 | import java.awt.RenderingHints;
8 | import java.awt.color.ColorSpace;
9 | import java.awt.image.BufferedImage;
10 | import java.io.File;
11 | import java.io.IOException;
12 | import java.net.MalformedURLException;
13 | import java.net.URL;
14 | import java.nio.charset.Charset;
15 |
16 | import javax.imageio.ImageIO;
17 |
18 | import com.google.zxing.BarcodeFormat;
19 | import com.google.zxing.BinaryBitmap;
20 | import com.google.zxing.ChecksumException;
21 | import com.google.zxing.FormatException;
22 | import com.google.zxing.LuminanceSource;
23 | import com.google.zxing.NotFoundException;
24 | import com.google.zxing.Result;
25 | import com.google.zxing.WriterException;
26 | import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
27 | import com.google.zxing.common.BitMatrix;
28 | import com.google.zxing.common.HybridBinarizer;
29 | import com.google.zxing.qrcode.QRCodeReader;
30 | import com.google.zxing.qrcode.QRCodeWriter;
31 |
32 | /**
33 | * QRCode 处理器
34 | *
35 | * @author ywjno(ywjno.dev@gmail.com)
36 | */
37 | public final class QRCode {
38 |
39 | /** QRCode 生成器格式 */
40 | private QRCodeFormat format = null;
41 |
42 | /** 生成的 QRCode 图像对象 */
43 | private BufferedImage qrcodeImage = null;
44 |
45 | /** 生成的 QRCode 图片文件 */
46 | private File qrcodeFile = null;
47 |
48 | /**
49 | * 返回生成的 QRCode 图像对象
50 | *
51 | * @return 生成的 QRCode 图像对象
52 | */
53 | public BufferedImage getQrcodeImage() {
54 | return qrcodeImage;
55 | }
56 |
57 | /**
58 | * 返回生成的 QRCode 图片文件
59 | *
60 | * @return 生成的 QRCode 图片文件
61 | */
62 | public File getQrcodeFile() {
63 | return qrcodeFile;
64 | }
65 |
66 | private QRCode() {
67 |
68 | }
69 |
70 | /**
71 | * 使用带默认值的「QRCode 生成器格式」来创建一个 QRCode 处理器。
72 | *
73 | * @param content
74 | * 所要生成 QRCode 的内容
75 | *
76 | * @return QRCode 处理器
77 | */
78 | public static QRCode NEW(final String content) {
79 | return NEW(content, QRCodeFormat.NEW());
80 | }
81 |
82 | /**
83 | * 使用指定的「QRCode 生成器格式」来创建一个 QRCode 处理器。
84 | *
85 | * @param content
86 | * 所要生成 QRCode 的内容
87 | * @param format
88 | * QRCode 生成器格式
89 | *
90 | * @return QRCode 处理器
91 | */
92 | public static QRCode NEW(final String content, QRCodeFormat format) {
93 | QRCode qrcode = new QRCode();
94 | qrcode.format = format;
95 | qrcode.qrcodeImage = toQRCode(content, format);
96 | return qrcode;
97 | }
98 |
99 | /**
100 | * 把指定的内容生成为一个 QRCode 的图片,之后保存到指定的文件中。
101 | *
102 | * @param f
103 | * 指定的文件
104 | *
105 | * @return QRCode 处理器
106 | */
107 | public QRCode toFile(String f) {
108 | return toFile(new File(f), this.format.getIcon());
109 | }
110 |
111 | /**
112 | * 把指定的内容生成为一个 QRCode 的图片,之后保存到指定的文件中。
113 | *
114 | * @param qrcodeFile
115 | * 指定的文件
116 | *
117 | * @return QRCode 处理器
118 | */
119 | public QRCode toFile(File qrcodeFile) {
120 | return toFile(qrcodeFile, this.format.getIcon());
121 | }
122 |
123 | /**
124 | * 把指定的内容生成为一个 QRCode 的图片,并在该图片中间添加上指定的图片;之后保存到指定的文件内。
125 | *
126 | * @param qrcodeFile
127 | * QRCode 图片生成的指定的文件
128 | * @param appendFile
129 | * 需要添加的图片。传入的文件路径如果没有(null 或者为空)的时候将忽略该参数
130 | *
131 | * @return QRCode 处理器
132 | */
133 | public QRCode toFile(String qrcodeFile, String appendFile) {
134 | if (null == appendFile || appendFile.length() == 0) {
135 | return toFile(new File(qrcodeFile));
136 | }
137 | return toFile(new File(qrcodeFile), new File(appendFile));
138 | }
139 |
140 | /**
141 | * 把指定的内容生成为一个 QRCode 的图片,并在该图片中间添加上指定的图片;之后保存到指定的文件内。
142 | *
143 | * @param qrcodeFile
144 | * QRCode 图片生成的指定的文件
145 | * @param appendFile
146 | * 需要添加的图片。传入的图片不存在的时候将忽略该参数
147 | *
148 | * @return QRCode 处理器
149 | */
150 | public QRCode toFile(File qrcodeFile, File appendFile) {
151 | try {
152 | if (!qrcodeFile.exists()) {
153 | qrcodeFile.getParentFile().mkdirs();
154 | qrcodeFile.createNewFile();
155 | }
156 |
157 | if (null != appendFile && appendFile.isFile() && appendFile.length() != 0) {
158 | appendImage(ImageIO.read(appendFile));
159 | }
160 |
161 | if (!ImageIO.write(this.qrcodeImage, getSuffixName(qrcodeFile), qrcodeFile)) {
162 | throw new RuntimeException("Unexpected error writing image");
163 | }
164 | }
165 | catch (IOException e) {
166 | throw new RuntimeException(e);
167 | }
168 | this.qrcodeFile = qrcodeFile;
169 | return this;
170 | }
171 |
172 | private void appendImage(BufferedImage appendImage) {
173 | appendImage(this.qrcodeImage, appendImage, this.format);
174 | }
175 |
176 | private static void appendImage(BufferedImage qrcodeImage, BufferedImage appendImage, QRCodeFormat format) {
177 | int baseWidth = qrcodeImage.getWidth();
178 | int baseHeight = qrcodeImage.getHeight();
179 |
180 | // 计算 icon 的最大边长
181 | // 公式为 二维码面积*错误修正等级*0.4 的开方
182 | int maxWidth = (int) Math.sqrt(baseWidth * baseHeight * format.getErrorCorrectionLevelValue() * 0.4);
183 | int maxHeight = maxWidth;
184 |
185 | // 获取 icon 的实际边长
186 | int roundRectWidth = (maxWidth < appendImage.getWidth()) ? maxWidth : appendImage.getWidth();
187 | int roundRectHeight = (maxHeight < appendImage.getHeight()) ? maxHeight : appendImage.getHeight();
188 |
189 | BufferedImage roundRect = new BufferedImage(roundRectWidth, roundRectHeight, BufferedImage.TYPE_INT_ARGB);
190 |
191 | Graphics2D g2 = roundRect.createGraphics();
192 | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
193 | g2.setColor(Color.WHITE);
194 | g2.fillRoundRect(0, 0, roundRectWidth, roundRectHeight, 27, 27);
195 | g2.setComposite(AlphaComposite.SrcAtop);
196 | g2.drawImage(appendImage, 0, 0, roundRectWidth, roundRectHeight, null);
197 | g2.dispose();
198 |
199 | Graphics gc = qrcodeImage.getGraphics();
200 | gc.setColor(format.getBackGroundColor());
201 | gc.drawImage(roundRect, (baseWidth - roundRectWidth) / 2, (baseHeight - roundRectHeight) / 2, null);
202 | gc.dispose();
203 | }
204 |
205 | /**
206 | * 使用带默认值的「QRCode 生成器格式」,把指定的内容生成为一个 QRCode 的图像对象。
207 | *
208 | * @param content
209 | * 所需生成 QRCode 的内容
210 | *
211 | * @return QRCode 的图像对象
212 | */
213 | public static BufferedImage toQRCode(String content) {
214 | return toQRCode(content, null);
215 | }
216 |
217 | /**
218 | * 使用指定的「QRCode生成器格式」,把指定的内容生成为一个 QRCode 的图像对象。
219 | *
220 | * @param content
221 | * 所需生成 QRCode 的内容
222 | * @param format
223 | * QRCode 生成器格式
224 | * @return QRCode 的图像对象
225 | */
226 | public static BufferedImage toQRCode(String content, QRCodeFormat format) {
227 | if (format == null) {
228 | format = QRCodeFormat.NEW();
229 | }
230 |
231 | content = new String(content.getBytes(Charset.forName(format.getEncode())));
232 | BitMatrix matrix = null;
233 | try {
234 | matrix = new QRCodeWriter().encode(content,
235 | BarcodeFormat.QR_CODE,
236 | format.getSize(),
237 | format.getSize(),
238 | format.getHints());
239 | }
240 | catch (WriterException e) {
241 | throw new RuntimeException(e);
242 | }
243 |
244 | int width = matrix.getWidth();
245 | int height = matrix.getHeight();
246 | int fgColor = format.getForeGroundColor().getRGB();
247 | int bgColor = format.getBackGroundColor().getRGB();
248 | BufferedImage image = new BufferedImage(width, height, ColorSpace.TYPE_RGB);
249 | for (int x = 0; x < width; x++) {
250 | for (int y = 0; y < height; y++) {
251 | image.setRGB(x, y, matrix.get(x, y) ? fgColor : bgColor);
252 | }
253 | }
254 |
255 | File appendFile = format.getIcon();
256 | if (null != appendFile && appendFile.isFile() && appendFile.length() != 0) {
257 | BufferedImage appendImage = null;
258 | try {
259 | appendImage = ImageIO.read(appendFile);
260 | }
261 | catch (IOException e) {
262 | throw new RuntimeException(e);
263 | }
264 |
265 | appendImage(image, appendImage, format);
266 | }
267 |
268 | return image;
269 | }
270 |
271 | /**
272 | * 从指定的 QRCode 图片文件中解析出其内容。
273 | *
274 | * @param qrcodeFile
275 | * QRCode 文件
276 | *
277 | * @return QRCode 中的内容
278 | */
279 | public static String from(String qrcodeFile) {
280 | if (qrcodeFile.startsWith("http://") || qrcodeFile.startsWith("https://")) {
281 | try {
282 | return from(new URL(qrcodeFile));
283 | }
284 | catch (MalformedURLException e) {
285 | throw new RuntimeException(e);
286 | }
287 | } else {
288 | return from(new File(qrcodeFile));
289 | }
290 | }
291 |
292 | /**
293 | * 从指定的 QRCode 图片文件中解析出其内容。
294 | *
295 | * @param qrcodeFile
296 | * QRCode 图片文件
297 | *
298 | * @return QRCode 中的内容
299 | */
300 | public static String from(File qrcodeFile) {
301 | try {
302 | BufferedImage image = ImageIO.read(qrcodeFile);
303 | return from(image);
304 | }
305 | catch (IOException e) {
306 | throw new RuntimeException(e);
307 | }
308 | }
309 |
310 | /**
311 | * 从指定的 QRCode 图片链接中解析出其内容。
312 | *
313 | * @param qrcodeUrl
314 | * QRCode 图片链接
315 | *
316 | * @return QRCode 中的内容
317 | */
318 | public static String from(URL qrcodeUrl) {
319 | try {
320 | BufferedImage image = ImageIO.read(qrcodeUrl);
321 | return from(image);
322 | }
323 | catch (IOException e) {
324 | throw new RuntimeException(e);
325 | }
326 | }
327 |
328 | /**
329 | * 从指定的 QRCode 图像对象中解析出其内容。
330 | *
331 | * @param qrcodeImage
332 | * QRCode 图像对象
333 | *
334 | * @return QRCode 中的内容
335 | */
336 | public static String from(BufferedImage qrcodeImage) {
337 | LuminanceSource source = new BufferedImageLuminanceSource(qrcodeImage);
338 | BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
339 | String content = null;
340 | try {
341 | Result result = new QRCodeReader().decode(bitmap);
342 | content = result.getText();
343 | }
344 | catch (NotFoundException e) {
345 | throw new RuntimeException(e);
346 | }
347 | catch (ChecksumException e) {
348 | throw new RuntimeException(e);
349 | }
350 | catch (FormatException e) {
351 | throw new RuntimeException(e);
352 | }
353 | return content;
354 | }
355 |
356 | private String getSuffixName(File file) {
357 | String path = file.getAbsolutePath();
358 |
359 | if (null == path) {
360 | return this.format.getImageFormat();
361 | }
362 | int pos = path.lastIndexOf('.');
363 | if (-1 == pos) {
364 | return this.format.getImageFormat();
365 | }
366 | return path.substring(pos + 1).toUpperCase();
367 | }
368 | }
369 |
--------------------------------------------------------------------------------
/java/src/org/nutz/qrcode/QRCodeFormat.java:
--------------------------------------------------------------------------------
1 | package org.nutz.qrcode;
2 |
3 | import java.awt.Color;
4 | import java.io.File;
5 | import java.util.Hashtable;
6 |
7 | import com.google.zxing.EncodeHintType;
8 | import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
9 |
10 | /**
11 | * QRCode 生成器的格式
12 | *
13 | * @author ywjno(ywjno.dev@gmail.com)
14 | */
15 | public class QRCodeFormat {
16 |
17 | /** 图片大小 */
18 | private int size;
19 |
20 | /** 内容编码格式 */
21 | private String encode;
22 |
23 | /** 错误修正等级 (Error Collection Level) */
24 | private ErrorCorrectionLevel errorCorrectionLevel;
25 |
26 | /** 错误修正等级的具体值 */
27 | private double errorCorrectionLevelValue;
28 |
29 | /** 前景色 */
30 | private Color foreGroundColor;
31 |
32 | /** 背景色 */
33 | private Color backGroundColor;
34 |
35 | /** 图片的文件格式 */
36 | private String imageFormat;
37 |
38 | /** 图片的外边距大小 (Quiet Zone) */
39 | private int margin;
40 |
41 | /** 提供给编码器额外的参数 */
42 | private Hashtable hints;
43 |
44 | /** 需要添加的图片 */
45 | private File icon;
46 |
47 | /**
48 | * 创建一个带有默认值的 QRCode 生成器的格式。默认值如下
49 | *
50 | *
51 | * - 图片大小: 256px
52 | * - 内容编码格式: UTF-8
53 | * - 错误修正等级: Level M (有15% 的内容可被修正)
54 | * - 前景色: 黑色
55 | * - 背景色: 白色
56 | * - 输出图片的文件格式: png
57 | * - 图片空白区域大小: 0个单位
58 | *
59 | *
60 | * @return QRCode 生成器格式
61 | */
62 | public static QRCodeFormat NEW() {
63 | return new QRCodeFormat();
64 | }
65 |
66 | private QRCodeFormat() {
67 | this.size = 256;
68 | this.encode = "utf-8";
69 | this.errorCorrectionLevel = ErrorCorrectionLevel.M;
70 | this.errorCorrectionLevelValue = 0.15;
71 | this.foreGroundColor = Color.BLACK;
72 | this.backGroundColor = Color.WHITE;
73 | this.imageFormat = "png";
74 | this.margin = 0;
75 | this.hints = new Hashtable();
76 | }
77 |
78 | /**
79 | * 返回图片的大小。
80 | *
81 | * @return 图片的大小
82 | */
83 | public int getSize() {
84 | return this.size;
85 | }
86 |
87 | /**
88 | * 设置图片的大小。图片的大小等于实际内容与外边距的值(建议设置成偶数值)。
89 | *
90 | * @param size
91 | * 图片的大小
92 | *
93 | * @return QRCode生成器的格式
94 | */
95 | public QRCodeFormat setSize(int size) {
96 | this.size = size;
97 | return this;
98 | }
99 |
100 | /**
101 | * 返回内容编码格式。
102 | *
103 | * @return 内容编码格式
104 | */
105 | public String getEncode() {
106 | return encode;
107 | }
108 |
109 | /**
110 | * 设置内容编码格式。
111 | *
112 | * @param encode
113 | * 内容编码格式
114 | *
115 | * @return QRCode生成器的格式
116 | */
117 | public QRCodeFormat setEncode(String encode) {
118 | this.encode = encode;
119 | return this;
120 | }
121 |
122 | /**
123 | * 返回错误修正等级。
124 | *
125 | * @return 错误修正等级
126 | */
127 | public ErrorCorrectionLevel getErrorCorrectionLevel() {
128 | return errorCorrectionLevel;
129 | }
130 |
131 | /**
132 | * 返回错误修正等级的具体值。
133 | *
134 | * @return 错误修正等级的具体值
135 | */
136 | public double getErrorCorrectionLevelValue() {
137 | return errorCorrectionLevelValue;
138 | }
139 |
140 | /**
141 | * 设置错误修正等级。其定义如下
142 | *
143 | *
144 | * - L: 有 7% 的内容可被修正
145 | * - M: 有15% 的内容可被修正
146 | * - Q: 有 25% 的内容可被修正
147 | * - H: 有 30% 的内容可被修正
148 | *
149 | *
150 | * @param errorCorrectionLevel
151 | * 错误修正等级
152 | *
153 | * @return QRCode生成器的格式
154 | */
155 | public QRCodeFormat setErrorCorrectionLevel(char errorCorrectionLevel) {
156 | switch (Character.toUpperCase(errorCorrectionLevel)) {
157 | case 'L':
158 | this.errorCorrectionLevel = ErrorCorrectionLevel.L;
159 | this.errorCorrectionLevelValue = 0.07;
160 | break;
161 | case 'M':
162 | this.errorCorrectionLevel = ErrorCorrectionLevel.M;
163 | this.errorCorrectionLevelValue = 0.15;
164 | break;
165 | case 'Q':
166 | this.errorCorrectionLevel = ErrorCorrectionLevel.Q;
167 | this.errorCorrectionLevelValue = 0.25;
168 | break;
169 | case 'H':
170 | this.errorCorrectionLevel = ErrorCorrectionLevel.H;
171 | this.errorCorrectionLevelValue = 0.3;
172 | break;
173 | default:
174 | this.errorCorrectionLevel = ErrorCorrectionLevel.M;
175 | }
176 |
177 | return this;
178 | }
179 |
180 | /**
181 | * 返回前景色。
182 | *
183 | * @return 前景色
184 | */
185 | public Color getForeGroundColor() {
186 | return foreGroundColor;
187 | }
188 |
189 | /**
190 | * 设置前景色。值为十六进制的颜色值(与 CSS 定义颜色的值相同,不支持简写),可以忽略「#」符号。
191 | *
192 | * @param foreGroundColor
193 | * 前景色的值
194 | *
195 | * @return QRCode生成器的格式
196 | */
197 | public QRCodeFormat setForeGroundColor(String foreGroundColor) {
198 | try {
199 | this.foreGroundColor = getColor(foreGroundColor);
200 | }
201 | catch (NumberFormatException e) {
202 | this.foreGroundColor = Color.BLACK;
203 | }
204 | return this;
205 | }
206 |
207 | /**
208 | * 设置前景色。
209 | *
210 | * @param foreGroundColor
211 | * 前景色的值
212 | *
213 | * @return QRCode生成器的格式
214 | */
215 | public QRCodeFormat setForeGroundColor(Color foreGroundColor) {
216 | this.foreGroundColor = foreGroundColor;
217 | return this;
218 | }
219 |
220 | /**
221 | * 返回背景色。
222 | *
223 | * @return 背景色
224 | */
225 | public Color getBackGroundColor() {
226 | return backGroundColor;
227 | }
228 |
229 | /**
230 | * 设置背景色。值为十六进制的颜色值(与 CSS 定义颜色的值相同,不支持简写),可以忽略「#」符号。
231 | *
232 | * @param backGroundColor
233 | * 前景色的值
234 | *
235 | * @return QRCode生成器的格式
236 | */
237 | public QRCodeFormat setBackGroundColor(String backGroundColor) {
238 | try {
239 | this.backGroundColor = getColor(backGroundColor);
240 | }
241 | catch (NumberFormatException e) {
242 | this.backGroundColor = Color.WHITE;
243 | }
244 | return this;
245 | }
246 |
247 | /**
248 | * 设置背景色。
249 | *
250 | * @param backGroundColor
251 | * 前景色的值
252 | *
253 | * @return QRCode生成器的格式
254 | */
255 | public QRCodeFormat setBackGroundColor(Color backGroundColor) {
256 | this.backGroundColor = backGroundColor;
257 | return this;
258 | }
259 |
260 | /**
261 | * 返回图片的文件格式。
262 | *
263 | * @return 图片的文件格式
264 | */
265 | public String getImageFormat() {
266 | return imageFormat.toUpperCase();
267 | }
268 |
269 | /**
270 | * 设置图片的文件格式 。
271 | *
272 | * @param imageFormat
273 | * 图片的文件格式
274 | *
275 | * @return QRCode生成器的格式
276 | */
277 | public QRCodeFormat setImageFormat(String imageFormat) {
278 | this.imageFormat = imageFormat;
279 | return this;
280 | }
281 |
282 | /**
283 | * 返回图片的外边距大小。
284 | *
285 | * @return 图片的外边距大小
286 | */
287 | public int getMargin() {
288 | return margin;
289 | }
290 |
291 | /**
292 | * 设置图片的外边距大小 。
293 | *
294 | * @param margin
295 | * 图片的外边距大小
296 | *
297 | * @return QRCode生成器的格式
298 | */
299 | public QRCodeFormat setMargin(int margin) {
300 | this.margin = margin;
301 | return this;
302 | }
303 |
304 | /**
305 | * 返回提供给编码器额外的参数。
306 | *
307 | * @return 提供给编码器额外的参数
308 | */
309 | public Hashtable getHints() {
310 | hints.clear();
311 | hints.put(EncodeHintType.ERROR_CORRECTION, getErrorCorrectionLevel());
312 | hints.put(EncodeHintType.CHARACTER_SET, getEncode());
313 | hints.put(EncodeHintType.MARGIN, getMargin());
314 | return hints;
315 | }
316 |
317 | /**
318 | * 返回添加的图片。
319 | *
320 | * @return 添加的图片
321 | */
322 | public File getIcon() {
323 | return icon;
324 | }
325 |
326 | /**
327 | * 设置添加的图片 。
328 | *
329 | * @param icon
330 | * 添加的图片
331 | *
332 | * @return QRCode生成器的格式
333 | */
334 | public QRCodeFormat setIcon(File icon) {
335 | this.icon = icon;
336 | return this;
337 | }
338 |
339 | /**
340 | * 设置添加的图片 。
341 | *
342 | * @param iconPath
343 | * 添加的图片
344 | *
345 | * @return QRCode生成器的格式
346 | */
347 | public QRCodeFormat setIcon(String iconPath) {
348 | return setIcon(new File(iconPath));
349 | }
350 |
351 | private Color getColor(String hexString) {
352 | if (hexString.charAt(0) == '#') {
353 | return new Color(Long.decode(hexString).intValue());
354 | } else {
355 | return new Color(Long.decode("0xFF" + hexString).intValue());
356 | }
357 | }
358 | }
359 |
--------------------------------------------------------------------------------