-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.hpp
52 lines (37 loc) · 1.21 KB
/
decode.hpp
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
#pragma once
#include <opencv2/opencv.hpp>
#include <zbar.h>
#include <decode_object.hpp>
#include <vector>
#include <functional>
using namespace cv;
using namespace std;
using namespace zbar;
// Find and decode barcodes and QR codes
void decode (Mat &im, vector<decodedObject>&decodedObjects, function <void(decodedObject&)> callback)
{
// Create zbar scanner
ImageScanner scanner;
// Configure scanner
scanner.set_config (ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
scanner.set_config(ZBAR_QRCODE, ZBAR_CFG_ASCII, 1);
// Convert image to grayscale
Mat imGray;
cvtColor (im, imGray,COLOR_BGR2GRAY);
// Wrap image data in a zbar image
Image image (im.cols, im.rows, "Y800", (uchar *)imGray.data, im.cols * im.rows);
// Scan the image for barcodes and QRCodes
int n = scanner.scan (image);
// Print results
for (Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
{
decodedObject obj;
obj.type = symbol->get_type_name();
obj.data = symbol->get_data();
// Obtain location
for (int i = 0; i < symbol->get_location_size(); i++)
obj.location.push_back(Point(symbol->get_location_x(i),symbol->get_location_y(i)));
decodedObjects.push_back(obj);
(callback) (obj);
}
}