-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinSourceCode.cpp
132 lines (105 loc) · 2.45 KB
/
WinSourceCode.cpp
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
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <zbar.h>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <cstdlib>
using namespace cv;
using namespace std;
using namespace zbar;
int main( int argc, char** argv )
{
int n, i;
if( argc < 2)
{
cout <<"Usage: app.exe image.jpg [x] (if [x] is set to 1 image window is shown)" << endl;
system("pause");
return -1;
}
int displayOK;
if(argc > 2)
{
if(!strcmp(argv[2],"1"))
displayOK = 1;
}
else
{
displayOK = 0;
}
//set output file
string INname;
INname = argv[1];
ofstream OUTfis;
size_t lastindex = INname.find_last_of(".");
string OUTname = INname.substr(0, lastindex);
OUTname = OUTname + ".TAG";
OUTfis.open(OUTname);
//scanner setup
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
Mat imgIN;
// Read the image - as grayscale
imgIN = imread(INname, 0);
// Check for invalid input
if(! imgIN.data )
{
cout << "Could not open or find the image" << endl ;
system("pause");
return -1;
}
int width = imgIN.cols;
int height = imgIN.rows;
// wrap image data
uchar *raw = (uchar *)imgIN.data;
Image image(width, height, "Y800", raw, width * height);
// scan the image for barcodes
scanner.scan(image);
Mat imgOUT;
if(displayOK)
{
cvtColor(imgIN,imgOUT,CV_GRAY2RGB);
}
string TAG;
// extract results
for(Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
{
TAG = symbol->get_data();
if(TAG.size() != 14)
continue;
//print tag
cout << TAG << endl;
OUTfis << TAG << endl;
//define vector of points
vector<Point> vp;
if(displayOK)
{
n = symbol->get_location_size();
//get bar code location in image
for(i=0; i<n; i++)
{
vp.push_back(Point(symbol->get_location_x(i),symbol->get_location_y(i)));
}
//build rectangle around the bar code
RotatedRect r = minAreaRect(vp);
Point2f pts[4];
r.points(pts);
//draw lines
for(i=0; i<4; i++)
{
line(imgOUT, pts[i], pts[(i+1)%4], Scalar(255,0,0), 3);
}
}
}
//display information
if(displayOK)
{
namedWindow( "TAG Identification", WINDOW_NORMAL );
imshow("TAG Identification",imgOUT);
}
// clean up
image.set_data(NULL, 0);
waitKey();
}