Skip to content

Commit 2e40133

Browse files
committed
barcode and QR code scanner using zbar, pyzbar and OpenCV
1 parent 97f1940 commit 2e40133

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <zbar.h>
3+
4+
using namespace cv;
5+
using namespace std;
6+
using namespace zbar;
7+
8+
typedef struct
9+
{
10+
string type;
11+
string data;
12+
vector <Point> location;
13+
} decodedObject;
14+
15+
// Find and decode barcodes and QR codes
16+
void decode(Mat &im, vector<decodedObject>&decodedObjects)
17+
{
18+
19+
// Create zbar scanner
20+
ImageScanner scanner;
21+
22+
// Configure scanner
23+
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
24+
25+
// Convert image to grayscale
26+
Mat imGray;
27+
cvtColor(im, imGray,CV_BGR2GRAY);
28+
29+
// Wrap image data in a zbar image
30+
Image image(im.cols, im.rows, "Y800", (uchar *)imGray.data, im.cols * im.rows);
31+
32+
// Scan the image for barcodes and QRCodes
33+
int n = scanner.scan(image);
34+
35+
// Print results
36+
for(Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
37+
{
38+
decodedObject obj;
39+
40+
obj.type = symbol->get_type_name();
41+
obj.data = symbol->get_data();
42+
43+
// Print type and data
44+
cout << "Type : " << obj.type << endl;
45+
cout << "Data : " << obj.data << endl << endl;
46+
47+
// Obtain location
48+
for(int i = 0; i< symbol->get_location_size(); i++)
49+
{
50+
obj.location.push_back(Point(symbol->get_location_x(i),symbol->get_location_y(i)));
51+
}
52+
53+
decodedObjects.push_back(obj);
54+
}
55+
}
56+
57+
// Display barcode and QR code location
58+
void display(Mat &im, vector<decodedObject>&decodedObjects)
59+
{
60+
// Loop over all decoded objects
61+
for(int i = 0; i < decodedObjects.size(); i++)
62+
{
63+
vector<Point> points = decodedObjects[i].location;
64+
vector<Point> hull;
65+
66+
// If the points do not form a quad, find convex hull
67+
if(points.size() > 4)
68+
convexHull(points, hull);
69+
else
70+
hull = points;
71+
72+
// Number of points in the convex hull
73+
int n = hull.size();
74+
75+
for(int j = 0; j < n; j++)
76+
{
77+
line(im, hull[j], hull[ (j+1) % n], Scalar(255,0,0), 3);
78+
}
79+
80+
}
81+
82+
// Display results
83+
imshow("Results", im);
84+
waitKey(0);
85+
86+
}
87+
88+
int main(int argc, char* argv[])
89+
{
90+
91+
// Read image
92+
Mat im = imread("zbar-test.jpg");
93+
94+
// Variable for decoded objects
95+
vector<decodedObject> decodedObjects;
96+
97+
// Find and decode barcodes and QR codes
98+
decode(im, decodedObjects);
99+
100+
// Display location
101+
display(im, decodedObjects);
102+
103+
return EXIT_SUCCESS;
104+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from __future__ import print_function
2+
import pyzbar.pyzbar as pyzbar
3+
import numpy as np
4+
import cv2
5+
6+
def decode(im) :
7+
# Find barcodes and QR codes
8+
decodedObjects = pyzbar.decode(im)
9+
10+
# Print results
11+
for obj in decodedObjects:
12+
print('Type : ', obj.type)
13+
print('Data : ', obj.data,'\n')
14+
15+
return decodedObjects
16+
17+
18+
# Display barcode and QR code location
19+
def display(im, decodedObjects):
20+
21+
# Loop over all decoded objects
22+
for decodedObject in decodedObjects:
23+
points = decodedObject.location
24+
25+
# If the points do not form a quad, find convex hull
26+
if len(points) > 4 :
27+
hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
28+
hull = list(map(tuple, np.squeeze(hull)))
29+
else :
30+
hull = points;
31+
32+
# Number of points in the convex hull
33+
n = len(hull)
34+
35+
# Draw the convext hull
36+
for j in range(0,n):
37+
cv2.line(im, hull[j], hull[ (j+1) % n], (255,0,0), 3)
38+
39+
# Display results
40+
cv2.imshow("Results", im);
41+
cv2.waitKey(0);
42+
43+
44+
# Main
45+
if __name__ == '__main__':
46+
47+
# Read image
48+
im = cv2.imread('zbar-test.jpg')
49+
50+
decodedObjects = decode(im)
51+
display(im, decodedObjects)
52+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from __future__ import print_function
2+
from sys import argv
3+
import zbar
4+
import cv2
5+
import numpy as np
6+
7+
# Find barcodes and QR codes
8+
def decode(im):
9+
10+
# Create zbar scanner
11+
scanner = zbar.ImageScanner()
12+
13+
# Configure scanner
14+
scanner.parse_config('enable')
15+
16+
# Convert image to grayscale
17+
imGray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
18+
19+
# Find height and width
20+
height, width = imGray.shape
21+
22+
# Get raw image bytes
23+
raw = imGray.tobytes()
24+
25+
# Wrap image data in a zbar image
26+
decodedObjects = zbar.Image(width, height, 'Y800', raw)
27+
28+
# Scan the image for barcodes and QRCodes
29+
scanner.scan(decodedObjects)
30+
31+
# Print results
32+
for decodedObject in decodedObjects:
33+
print('Type : ', decodedObject.type);
34+
print('Data :', decodedObject.data,'\n');
35+
36+
# Return decoded object
37+
return decodedObjects
38+
39+
# Display barcode and QR code location
40+
def display(im, decodedObjects):
41+
42+
# Loop over all decoded objects
43+
for decodedObject in decodedObjects:
44+
points = decodedObject.location
45+
46+
# If the points do not form a quad, find convex hull
47+
if len(points) > 4 :
48+
hull = cv2.convexHull(np.array([point for point in points]))
49+
hull = map(tuple, np.squeeze(hull))
50+
else :
51+
hull = points;
52+
53+
# Number of points in the convex hull
54+
n = len(hull)
55+
56+
# Draw the convext hull
57+
for j in xrange(0,n):
58+
cv2.line(im, hull[j], hull[ (j+1) % n], (255,0,0), 3)
59+
60+
# Display results
61+
cv2.imshow("Results", im);
62+
cv2.waitKey(0);
63+
64+
65+
# Main
66+
if __name__ == '__main__':
67+
68+
# Read image using OpenCV
69+
im = cv2.imread('zbar-test.jpg')
70+
71+
# Decode Image
72+
decodedObjects = decode(im)
73+
74+
# Display location
75+
display(im, decodedObjects)
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+

barcode-QRcodeScanner/zbar-test.jpg

43.8 KB
Loading

0 commit comments

Comments
 (0)