-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCodeDotShapeEnum.java
More file actions
355 lines (332 loc) · 14.7 KB
/
CodeDotShapeEnum.java
File metadata and controls
355 lines (332 loc) · 14.7 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.faea.qrcode.constants;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import com.faea.qrcode.model.CodeDotDrawModel;
import com.faea.qrcode.model.CodeDotPoint;
import java.awt.*;
import java.awt.geom.Path2D;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 二维码码点形状枚举
*
* @author liuchao
* @date 2023/2/18
*/
public enum CodeDotShapeEnum {
ROUND("round", "圆") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
int multiple = model.getMultiple();
for (CodeDotPoint point : model.getCodeDotPointList()) {
graphics2D.fillOval(point.getRealX(), point.getRealY(), multiple, multiple);
}
}
}, ROUND_MIN("round_min", "小圆") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
int multiple = model.getMultiple();
int offset = multiple / 5;
int size = multiple - offset * 2;
for (CodeDotPoint point : model.getCodeDotPointList()) {
graphics2D.fillOval(point.getRealX() + offset, point.getRealY() + offset, size, size);
}
}
}, SQUARE("square", "方形") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
int multiple = model.getMultiple();
int offsetX = multiple / 8, offsetY = multiple / 8;
int width = multiple - offsetX * 2, height = multiple - offsetY * 2;
for (CodeDotPoint point : model.getCodeDotPointList()) {
graphics2D.fillRect(point.getRealX() + offsetX, point.getRealY() + offsetY, width, height);
}
}
}, NORMAL("normal", "普通(默认)") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
int multiple = model.getMultiple();
for (CodeDotPoint point : model.getCodeDotPointList()) {
graphics2D.fillRect(point.getRealX(), point.getRealY(), multiple, multiple);
}
}
}, TRIANGLE("triangle", "三角形") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
int multiple = model.getMultiple();
for (CodeDotPoint point : model.getCodeDotPointList()) {
int[] xPoints = {point.getRealX(), point.getRealX() + (multiple >> 1), point.getRealX() + multiple};
int[] yPoints = {point.getRealY() + multiple, point.getRealY(), point.getRealY() + multiple};
graphics2D.fillPolygon(xPoints, yPoints, 3);
}
}
}, ROUNDED("rounded", "圆角矩形") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
int multiple = model.getMultiple();
int arc = multiple / 2;
for (CodeDotPoint point : model.getCodeDotPointList()) {
graphics2D.fillRoundRect(point.getRealX(), point.getRealY(), multiple,
multiple, arc, arc);
}
}
}, DIAMOND("diamond", "菱形") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
int multiple = model.getMultiple();
//x轴和y轴偏移量
int offset = multiple / 10;
// 由于上下左右都需要空出 一个偏移量,所以需要 宽度就需要 减去 两个偏移量
int width = multiple - offset * 2, height = multiple - offset * 2;
for (CodeDotPoint point : model.getCodeDotPointList()) {
int x = point.getRealX() + offset;
int y = point.getRealY() + offset;
int[] xPoints = {x, x + width / 2, x + width, x + width / 2};
int[] yPoints = {y + height / 2, y, y + height / 2, y + height};
graphics2D.fillPolygon(xPoints, yPoints, 4);
}
}
}, TRANSVERSE_STRIPE("transverse_stripe", "横向条纹") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
Map<String, CodeDotPoint> map = Maps.newHashMap();
for (CodeDotPoint point : model.getCodeDotPointList()) {
String key = point.getInputY() + "-" + point.getInputX();
map.put(key, point);
}
int multiple = model.getMultiple();
int offset = multiple / 5;
int h = multiple - 2 * offset;
int arcWidth = h;
int arcHeight = h;
for (int i = 0; i < model.getCodeDotPointList().size(); i++) {
CodeDotPoint point = model.getCodeDotPointList().get(i);
int len = 1;
for (int startX = point.getInputX() + 1; startX < model.getCodeWidth(); startX++) {
CodeDotPoint tempPoint = map.get(point.getInputY() + "-" + startX);
if (null == tempPoint) {
break;
}
len += 1;
}
if (len == 1) {
graphics2D.fillOval(point.getRealX() + offset, point.getRealY() + offset,
multiple - 2 * offset, multiple - 2 * offset);
} else {
int w = multiple * len;
int x = point.getRealX();
int y = point.getRealY() + offset;
// 绘制带有圆角的矩形
graphics2D.fillRoundRect(x, y, w, h, arcWidth, arcHeight);
}
i += (len - 1);
}
}
}, VERTICAL_STRIPE("vertical_stripe", "竖向条纹") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
Map<String, CodeDotPoint> map = Maps.newHashMap();
for (CodeDotPoint point : model.getCodeDotPointList()) {
String key = point.getInputX() + "-" + point.getInputY();
map.put(key, point);
}
int multiple = model.getMultiple();
int offset = multiple / 5;
int w = multiple - 2 * offset;
int arcWidth = w;
int arcHeight = w;
//忽略的点
List<String> ignorePoint = Lists.newArrayList();
for (CodeDotPoint point : model.getCodeDotPointList()) {
String key = point.getInputX() + "-" + point.getInputY();
if (ignorePoint.contains(key)) {
continue;
}
int len = 1;
for (int startY = point.getInputY() + 1; startY < model.getCodeHeight(); startY++) {
key = point.getInputX() + "-" + startY;
if (!map.containsKey(key)) {
break;
}
len += 1;
ignorePoint.add(key);
}
if (len == 1) {
graphics2D.fillOval(point.getRealX() + offset, point.getRealY() + offset, multiple - offset * 2, multiple - offset * 2);
} else {
int h = multiple * len;
int x = point.getRealX() + offset;
int y = point.getRealY();
// 绘制圆角矩形
graphics2D.fillRoundRect(x, y, w, h, arcWidth, arcHeight);
}
}
}
}, STAR("star", "星形") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
int multiple = model.getMultiple();
int width = multiple, height = width;
//圆弧的半径
int radius = width / 2;
for (CodeDotPoint point : model.getCodeDotPointList()) {
int x = point.getRealX();
int y = point.getRealY();
Path2D path = new Path2D.Double();
path.moveTo(x + radius, y);
path.quadTo(x + radius, y + radius, x + width, y + radius);
path.quadTo(x + radius, y + radius, x + radius, y + height);
path.quadTo(x + radius, y + radius, x, y + radius);
path.quadTo(x + radius, y + radius, x + radius, y);
graphics2D.fill(path);
}
}
}, CIRCULAR("circular", "圆形") {
@Override
public void draw(Graphics2D graphics2D, CodeDotDrawModel model) {
double offset = 0.5;
int multiple = model.getMultiple();
double w = multiple - offset * 2;
double h = w;
double arc = multiple / 2;
List<CodeDotPoint> codeDotPointList = model.getCodeDotPointList();
//所有的坐标点
List<String> allPointKeyList = codeDotPointList.stream().map(p -> p.getInputX() + "-" + p.getInputY()).collect(Collectors.toList());
//需要忽略的点
List<CodeDotPoint> ignorePoint = Lists.newArrayList();
for (int i = 0; i < codeDotPointList.size(); i++) {
CodeDotPoint point = codeDotPointList.get(i);
double x = point.getRealX() + offset;
double y = point.getRealY() + offset;
String leftKey = (point.getInputX() - 1) + "-" + point.getInputY();
String topKey = point.getInputX() + "-" + (point.getInputY() - 1);
String rightKey = (point.getInputX() + 1) + "-" + point.getInputY();
String bottomKey = point.getInputX() + "-" + (point.getInputY() + 1);
// 左、右都有点
if (allPointKeyList.contains(leftKey) && allPointKeyList.contains(rightKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x, y);
path.lineTo(x + w, y);
path.lineTo(x + w, y + h);
path.lineTo(x, y + h);
path.closePath();
graphics2D.fill(path);
// 上、下都有点
} else if (allPointKeyList.contains(topKey) && allPointKeyList.contains(bottomKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x, y);
path.lineTo(x + w, y);
path.lineTo(x + w, y + h);
path.lineTo(x, y + h);
path.closePath();
graphics2D.fill(path);
// 右、下都有点
} else if (allPointKeyList.contains(rightKey) && allPointKeyList.contains(bottomKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x, y + h);
path.quadTo(x, y, x + w, y);
path.lineTo(x + w, y + h);
path.closePath();
graphics2D.fill(path);
// 左、下都有点
} else if (allPointKeyList.contains(leftKey) && allPointKeyList.contains(bottomKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x, y);
path.quadTo(x + w, y, x + w, y + h);
path.lineTo(x, y + h);
path.closePath();
graphics2D.fill(path);
// 上、右都有点
} else if (allPointKeyList.contains(topKey) && allPointKeyList.contains(rightKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x + w, y);
path.lineTo(x + w, y + h);
path.quadTo(x, y + h, x, y);
path.closePath();
graphics2D.fill(path);
// 左、上都有点
} else if (allPointKeyList.contains(leftKey) && allPointKeyList.contains(topKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x, y);
path.lineTo(x + w, y);
path.quadTo(x + w, y + h, x, y + h);
path.closePath();
graphics2D.fill(path);
// 上有点
} else if (allPointKeyList.contains(topKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x, y);
path.lineTo(x + w, y);
path.lineTo(x + w, y + h - arc);
path.quadTo(x + w / 2, y + h, x, y + h - arc);
path.closePath();
graphics2D.fill(path);
// 下有点
} else if (allPointKeyList.contains(bottomKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x, y + arc);
path.quadTo(x + w / 2, y, x + w, y + arc);
path.lineTo(x + w, y + h);
path.lineTo(x, y + h);
path.closePath();
graphics2D.fill(path);
// 左有点
} else if (allPointKeyList.contains(leftKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x, y);
path.lineTo(x + w - arc, y);
path.quadTo(x + w, y + h / 2, x + w - arc, y + h);
path.lineTo(x, y + h);
path.closePath();
graphics2D.fill(path);
//右边有点
} else if (allPointKeyList.contains(rightKey)) {
Path2D path = new Path2D.Double();
path.moveTo(x + w, y);
path.lineTo(x + w, y + h);
path.lineTo(x + arc, y + h);
path.quadTo(x, y + h / 2, x + arc, y);
path.closePath();
graphics2D.fill(path);
} else {
graphics2D.fillOval(point.getRealX(), point.getRealY(), multiple, multiple);
}
}
}
};
/**
* 编码
*/
private String code;
private String desc;
/**
* 绘制码点
*
* @param graphics2D
* @param model
* @return void
* @author liuchao
* @date 2023/2/19
*/
public abstract void draw(Graphics2D graphics2D, CodeDotDrawModel model);
CodeDotShapeEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public String getCode() {
return code;
}
public String getDesc() {
return desc;
}
private static Map<String, CodeDotShapeEnum> codeMap = Maps.newHashMap();
static {
for (CodeDotShapeEnum e : CodeDotShapeEnum.values()) {
codeMap.put(e.getCode(), e);
}
}
public static CodeDotShapeEnum getByCode(String code) {
return codeMap.get(code);
}
}