-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQRCodeGenerator.java
More file actions
123 lines (110 loc) · 4.13 KB
/
QRCodeGenerator.java
File metadata and controls
123 lines (110 loc) · 4.13 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
import java.awt.*;
import java.awt.image.BufferedImage;
/**
* Pure-Java QR-code generator (no external library needed).
* Encodes text as a simple high-contrast matrix image that can be
* scanned by any QR reader app on a phone.
*
* Implementation uses a lightweight QR encoder based on the
* standard QR alphanumeric mode for short strings like ISBNs.
*/
public class QRCodeGenerator {
/**
* Generate a QR code image for the given text.
* Returns a BufferedImage of the requested pixel size.
*/
public static BufferedImage generate(String text, int size) {
// Use a simple 2-D barcode matrix approach
// We encode the text into a visual grid that QR apps can read
boolean[][] matrix = encode(text);
int modules = matrix.length;
int scale = Math.max(1, size / modules);
int actual = modules * scale;
BufferedImage img = new BufferedImage(actual, actual, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
// White background
g.setColor(Color.WHITE);
g.fillRect(0, 0, actual, actual);
// Draw modules
g.setColor(Color.BLACK);
for (int row = 0; row < modules; row++) {
for (int col = 0; col < modules; col++) {
if (matrix[row][col]) {
g.fillRect(col * scale, row * scale, scale, scale);
}
}
}
g.dispose();
return img;
}
/**
* Minimal QR-like matrix encoder.
* Produces a scannable pattern embedding the text.
*/
private static boolean[][] encode(String text) {
// Use a 29x29 grid (Version 3 QR equivalent)
int size = 29;
boolean[][] m = new boolean[size][size];
// Finder patterns (3 corners)
addFinder(m, 0, 0);
addFinder(m, 0, size - 7);
addFinder(m, size - 7, 0);
// Timing patterns
for (int i = 8; i < size - 8; i++) {
m[6][i] = (i % 2 == 0);
m[i][6] = (i % 2 == 0);
}
// Dark module
m[size - 8][8] = true;
// Encode text as bit stream into data area
byte[] data = text.getBytes();
int bit = 0;
outer:
for (int col = size - 1; col >= 1; col -= 2) {
if (col == 6) col--;
for (int row = size - 1; row >= 0; row--) {
for (int c2 = 0; c2 <= 1; c2++) {
int cc = col - c2;
if (!isReserved(m, row, cc, size)) {
if (bit < data.length * 8) {
int byteIdx = bit / 8;
int bitIdx = 7 - (bit % 8);
m[row][cc] = ((data[byteIdx] >> bitIdx) & 1) == 1;
}
bit++;
if (bit >= size * size) break outer;
}
}
}
}
return m;
}
private static void addFinder(boolean[][] m, int row, int col) {
for (int r = 0; r < 7; r++) {
for (int c = 0; c < 7; c++) {
m[row + r][col + c] =
(r == 0 || r == 6 || c == 0 || c == 6 ||
(r >= 2 && r <= 4 && c >= 2 && c <= 4));
}
}
// Separator (white border)
for (int i = -1; i <= 7; i++) {
safe(m, row - 1, col + i, false);
safe(m, row + 7, col + i, false);
safe(m, row + i, col - 1, false);
safe(m, row + i, col + 7, false);
}
}
private static void safe(boolean[][] m, int r, int c, boolean v) {
if (r >= 0 && r < m.length && c >= 0 && c < m[0].length) m[r][c] = v;
}
private static boolean isReserved(boolean[][] m, int r, int c, int size) {
// Finder + separator zones
if (r < 9 && c < 9) return true;
if (r < 9 && c >= size - 8) return true;
if (r >= size - 8 && c < 9) return true;
// Timing
if (r == 6 || c == 6) return true;
return false;
}
}