-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataMatrix.java
More file actions
199 lines (154 loc) · 6.77 KB
/
DataMatrix.java
File metadata and controls
199 lines (154 loc) · 6.77 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
package MMS.Project.BCodeGen.Barcode;
import MMS.Project.BCodeGen.IBarcode;
import MMS.Project.BCodeGen.Utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.DataMatrixWriter;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import javafx.scene.control.TextField;
/**
* Created by Paul on 02.06.2017.
*/
public class DataMatrix implements IBarcode {
private final BarcodeFormat BARCODE_TYPE = BarcodeFormat.DATA_MATRIX;
private String data;
private Node properties;
private TextField tfData;
private TextField tfMargin;
private TextField tfBitSize;
private final Color BLACK = Color.BLACK;
private final Color WHITE = Color.WHITE;
private int bitSize = 20;//size of a black/white square
private int multiplier = 2;
public DataMatrix() {
System.out.println("Creating: " + this.getClass().getName());
properties = generateMandatoryProperties(new Insets(5, 5, 5, 5));
}
@Override
public Image runGenerator() throws Exception {
DataMatrixWriter dataMatrixWriter = new DataMatrixWriter();
BitMatrix encoded = dataMatrixWriter.encode(data, BARCODE_TYPE, 1, 1);
int margin = multiplier*bitSize;
//transfer BitMatrix to bigger image
BufferedImage image = new BufferedImage(encoded.getWidth() * bitSize + margin * 2, encoded.getHeight() * bitSize + margin * 2, BufferedImage.TYPE_INT_ARGB);
int black = BLACK.getRGB();
int white = WHITE.getRGB();
Graphics2D g2d = image.createGraphics();
g2d.setPaint(new Color(white));
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
g2d.dispose();
try {
for (int i = 0; i < encoded.getWidth(); i++) {
for (int j = 0; j < encoded.getHeight(); j++) {
Boolean paintBlack = encoded.get(i, j);
for (int x = 0; x < bitSize && i * bitSize + x < image.getWidth(); x++) {
for (int y = 0; y < bitSize && j * bitSize + y < image.getHeight(); y++) {
if (paintBlack) {
image.setRGB(i * bitSize + x + margin, j * bitSize + y + margin, black);
} else {
image.setRGB(i * bitSize + x + margin, j * bitSize + y + margin, white);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
WritableImage barcode = new WritableImage(image.getWidth(), image.getHeight());
SwingFXUtils.toFXImage(image, barcode);
return barcode;
}
@Override
public Node mandatoryProperties() {
return properties;
}
protected Node generateMandatoryProperties(Insets insets) {
Utils.log("First time generating mandatory properties",
Level.SEVERE,
this);
VBox container = new VBox();
container.setPadding(insets);
container.getChildren().add(new Label("Data: "));
tfData = new TextField("MMS 2017");
tfData.setTooltip(new Tooltip("Data for DataMatrix"));
ChangeListener<String> tfDataChanged = new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
data = newValue;
}
};
tfData.textProperty().addListener(tfDataChanged);
tfDataChanged.changed(tfData.textProperty(),
null,
tfData.getText());
container.getChildren().add(tfData);
GridPane gpSettings = new GridPane();
gpSettings.setHgap(insets.getTop());
gpSettings.setVgap(insets.getLeft());
gpSettings.add(generateMarginSettings(insets), 0, 0);
gpSettings.add(generateBitSizeSettings(insets), 1, 0);
// gpSettings.add(generateColorSettings(insets), 1, 1);
// gpSettings.add(generateOptionalSettings(insets), 0, 2);
container.getChildren().add(gpSettings);
return container;
}
private Node generateMarginSettings(Insets insets) {
VBox container = new VBox();
container.getChildren().add(new Label("Margin:"));
tfMargin = new TextField("2");
tfMargin.setTooltip(new Tooltip("Quiet zone around the code in bit. Usually 1-4."));
ChangeListener<String> tfMarginChanged = new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
try {
multiplier = Integer.parseInt(newValue);
} catch (Exception e) {//if empty string or !int
if(!newValue.equals("")) {
tfMargin.setText(oldValue);
}
}
}
};
tfMargin.textProperty().addListener(tfMarginChanged);
container.getChildren().add(tfMargin);
return container;
}
private Node generateBitSizeSettings(Insets insets) {
VBox container = new VBox();
container.getChildren().add(new Label("Bitsize:"));
tfBitSize = new TextField("20");
tfBitSize.setTooltip(new Tooltip("How much pixel should a bit be wide? Higher = better resolution, but also requires more storage. Default 20."));
ChangeListener<String> tfMarginChanged = new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
try {
bitSize = Integer.parseInt(newValue);
} catch (Exception e) {//if empty string or !int
if(!newValue.equals("")) {
tfBitSize.setText(oldValue);
}
}
}
};
tfBitSize.textProperty().addListener(tfMarginChanged);
container.getChildren().add(tfBitSize);
return container;
}
public String toString() {
return "DataMatrix";
}
}