-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerator.java
More file actions
618 lines (567 loc) · 19.3 KB
/
Generator.java
File metadata and controls
618 lines (567 loc) · 19.3 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* Adam Graybill
* June 11, 2025
* QR Code Generator
*
* Sources:
* https://www.thonky.com/qr-code-tutorial
* https://www.youtube.com/watch?v=w5ebcowAJD8
* https://docs.google.com/spreadsheets/d/1kjJSg-Fgdyz4vBqU8iEvL2JYPhMWcL73otAI9s4gj-k/edit?gid=1916601463#gid=1916601463
* https://www.geeksforgeeks.org/how-to-print-colored-text-in-java-console/
* ^ (I have used that source for so many projects. I owe geeksforgeeks my life)
* https://github.com/yansikeim/QR-Code/blob/master/ISO%20IEC%2018004%202015%20Standard.pdf
*/
import java.util.*;
class Generator {
private String url;
private int length;
private int size;
private int version;
private boolean[][] urlBytes;
private boolean[][] codeArray;
private boolean[][] marked;
private int numMarked;
private boolean up;
private int lastRow;
private int lastCol;
private boolean smallEnough;
private int offset;
private boolean[][][] blocks;
public static final int MAXLENGTH = 152;
public static final int MAXVERSION = 8;
public Generator(String url) {
this.url = url;
}
public Generator() {
}
public void setUrl(String newUrl) {
url = newUrl;
}
/**
* Main function that does everything needed to create a QR Code
*/
public void create() {
length = url.length();
if (length > MAXLENGTH) {
smallEnough = false;
return;
}
smallEnough = true;
version = UglyStuff.getVersion(length);
blocks = UglyStuff.initializeBlocks(version);
urlBytes = new boolean[length][8];
size = 17 + (4 * version);
codeArray = new boolean[size][size]; // [y][x] starting in top left
marked = new boolean[size][size];
up = true;
lastRow = size - 1;
lastCol = size - 1;
numMarked = 0; // avoid double counting
offset = 0; // avoid messing up the writing when you hit the vertial timing strip
// Start Drawing things onto the square
alignmentSquares();
if (version >= 7) {
UglyStuff.drawVersionInformation(codeArray, marked, version);
if (version > MAXVERSION) {
return;
}
}
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (marked[row][col]) {
numMarked++;
}
}
}
urlBytes = new boolean[UglyStuff.totBlockWords(version)][8];
writeToBlocks();
interleave(blocks);
writeUrl();
errorCorrection();
mask();
}
/**
* Draws the alignment squares onto the code
*/
private void alignmentSquares() {
UglyStuff.finderPattern(codeArray); // technically these are position squares
UglyStuff.markedAlignment(marked);
if (version > 1) { // these are alignment squares.
UglyStuff.alignmentSquares(codeArray, marked, version);
}
}
/**
* Writes the URL message (including everything before and after) onto the
* code
*/
private void writeUrl() {
for (boolean[] input : urlBytes) {
writeNextByte(input);
}
}
/**
* Converts an integer into a boolean[] byte
*
* @param input Integer to convert
* @return Byte representation
*/
private boolean[] intToBoolArray(int input) {
boolean[] ret = new boolean[8];
for (int tracer = 7; tracer >= 0; tracer--) {
int check = (int) Math.pow(2, tracer);
if (input >= check) {
input -= check;
ret[tracer] = true;
}
}
return ret;
}
/**
* Turns a byte (representated by a boolean array) into an integer
*
* @param input Byte to convert
* @return Integer representation
*/
private int boolArrayToint(boolean[] input) {
int ret = 0;
for (int i = 0; i < 8; i++) {
ret += input[i] ? (int) Math.pow(2, i) : 0;
}
return ret;
}
/**
* Finds the next square to go to given current position, direction, bit,
* etc
*/
private void getNextSquare() {
while (marked[lastRow][lastCol]) {
if (lastCol == 6) {
offset = 1;
lastCol--;
}
if (lastCol % 2 == 0 + offset) {
lastCol--;
} else {
lastCol++;
lastRow += up ? -1 : 1;
}
// too far down
if (lastRow == size) {
lastRow--;
lastCol -= 2;
up = !up;
}
// too far up
if (lastRow <= -1) {
lastRow = 0;
lastCol -= 2;
up = !up;
}
}
}
/**
* Writes the next byte onto the code
*
* @param input Byte to write
*/
private void writeNextByte(boolean[] input) {
for (int i = input.length - 1; i >= 0; i--) {
getNextSquare();
codeArray[lastRow][lastCol] = input[i];
marked[lastRow][lastCol] = true;
numMarked++;
}
}
/**
* Draws a format string onto the QR Code
*
* @param mask The number of the mask in use
*
*/
private void formatString(int mask) {
UglyStuff.drawFormatString(codeArray, mask);
}
/**
* Calulates the score for a given mask as specified by the ISO IEC 18004
*
* @param code A QR Code to grade
* @return The score as given by the criteria in the handbook
*/
private int score(boolean[][] code) { // it would appear something in here doesn't work
int ret = 0;
// Feature 1: strings of five or more same color
int counterx = 1;
int countery = 1;
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (col + 1 < size && code[row][col] == code[row][col + 1]) {
counterx++;
} else {
if (counterx >= 5) {
ret += counterx - 2;
}
counterx = 1;
}
if (col + 1 < size && code[col][row] == code[col + 1][row]) {
countery++;
} else {
if (countery >= 5) {
ret += countery - 2;
}
countery = 1;
}
}
}
// Feature 2: 2x2 squares
for (int row = 0; row < size - 1; row++) {
for (int col = 0; col < size - 1; col++) {
if (code[row][col] == code[row + 1][col] && code[row][col] == code[row][col + 1] && code[row][col] == code[row + 1][col + 1]) {
ret += 3;
}
}
}
// feature 3: 1:1:3:1:1 (d:l:d:l:d) followed/proceeded by 4 light
for (boolean[][] codeCheck : new boolean[][][]{code, rotate(code)}) {
boolean lightBefore;
int[] patternConsts = new int[]{1, 1, 3, 1, 1};
int patternIndex;
int constantMult = 1;
int consecutiveEqual;
for (int row = 0; row < size; row++) {
patternIndex = 0;
consecutiveEqual = 1;
lightBefore = false;
for (int col = 1; col < size; col++) {
if (codeCheck[row][col] == codeCheck[row][col - 1]) {
consecutiveEqual++;
} else {
if (!codeCheck[row][col] && patternIndex == 0) {
constantMult = consecutiveEqual;
patternIndex++;
consecutiveEqual = 1;
continue;
}
if (patternIndex != 0) {
if (patternConsts[patternIndex] == constantMult * consecutiveEqual) {
patternIndex++;
if (patternIndex == 4) {
int scale = 0;
scale += lightBefore ? 1 : 0;
scale += col + 4 < size && nextFourFalse(row, col, codeCheck) ? 1 : 0;
ret += scale * 40;
patternIndex = 0;
}
} else {
if (!codeCheck[row][col]) {
constantMult = consecutiveEqual;
patternIndex = 1;
lightBefore = false;
} else {
patternIndex = 0;
lightBefore = consecutiveEqual >= 4;
}
}
}
consecutiveEqual = 1;
}
}
}
}
// feature 4: Ratio of light to dark squares
double dark = 0;
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
dark += code[row][col] ? 1 : 0;
}
}
int percentage = (int) ((dark / (size * size)) * 100);
percentage = Math.abs(percentage - 50);
ret += 10 * (percentage / 5);
return ret;
}
/**
* Returns if the next four square are all light Must not give input such
* that four additional squares would go out of bounds
*
* @param row row index of the starting square
* @param col column index of the starting square
* @param code QR Code as a 2D boolean array
* @return true if the next four squares are light. false if not
*/
private boolean nextFourFalse(int row, int col, boolean[][] code) {
for (int i = 1; i <= 4; i++) {
if (code[row][col + i]) {
return false;
}
}
return true;
}
/**
* Rotates an array 90˚ Counter-Clockwise
*
* @param code
* @return
*/
private boolean[][] rotate(boolean[][] code) {
boolean[][] ret = new boolean[code.length][code.length];
for (int i = 0; i < code.length; i++) {
for (int j = 0; j < code.length; j++) {
ret[i][j] = code[j][code.length - i - 1];
}
}
return ret;
}
/**
* Draws a mask onto an array
*
* @param array Given boolean[][] representation of a QR Code
* @param pattern Mask number as given by the ISO IEC 18004
* @param untouched Array of squares not to touch (alignment patters, timing
* strips, etc)
* @return Array with the mask drawn on
*/
private boolean[][] maskArray(boolean[][] array, int pattern, boolean[][] untouched) {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (untouched[row][col]) {
continue;
}
if (UglyStuff.maskPatternEval(pattern, row, col)) {
array[row][col] = !codeArray[row][col];
} else {
array[row][col] = codeArray[row][col];
}
}
}
return array;
}
/**
* Calulates the mask with the lowest score, implements it onto the code,
* and adds the format string
*/
private void mask() {
boolean[][] untouched = new boolean[size][size];
boolean[][] blankTestArray = new boolean[size][size];
UglyStuff.markedAlignment(untouched);
UglyStuff.finderPattern(blankTestArray);
if (version > 1) {
UglyStuff.alignmentSquares(blankTestArray, untouched, version);
}
if (version >= 7) {
UglyStuff.markVersionInformation(untouched);
UglyStuff.drawVersionInformation(blankTestArray, untouched, version);
}
int[] scores = new int[8];
for (int pattern = 0; pattern < 8; pattern++) {
boolean[][] arrayForScoring = maskArray(blankTestArray, pattern, untouched);
UglyStuff.drawFormatString(arrayForScoring, pattern);
scores[pattern] = score(arrayForScoring);
}
int lowestPattern = 0;
int lowestScore = Integer.MAX_VALUE;
for (int i = 0; i < 8; i++) {
if (scores[i] < lowestScore) {
lowestScore = scores[i];
lowestPattern = i;
}
}
codeArray = maskArray(codeArray, lowestPattern, untouched);
formatString(lowestPattern);
}
/**
* Reverses a boolean[]
*
* @param toReverse Input array
* @return Reversed array
*/
private boolean[] reverse(boolean[] toReverse) {
boolean[] ret = new boolean[toReverse.length];
for (int i = 0; i < toReverse.length; i++) {
ret[i] = toReverse[toReverse.length - i - 1];
}
return ret;
}
/**
* Writes the URL into blocks as specified by the version
*/
private void writeToBlocks() {
ArrayList<Boolean> allbits = new ArrayList<>(Arrays.asList(false, true, false, false));
for (boolean b : reverse(intToBoolArray(length))) {
allbits.add(b);
}
for (int c = 0; c < length; c++) {
int character = url.charAt(c);
boolean[] charBoolArray = reverse(intToBoolArray(character));
for (boolean b : charBoolArray) {
allbits.add(b);
}
}
for (int i = 0; i < 4; i++) {
allbits.add(false);
}
boolean parity = true;
while (allbits.size() <= 8 * UglyStuff.totBlockWords(version)) {
for (boolean b : reverse(intToBoolArray(parity ? 236 : 17))) {
allbits.add(b);
}
parity = !parity;
}
boolean[] hold = new boolean[8];
int blockNum = 0;
int blockLength = blocks[0].length;
int numBlocksAdded = 0;
int holdIndex = 7;
while (!allbits.isEmpty()) {
if (holdIndex == -1) {
blocks[blockNum][numBlocksAdded] = hold;
numBlocksAdded++;
hold = new boolean[8];
holdIndex = 7;
}
hold[holdIndex] = allbits.removeFirst();
holdIndex--;
if (numBlocksAdded == blockLength) {
blockNum++;
if (blockNum < blocks.length) {
blockLength = blocks[blockNum].length;
}
numBlocksAdded = 0;
}
}
}
/**
* Interleaves bytes
*
* @param toInterleave Blocks of boolean[] representations of bytes
*/
private void interleave(boolean[][][] toInterleave) {
int tracer = 0;
for (int i = 0; i < toInterleave[toInterleave.length - 1].length; i++) { // same number of loops as codewords per block
for (boolean[][] block : toInterleave) { // loop through each block
if (i >= block.length) {
continue;
}
urlBytes[tracer] = block[i];
tracer++;
}
}
}
/**
* Interleaves the remainders one byte from each block after another
*
* @param remainders Coefficient remainders broken into blocks as
* specificied by the version
* @return Remainders interwoven into one stream of integers
*/
private int[] interleaveRemainders(int[][] remainders) {
int[] ret = new int[remainders.length * remainders[0].length];
int tracer = 0;
for (int i = 0; i < remainders[0].length; i++) {
for (int[] remainder : remainders) {
ret[tracer] = remainder[i];
tracer++;
}
}
return ret;
}
/**
* Writes the error correction bits onto the Code
*
* @param remainder Integer coefficients of the remainder as given by the
* polynomial division
*/
private void writeErrorCorrection(int[] remainder) {
for (int i = 0; i < remainder.length; i++) {
int input = remainder[i];
if (input == -1) {
continue;
}
boolean[] booleanRepresentation = intToBoolArray(input);
writeNextByte(booleanRepresentation);
}
while (numMarked < Math.pow(size, 2)) {
writeNextByte(new boolean[]{false});
}
}
/**
* Adds calculates and writes the error correction bytes onto the code
*/
private void errorCorrection() {
int[][] remainders = UglyStuff.initializeRemainderBlocks(version);
for (int i = 0; i < blocks.length; i++) {
int[] coefficients = new int[blocks[i].length];
for (int j = 0; j < coefficients.length; j++) {
coefficients[j] = boolArrayToint(blocks[i][j]);
}
remainders[i] = UglyStuff.longDivision(coefficients, version);
}
int[] mergedRemainders = interleaveRemainders(remainders);
writeErrorCorrection(mergedRemainders);
}
//
// \u001B[0m reset
// \u001B[40m black
// \u001B[41m red
// \u001B[42m green
// \u001B[43m yellow
// \u001B[44m blue
// \u001B[45m purple
// \u001B[46m cyan
// \u001B[47m white
/**
* Turns a boolean[][] into a string that shows a QR code when printed
*
* @param toPrint Your boolean[][] representation of a Code
* @return String representation of a QR Code
*/
protected String printArray(boolean[][] toPrint) {
final String on = "\u001B[47m";
final String off = "\u001B[40m";
final String reset = "\u001B[0m";
String ret = "";
String blank = on + " " + reset;
for (int i = 0; i < toPrint.length + 2; i++) {
ret += blank;
}
ret += "\n";
int rowIndex = 0;
for (boolean[] row : toPrint) {
ret += blank;
int colIndex = 0;
for (boolean square : row) {
String color = "";
if (marked[rowIndex][colIndex]) {
color = square ? off : on;
}
ret += color + " " + reset;
colIndex++;
}
ret += blank + "\n";
rowIndex++;
}
for (int i = 0; i < toPrint.length + 2; i++) {
ret += blank;
}
return ret;
}
@Override
public String toString() {
if (!smallEnough) {
return String.format("That URL is too long. This generator only supports URLs up to %d characters long", MAXLENGTH);
}
// TODO: Find a way to make a jpeg or something
// https://stackoverflow.com/questions/2407113/open-source-image-processing-lib-in-java
if (version > 9) {
return "The QR Code is too large to fit on the screen.";
}
return printArray(codeArray);
}
public static void main(String[] args) {
try (Scanner s = new Scanner(System.in)) {
System.out.print("Enter your URL: ");
Generator g = new Generator(s.nextLine());
g.create();
System.out.println(g);
}
}
}