-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarcode scanner.txt
342 lines (285 loc) · 10.6 KB
/
barcode scanner.txt
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
/**
* Program: Barcode Program
* Description:
* - BarcodeIO: An interface which has different methods for scanning, reading text, generating and translating images from text, along with displaying texts and images.
*
* - BarcodeImage: Our class which is our 2D barcode pattern. It contains different methods to make a image using
* the string data also using accessors and modifiers.
* - InfoBox: Our BarcodeIO interface.
*/
// Phase 1:
public interface BarcodeIO {
//scanning and storing images ^_^
public boolean scan(BarcodeImage bc);
//Reading our texts
public boolean readText(String text);
//Generating images from the stored text
public boolean generateImageFromText();
// Translating our stored image to text
public boolean translateImageToText();
//Displaying the stored text to the console
public void displayTextToConsole();
// Displaying the stored image to the console
public void displayImageToConsole();
}
// Phase 2:
public class BarcodeImage implements Cloneable {
// Constants for the maximum height and width
public static final int MAX_HEIGHT = 30;
public static final int MAX_WIDTH = 65;
// Store Image
private boolean[][] imageData;
//Constructor
public BarcodeImage() {
// Instantiate a 2D array and fill it with blanks (false)
imageData = new boolean[MAX_HEIGHT][MAX_WIDTH];
}
//Constructor with Parameters
public BarcodeImage(String[] strData) {
this(); // Call the default constructor to initialize imageData :o
// Check and pack the incoming data into the lower-left corner of the array
if (strData != null && strData.length <= MAX_HEIGHT) {
int rows = strData.length;
int cols = strData[0].length(); // Assuming all strings have the same length
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols && col < MAX_WIDTH; col++) {
// Convert '1' to true (black) and '0' to false (white)
imageData[MAX_HEIGHT - 1 - row][col] = (strData[row].charAt(col) == '1');
}
}
}
}
//Accessor to get value of a pixel
public boolean getPixel(int row, int col) {
if (row >= 0 && row < MAX_HEIGHT && col >= 0 && col < MAX_WIDTH) {
return imageData[row][col];
}
return false; // Return false for out-of-bounds access
}
// Mutator to set the value of a pixel
public boolean setPixel(int row, int col, boolean value) {
if (row >= 0 && row < MAX_HEIGHT && col >= 0 && col < MAX_WIDTH) {
imageData[row][col] = value;
return true;
}
return false; // Return false for out-of-bounds access
}
// Private method to checking the size of incoming data
private boolean checkSize(String[] data) {
return data != null && data.length <= MAX_HEIGHT;
}
// Optional - Displaying our image to the console (debugging)
public void displayToConsole() {
for (int row = 0; row < MAX_HEIGHT; row++) {
for (int col = 0; col < MAX_WIDTH; col++) {
System.out.print(imageData[row][col] ? '*' : ' ');
}
System.out.println();
}
}
// Cloning our method for a copy.
@Override
public BarcodeImage clone() {
try {
BarcodeImage clone = (BarcodeImage) super.clone();
// Deep copy of imageData
clone.imageData = new boolean[MAX_HEIGHT][MAX_WIDTH];
for (int row = 0; row < MAX_HEIGHT; row++) {
System.arraycopy(this.imageData[row], 0, clone.imageData[row], 0, MAX_WIDTH);
}
return clone;
} catch (CloneNotSupportedException e) {
// Should not happen since BarcodeImage implements Cloneable
return null;
}
}
}
// Phase 3:
public class InfoBox implements BarcodeIO {
// Constants for characters
public static final char BLACK_CHAR = '*';
public static final char WHITE_CHAR = ' ';
// Internal data
private BarcodeImage image;
private String text;
private int actualWidth;
private int actualHeight;
// Constructors
public InfoBox() {
// Default Constructor
image = new BarcodeImage();
text = "undefined";
actualWidth = 0;
actualHeight = 0;
}
public InfoBox(BarcodeImage image) {
// Constructor with BarcodeImage
this();
scan(image);
}
public InfoBox(String text) {
// Constructor with text
this();
readText(text);
}
// Mutators
@Override
public boolean scan(BarcodeImage image) {
try {
this.image = image.clone(); // Use clone to create a deep copy
actualWidth = computeSignalWidth();
actualHeight = computeSignalHeight();
return true;
} catch (CloneNotSupportedException e) {
// Do nothing in case of CloneNotSupportedException
return false;
}
}
@Override
public boolean readText(String text) {
this.text = text;
return true;
}
@Override
public boolean generateImageFromText() {
clearImage(); // Clear the existing image
// Looping through each character in the text
for (int col = 0; col < text.length(); col++) {
char currentChar = text.charAt(col);
// Use helper method to write the character to the corresponding column in the image
writeCharToCol(col, currentChar);
}
// Setting the actualWidth and actualHeight based on image
actualWidth = computeSignalWidth();
actualHeight = computeSignalHeight();
return true;
}
@Override
public boolean translateImageToText() {
StringBuilder translatedText = new StringBuilder();
// Looping through each column in the image
for (int col = 0; col < actualWidth; col++) {
// Reading the character from the exact column in the image
char currentChar = readCharFromCol(col);
translatedText.append(currentChar);
}
// Set the translated text to the internal text
text = translatedText.toString();
return false;
}
// Accessors
public int getActualWidth() {
return actualWidth;
}
public int getActualHeight() {
return actualHeight;
}
// Private methods
private int computeSignalWidth() {
// Compute actualWidth based on the left BLACK spine
int col = 0;
while (col < BarcodeImage.MAX_WIDTH && image.getPixel(BarcodeImage.MAX_HEIGHT - 1, col)) {
col++;
}
return col;
}
private int computeSignalHeight() {
// Compute actualHeight based on the bottom BLACK spine
int row = 0;
while (row < BarcodeImage.MAX_HEIGHT && image.getPixel(row, 0)) {
row++;
}
return row;
}
// Display methods
@Override
public void displayTextToConsole() {
System.out.println(text);
}
@Override
public void displayImageToConsole() {
for (int row = BarcodeImage.MAX_HEIGHT - actualHeight; row < BarcodeImage.MAX_HEIGHT; row++) {
for (int col = 0; col < actualWidth; col++) {
System.out.print(image.getPixel(row, col) ? BLACK_CHAR : WHITE_CHAR);
}
System.out.println();
}
System.out.println("".equals(text) ? "" : new String(new char[actualWidth]).replace('\0', '-'));
}
// Optional method for debugging
public void displayRawImage() {
image.displayToConsole();
}
// Optional method to clear the image
private void clearImage() {
image = new BarcodeImage();
}
// Main method for testing
public static void main(String[] args) {
String[] sImageIn = {
"* * * * * * * * * * * * * * *",
"* *",
"********** *** *** ******* ",
"* ***************************",
"** * * * * * * * ",
"* ** ** ** ** *",
"****** **** ** * ** *** ",
"**** ** * * * ** *",
"*** * * *** * * ******** ",
"*****************************"
};
String[] sImageIn_2 = {
"* * * * * * * * * * * * * * *",
"* *",
"*** ** ******** ** ***** *** ",
"* **** ***************** ***",
"* * * * * * * * ",
"* ** **** * **",
"* * **** ** * * * *** ",
"*** *** * ** * **",
"*** * ** * ** * ** * ",
"*****************************"
};
BarcodeImage bc = new BarcodeImage(sImageIn);
InfoBox dm = new InfoBox(bc);
// First secret message
dm.translateImageToText();
dm.displayTextToConsole();
dm.displayImageToConsole();
// Second secret message
bc = new BarcodeImage(sImageIn_2);
dm.scan(bc);
dm.translateImageToText();
dm.displayTextToConsole();
dm.displayImageToConsole();
// Create your own message
dm.readText("What a great resume builder this is!");
dm.generateImageFromText();
dm.displayTextToConsole();
dm.displayImageToConsole();
}
}
//Output:
*** * ** ** * *** * * *** * * ** ** *
What a great resume builder this is!
**********************************
* *
**** * ***** **** **** ******
* *** ***************** ******
* * ** * * * * * **
* * * * ** * * * ****
* * * ** * * * * ** *
** * *** ***** ** * * **
**** * **** ** * * * * **
**********************************
What a great resume builder this is!
**********************************
* *
**** * ***** **** **** ******
* *** ***************** ******
* * ** * * * * * **
* * * * ** * * * ****
* * * ** * * * * ** *
** * *** ***** ** * * **
**** * **** ** * * * * **
**********************************