-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSimpleBarcodeTester.java
More file actions
368 lines (304 loc) · 14.3 KB
/
SimpleBarcodeTester.java
File metadata and controls
368 lines (304 loc) · 14.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
/*
* Copyright (C) 2014 karthik
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.*;
import karthik.Barcode.*;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.highgui.VideoCapture;
/**
*
* @author karthik
*/
public class SimpleBarcodeTester {
private static String fileSeparator = System.getProperty("file.separator");
private static String lineSeparator = System.getProperty("line.separator");
private static boolean IS_VIDEO = false;
private static boolean IS_CAMERA = false;
private static boolean SHOW_INTERMEDIATE_STEPS = false;
private static boolean showImages = true;
private static String imgFile;
private static VideoCapture video;
private static int CV_CAP_PROP_FPS = 5;
private static int CV_CAP_PROP_POS_FRAMES = 1;
private static int CV_FRAME_COUNT = 7;
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.loadLibrary("opencv_ffmpeg249_64");
}
public static void main(String[] args) {
Map<CharSequence, BarcodeLocation> results = new HashMap<>();
show_usage_syntax();
parse_args(args);
if (IS_VIDEO && IS_CAMERA)
IS_VIDEO = false;
if (!IS_VIDEO && !IS_CAMERA)
process_image();
if (IS_VIDEO) {
video = new VideoCapture(imgFile);
results = processVideo(imgFile);
if (results.size() == 0)
System.out.println("No results found");
}
if (IS_CAMERA) {
video = new VideoCapture(0);
video.open(0);
if (video.isOpened())
System.out.println("Camera is open");
results = processCamera("Camera feed");
if (results.size() == 0)
System.out.println("No results found");
}
Mat image = new Mat();
for (CharSequence result : results.keySet()) {
BarcodeLocation resultLoc = results.get(result);
System.out.print("Found " + result + " Location - " + resultLoc.toString());
System.out.print(lineSeparator + lineSeparator);
if (showImages) {
video.set(CV_CAP_PROP_POS_FRAMES, resultLoc.frame);
video.read(image);
Point[] rectPoints = resultLoc.coords;
Scalar colour = new Scalar(0, 0, 255);
for (int j = 0; j < 3; j++)
Core.line(image, rectPoints[j], rectPoints[j + 1], colour, 2, Core.LINE_AA, 0);
Core.line(image, rectPoints[3], rectPoints[0], colour, 2, Core.LINE_AA, 0);
ImageDisplay.showImageFrame(image, "Barcode text - " + result);
}
}
if(IS_CAMERA)
video.release();
}
private static Map<CharSequence, BarcodeLocation> processVideo(String filename) {
double frames_per_second;
int frame_count;
Mat image = new Mat();
Barcode barcode = null;
ImageDisplay videoDisp = null;
Map<CharSequence, BarcodeLocation> foundCodes = new HashMap<>();
try {
frames_per_second = video.get(CV_CAP_PROP_FPS);
frame_count = (int) video.get(CV_FRAME_COUNT);
System.out.println("FPS is " + frames_per_second);
System.out.println("Frame count is " + frame_count);
video.read(image);
if(showImages)
videoDisp = ImageDisplay.getImageFrame(image, "Video Frames");
for (int i = 0; i < frame_count; i += (frames_per_second / 3.0)) {
video.set(CV_CAP_PROP_POS_FRAMES, i);
video.read(image);
String imgName = filename + "_Frame_" + i;
if (barcode == null)
barcode = new MatrixBarcode(imgName, image, TryHarderFlags.VERY_SMALL_MATRIX);
else{
if(!Barcode.updateImage(barcode, image, imgName))
barcode = new MatrixBarcode(imgName, image, TryHarderFlags.VERY_SMALL_MATRIX);
}
// locateBarcode() returns a List<CandidateResult> with all possible candidate barcode regions from
// within the image. These images then get passed to a decoder(we use ZXing here but could be any decoder)
List<CandidateResult> results = barcode.locateBarcode();
String imgFile = barcode.getName();
Map<CharSequence, BarcodeLocation> frame_results = decodeBarcodeFromVideo(results, i);
foundCodes.putAll(frame_results);
System.out.print("Processed frame " + i + "- Found " + frame_results.size() + " results\r");
for (BarcodeLocation bl : frame_results.values()) {
Point[] rectPoints = bl.coords;
Scalar colour = new Scalar(255, 0, 0);
for (int j = 0; j < 3; j++)
Core.line(image, rectPoints[j], rectPoints[j + 1], colour, 2, Core.LINE_AA, 0);
Core.line(image, rectPoints[3], rectPoints[0], colour, 2, Core.LINE_AA, 0);
}
if(showImages)
videoDisp.updateImage(image, "Video frame " + i);
}
if(showImages)
videoDisp.close();
} catch (IOException ioe) {
System.out.println("IO Exception when finding barcode " + ioe.getMessage());
}
return foundCodes;
}
private static Map<CharSequence, BarcodeLocation> processCamera(String caption) {
double frames_per_second;
int frame_count;
Mat image = new Mat();
Barcode barcode = null;
Map<CharSequence, BarcodeLocation> foundCodes = new HashMap<>();
try {
frames_per_second = video.get(CV_CAP_PROP_FPS);
frame_count = (int) video.get(CV_FRAME_COUNT);
System.out.println("FPS is " + frames_per_second);
System.out.println("Frame count is " + frame_count);
video.read(image);
ImageDisplay videoDisp = ImageDisplay.getImageFrame(image, "Video Frames");
long end_time = System.currentTimeMillis() + 240000;
int i = 0;
while (System.currentTimeMillis() < end_time) {
i += 1;
video.read(image);
String imgName = caption + "_" + System.currentTimeMillis();
if (barcode == null)
barcode = new MatrixBarcode(imgName, image, TryHarderFlags.VERY_SMALL_MATRIX);
else{
if(!Barcode.updateImage(barcode, image, imgName))
barcode = new MatrixBarcode(imgName, image, TryHarderFlags.VERY_SMALL_MATRIX);
}
// locateBarcode() returns a List<CandidateResult> with all possible candidate barcode regions from
// within the image. These images then get passed to a decoder(we use ZXing here but could be any decoder)
List<CandidateResult> results = barcode.locateBarcode();
String imgFile = barcode.getName();
Map<CharSequence, BarcodeLocation> frame_results = decodeBarcodeFromVideo(results, i);
foundCodes.putAll(frame_results);
System.out.println("Processed frame " + i + "- Found " + frame_results.size() + " results");
for (BarcodeLocation bl : frame_results.values()) {
Point[] rectPoints = bl.coords;
Scalar colour = new Scalar(255, 0, 0);
for (int j = 0; j < 3; j++)
Core.line(image, rectPoints[j], rectPoints[j + 1], colour, 2, Core.LINE_AA, 0);
Core.line(image, rectPoints[3], rectPoints[0], colour, 2, Core.LINE_AA, 0);
}
videoDisp.updateImage(image, "Video frame " + i);
}
} catch (IOException ioe) {
System.out.println("IO Exception when finding barcode " + ioe.getMessage());
}
return foundCodes;
}
private static Map<CharSequence, BarcodeLocation> decodeBarcodeFromVideo(List<CandidateResult> candidateCodes,
int frameNumber) {
// decodes barcode using ZXing and either print the barcode text or says no barcode found
Result result = null;
Map<CharSequence, BarcodeLocation> results = new HashMap<>();
for (CandidateResult cr : candidateCodes) {
BufferedImage candidate = cr.candidate;
LuminanceSource source = new BufferedImageLuminanceSource(candidate);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
try {
result = reader.decode(bitmap, hints);
results.put(result.getText(), new BarcodeLocation(cr.ROI_coords, frameNumber));
} catch (ReaderException re) {
}
}
return results;
}
private static void process_image() {
Barcode barcode;
// instantiate a class of type MatrixBarcode with the image filename
try {
barcode = new MatrixBarcode(imgFile, SHOW_INTERMEDIATE_STEPS, TryHarderFlags.VERY_SMALL_MATRIX);
// locateBarcode() returns a List<CandidateResult> with all possible candidate barcode regions from
// within the image. These images then get passed to a decoder(we use ZXing here but could be any decoder)
List<CandidateResult> results = barcode.locateBarcode();
System.out.println("Decoding " + imgFile + " " + results.size() + " candidate codes found");
String imgFile = barcode.getName();
decodeBarcode(results, imgFile, "Localizer");
} catch (IOException ioe) {
System.out.println("IO Exception when finding barcode " + ioe.getMessage());
}
}
private static void decodeBarcode(List<CandidateResult> candidateCodes, String filename, String caption) {
// decodes barcode using ZXing and either print the barcode text or says no barcode found
BufferedImage decodedBarcode = null;
String title = null;
Result result = null;
for (CandidateResult cr : candidateCodes) {
BufferedImage candidate = cr.candidate;
decodedBarcode = null;
LuminanceSource source = new BufferedImageLuminanceSource(candidate);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
try {
result = reader.decode(bitmap, hints);
decodedBarcode = candidate;
title = filename + " " + caption + " - barcode text " + result.getText() + " " + cr.getROI_coords();
} catch (ReaderException re) {
}
if (decodedBarcode == null) {
title = filename + " - no barcode found - " + cr.getROI_coords();
if(showImages)
ImageDisplay.showImageFrame(candidate, title);
} else {
if(showImages)
ImageDisplay.showImageFrame(decodedBarcode, title);
System.out.println("Barcode text for " + filename + " is " + result.getText());
}
}
}
private static void show_usage_syntax() {
System.out.println("");
System.out.println("Barcode localizer by Karthik Jayaraman");
System.out.println("");
System.out.println("Usage: BarcodeTester <imagefile> [-matrix] [-oracle] ");
System.out.println("<imagefile> must be JPEG or PNG");
System.out.println("[-debug] - shows images for intermediate steps and saves intermediate files");
System.out.println("[-video] - <imagefile> is a video");
System.out.println("[-camera] - capture from camera");
System.out.println("[-noimages] - do not display any images, overrides -debug command");
System.out.println("");
}
private static void parse_args(String[] args) {
int ctr = 0;
String arg;
while (ctr < args.length) {
arg = args[ctr++];
if (arg.equalsIgnoreCase("-debug")) {
SHOW_INTERMEDIATE_STEPS = true;
continue;
}
if (arg.equalsIgnoreCase("-video")) {
IS_VIDEO = true;
continue;
}
if (arg.equalsIgnoreCase("-camera")) {
IS_CAMERA = true;
continue;
}
if (arg.equalsIgnoreCase("-noimages")) {
showImages = false;
SHOW_INTERMEDIATE_STEPS = false;
continue;
}
// must be filename if we got here
imgFile = arg;
}
}
private static class BarcodeLocation {
int frame;
Point[] coords;
private BarcodeLocation(Point[] coords, int frame) {
this.frame = frame;
this.coords = coords;
}
public String toString() {
StringBuffer sb = new StringBuffer("Frame " + frame);
for (Point p : coords)
sb.append("(" + (int) (p.x) + "," + (int) (p.y) + "), ");
return sb.toString();
}
}
}