/
QRCode.java
197 lines (173 loc) · 6.72 KB
/
QRCode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package net.glxn.qrgen.javase;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import net.glxn.qrgen.core.AbstractQRCode;
import net.glxn.qrgen.core.exception.QRGenerationException;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.core.scheme.Schema;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
public class QRCode extends AbstractQRCode {
public static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig();
protected final String text;
protected MatrixToImageConfig matrixToImageConfig = DEFAULT_CONFIG;
protected QRCode(String text) {
this.text = text;
qrWriter = new QRCodeWriter();
}
/**
* Create a QR code from the given text. <br><br>
* <p>
* There is a size limitation to how much you can put into a QR code. This has been tested to work with up to a length of
* 2950
* characters.<br><br>
* </p>
* <p>
* The QRCode will have the following defaults: <br> {size: 100x100}<br>{imageType:PNG} <br><br>
* </p>
* Both size and imageType can be overridden: <br> Image type override is done by calling {@link
* QRCode#to(ImageType)} e.g. QRCode.from("hello world").to(JPG) <br> Size override is done
* by calling
* {@link QRCode#withSize} e.g. QRCode.from("hello world").to(JPG).withSize(125, 125) <br>
*
* @param text the text to encode to a new QRCode, this may fail if the text is too large. <br>
* @return the QRCode object <br>
*/
public static QRCode from(String text) {
return new QRCode(text);
}
/**
* Creates a a QR Code from the given {@link Schema}.
* <p>
* The QRCode will have the following defaults: <br> {size: 100x100}<br>{imageType:PNG} <br><br>
* </p>
* @param schema the schema to encode as QRCode
* @return the QRCode object
*/
public static QRCode from(Schema schema) {
return new QRCode(schema.generateString());
}
/**
* Overrides the imageType from its default {@link net.glxn.qrgen.core.image.ImageType#PNG}
*
* @param imageType the {@link net.glxn.qrgen.core.image.ImageType} you would like the resulting QR to be
* @return the current QRCode object
*/
public QRCode to(ImageType imageType) {
this.imageType = imageType;
return this;
}
/**
* Overrides the size of the qr from its default 125x125
*
* @param width the width in pixels
* @param height the height in pixels
* @return the current QRCode object
*/
public QRCode withSize(int width, int height) {
this.width = width;
this.height = height;
return this;
}
/**
* Overrides the default charset by supplying a {@link com.google.zxing.EncodeHintType#CHARACTER_SET} hint to {@link
* com.google.zxing.qrcode.QRCodeWriter#encode}
*
* @param charset the charset as string, e.g. UTF-8
* @return the current QRCode object
*/
public QRCode withCharset(String charset) {
return withHint(EncodeHintType.CHARACTER_SET, charset);
}
/**
* Overrides the default error correction by supplying a {@link com.google.zxing.EncodeHintType#ERROR_CORRECTION} hint to
* {@link com.google.zxing.qrcode.QRCodeWriter#encode}
*
* @param level the error correction level to use by {@link com.google.zxing.qrcode.QRCodeWriter#encode}
* @return the current QRCode object
*/
public QRCode withErrorCorrection(ErrorCorrectionLevel level) {
return withHint(EncodeHintType.ERROR_CORRECTION, level);
}
/**
* Sets hint to {@link com.google.zxing.qrcode.QRCodeWriter#encode}
*
* @param hintType the hintType to set
* @param value the concrete value to set
* @return the current QRCode object
*/
public QRCode withHint(EncodeHintType hintType, Object value) {
hints.put(hintType, value);
return this;
}
@Override
public File file() {
File file;
try {
file = createTempFile();
MatrixToImageWriter.writeToPath(createMatrix(text), imageType.toString(), file.toPath(), matrixToImageConfig);
} catch (Exception e) {
throw new QRGenerationException("Failed to create QR image from text due to underlying exception", e);
}
return file;
}
@Override
public File file(String name) {
File file;
try {
file = createTempFile(name);
MatrixToImageWriter.writeToPath(createMatrix(text), imageType.toString(), file.toPath(), matrixToImageConfig);
} catch (Exception e) {
throw new QRGenerationException("Failed to create QR image from text due to underlying exception", e);
}
return file;
}
@Override
protected void writeToStream(OutputStream stream) throws IOException, WriterException {
MatrixToImageWriter.writeToStream(createMatrix(text), imageType.toString(), stream, matrixToImageConfig);
}
public File svg() {
File file;
try {
file = createTempSvgFile();
MatrixToSvgWriter.writeToPath(createMatrix(text), file.toPath(), matrixToImageConfig);
} catch (Exception e) {
throw new QRGenerationException("Failed to create QR svg from text due to underlying exception", e);
}
return file;
}
public File svg(String name) {
File file;
try {
file = createTempSvgFile(name);
MatrixToSvgWriter.writeToPath(createMatrix(text), file.toPath(), matrixToImageConfig);
} catch (Exception e) {
throw new QRGenerationException("Failed to create QR svg from text due to underlying exception", e);
}
return file;
}
public void svg(OutputStream outs) {
try {
MatrixToSvgWriter.writeToStream(createMatrix(text), outs, matrixToImageConfig);
} catch (Exception e) {
throw new QRGenerationException("Failed to create QR svg from text due to underlying exception", e);
}
}
private File createTempSvgFile() throws IOException {
return createTempSvgFile("QRCode");
}
private File createTempSvgFile(String name) throws IOException {
File file = File.createTempFile(name, ".svg");
file.deleteOnExit();
return file;
}
public QRCode withColor(int onColor, int offColor) {
matrixToImageConfig = new MatrixToImageConfig(onColor, offColor);
return this;
}
}