-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
88 lines (70 loc) · 3.12 KB
/
Main.java
File metadata and controls
88 lines (70 loc) · 3.12 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
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("\nPlease enter the text:\n");
String data = scanner.nextLine();
System.out.println("\nPlease enter a name for the QR:\n");
String qrName = scanner.next();
System.out.println("\nPlease enter the error correction level (1 - 4):");
int errorCorrectionLevel = scanner.nextInt();
scanner.close();
if(errorCorrectionLevel < 1 || errorCorrectionLevel > 4) throw new Exception("Please eneter a valid error correction level number (1 - 4)! Try again.");
QR qr = new QR(data, errorCorrectionLevel);
Packed resultQRPacked = qr.generateQR();
int width = resultQRPacked.size + 10;
int height = resultQRPacked.size + 10;
int sizeMultiplayer;
if (resultQRPacked.size < 15) {
sizeMultiplayer = 10;
} else {
sizeMultiplayer = 5;
}
BufferedImage bufferedImage = new BufferedImage(width * sizeMultiplayer, height * sizeMultiplayer, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, width * sizeMultiplayer, height * sizeMultiplayer);
if (resultQRPacked.state >= 0) {
for (int x = 0; x < resultQRPacked.size; x++) {
for (int y = 0; y < resultQRPacked.size; y++) {
if (resultQRPacked.matrix[x][y]) {
g2d.setColor(Color.black);
} else {
g2d.setColor(Color.white);
}
g2d.fillRect((x + 5) * sizeMultiplayer, (y + 5) * sizeMultiplayer, sizeMultiplayer, sizeMultiplayer);
}
}
} else {
System.out.println("build faild with output: " + Integer.toString(resultQRPacked.state));
Exception e = new Exception("An exception occurred!");
throw e;
}
g2d.dispose();
File dir = new File(System.getProperty("user.dir") + "\\generated images");
File file = new File(dir.getAbsolutePath() + "\\" + qrName + ".png");
try {
dir.mkdir();
file.createNewFile();
ImageIO.write(bufferedImage, "png", file);
} catch (IOException e) {
throw new IOException("An error occurred while trying to create the image png file!");
}
JFrame f = new JFrame(qrName);
JLabel label = new JLabel(new ImageIcon(file.getAbsolutePath()));
label.setBounds(-4, -4, width * sizeMultiplayer + 10, height * sizeMultiplayer + 10);
f.add(label);
f.setSize(width * sizeMultiplayer + 14 , height * sizeMultiplayer + 40);
f.setLayout(null);
f.setVisible(true);
}
}