zbar源码编译
wget http://downloads.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10.tar.gz
tar -zvxf zbar-0.10.tar.gz
cd zbar-0.10
export CFLAGS="" && ./configure --prefix=$(pwd)/output0808 --enable-shared --without-imagemagick --without-jpeg --without-python --without-gtk --without-qt --disable-video
make&&make intsall
成功后在output0808文件夹下得到如下结果
OpenCV+Zbar实现二维码识别demo
#include <iostream>
#include <algorithm>
#include <vector>
#include <zbar.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
using namespace zbar;
typedef struct
{
string type;
string data;
vector <Point> location;
}decodedObject;
// Find and decode barcodes and QR codes
void decode(Mat &im, vector<decodedObject>&decodedObjects)
{
// Create zbar scanner
ImageScanner scanner;
// Configure scanner
scanner.set_config(ZBAR_QRCODE, ZBAR_CFG_ENABLE, 1);
// Convert image to grayscale
Mat imGray;
cvtColor(im, imGray,CV_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();
// Print type and data
cout << "Type : " << obj.type << endl;
cout << "Data : " << obj.data << endl << endl;
decodedObjects.push_back(obj);
}
}
int main(int argc, char *argv[])
{
// Read image
string imagepath = argv[1];
Mat im = imread(imagepath);
// Variable for decoded objects
vector<decodedObject> decodedObjects;
// Find and decode barcodes and QR codes
decode(im, decodedObjects);
return 0;
}
我们采用CMake进行编译构建
CMakeLists.txt
project(qrtest)
find_package(OpenCV 2.4.9 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories("${PROJECT_SOURCE_DIR}/include")
add_executable(qrtest test.cpp)
target_link_libraries(qrtest ${OpenCV_LIBS})
target_link_libraries(qrtest "${PROJECT_SOURCE_DIR}/lib/libzbar.so.0.2.0")
target_link_libraries(qrtest pthread)
然后执行命令
mkdir build
cd build
cmake ..
make
output0808下的文件如下
测试
在output0808/build文件夹下执行命令
./qrtest qr_1.png
得到结果