forked from aboullaite/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQrCode-generation.java
More file actions
141 lines (116 loc) · 4.93 KB
/
QrCode-generation.java
File metadata and controls
141 lines (116 loc) · 4.93 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
import com.google.zxing.BarcodeFormat;
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.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class QrCode {
private final String DIR = "/directory/to/save/images";
private final String ext = ".png";
private final String LOGO = "logo_url";
private final String CONTENT = "some content here";
private final int WIDTH = 300;
private final int HEIGHT = 300;
public void generate() {
// Create new configuration that specifies the error correction
Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
// init directory
cleanDirectory(DIR);
initDirectory(DIR);
// Create a qr code with the url as content and a size of WxH px
bitMatrix = writer.encode(CONTENT, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
// Load QR image
BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix, getMatrixConfig());
// Load logo image
BufferedImage overly = getOverly(LOGO);
// Calculate the delta height and width between QR code and logo
int deltaHeight = qrImage.getHeight() - overly.getHeight();
int deltaWidth = qrImage.getWidth() - overly.getWidth();
// Initialize combined image
BufferedImage combined = new BufferedImage(qrImage.getHeight(), qrImage.getWidth(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) combined.getGraphics();
// Write QR code to new image at position 0/0
g.drawImage(qrImage, 0, 0, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
// Write logo into combine image at position (deltaWidth / 2) and
// (deltaHeight / 2). Background: Left/Right and Top/Bottom must be
// the same space for the logo to be centered
g.drawImage(overly, (int) Math.round(deltaWidth / 2), (int) Math.round(deltaHeight / 2), null);
// Write combined image as PNG to OutputStream
ImageIO.write(combined, "png", os);
// Store Image
Files.copy( new ByteArrayInputStream(os.toByteArray()), Paths.get(DIR + generateRandoTitle(new Random(), 9) +ext), StandardCopyOption.REPLACE_EXISTING);
} catch (WriterException e) {
e.printStackTrace();
//LOG.error("WriterException occured", e);
} catch (IOException e) {
e.printStackTrace();
//LOG.error("IOException occured", e);
}
}
private BufferedImage getOverly(String LOGO) throws IOException {
URL url = new URL(LOGO);
return ImageIO.read(url);
}
private void initDirectory(String DIR) throws IOException {
Files.createDirectories(Paths.get(DIR));
}
private void cleanDirectory(String DIR) {
try {
Files.walk(Paths.get(DIR), FileVisitOption.FOLLOW_LINKS)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
// Directory does not exist, Do nothing
}
}
private MatrixToImageConfig getMatrixConfig() {
// ARGB Colors
// Check Colors ENUM
return new MatrixToImageConfig(QrCode.Colors.WHITE.getArgb(), QrCode.Colors.ORANGE.getArgb());
}
private String generateRandoTitle(Random random, int length) {
return random.ints(48, 122)
.filter(i -> (i < 57 || i > 65) && (i < 90 || i > 97))
.mapToObj(i -> (char) i)
.limit(length)
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
.toString();
}
public enum Colors {
BLUE(0xFF40BAD0),
RED(0xFFE91C43),
PURPLE(0xFF8A4F9E),
ORANGE(0xFFF4B13D),
WHITE(0xFFFFFFFF),
BLACK(0xFF000000);
private final int argb;
Colors(final int argb){
this.argb = argb;
}
public int getArgb(){
return argb;
}
}
}