-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOutput.java
More file actions
89 lines (81 loc) · 3.16 KB
/
Output.java
File metadata and controls
89 lines (81 loc) · 3.16 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
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.*;
public class Output {
private Output() {
}
/**
* Turns a boolean[][] into a string that shows a QR code when printed
*
* @param toPrint Your square boolean[][] representation of a QR Code
* @param markedArray A square boolean[][] of the same size with every
* position of the toPrint array you want printed marked as true
* @return String represenation of a QR Code
*/
public static String printArray(boolean[][] toPrint, boolean[][] markedArray, final String on, final String off) {
if (toPrint.length != markedArray.length || toPrint[0].length != markedArray[0].length) {
return "Invalid markedArray; Both arrays must be of the same size";
}
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 (markedArray[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;
}
/**
* Creates a PNG of your QR Code
*
* @param code Boolean array representation of your QR Code
* @param filePath Relative filepath from the directory you run this program
* in
* @param name The name you want for the png. If you don't end the name with
* ".png", it will append it automatically
* @throws IOException Consequence of calling a python program internally
*/
public static void convertToPNG(boolean[][] code, Path filePath, String name) throws IOException {
String path = System.getProperty("user.dir") + filePath.toString();
if (path.charAt(path.length() - 1) != '/') {
path += "/";
}
path += name;
path += name.substring(name.length() - 4).equalsIgnoreCase(".png") ? "" : ".png";
System.out.println(path);
ImageGenerator ig = new ImageGenerator(path, code);
ig.drawCode();
}
public static void convertToCSV(boolean[][] code, Path filePath, String name) throws IOException {
String newFilePath = "/newCode" + name + (name.substring(name.length() - 4).equalsIgnoreCase(".csv") ? "" : ".csv");
File newFile = new File(System.getProperty("user.dir") + filePath.toString(), newFilePath);
try (FileWriter newFileWriter = new FileWriter(newFile)) {
for (boolean[] line : code) {
String thisLine = "";
for (boolean b : line) {
thisLine += (b ? '1' : '0') + ",";
}
newFileWriter.write(thisLine + '\n');
}
}
}
}