承接上文 opencv 通過cmake編譯生成動态連結庫
配置包含:
VC++目錄 包含目錄

vc++ 庫目錄
連結器 輸入
配置屬性->調試->工作目錄
下面是識别barcode的代碼
image\10.png圖檔放在了E:\vs2015_sketchtown\opencv_vs2015\bin\Release 裡
// QR_test.cpp : 定義控制台應用程式的入口點。
//
#include "stdafx.h"
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "zbar.h"
/*
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat image(2, 2, CV_8UC3, Scalar(0, 0, 255));
//cout << "M = " << endl << " " << image << endl << endl;
//if (argc != 2)
//{
// printf("usage: DisplayImage.out <Image_Path>\n");
// return -1;
//}
image = imread("E:\\1.jpg", IMREAD_COLOR);
if (!image.data)
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
*/
using namespace std;
using namespace zbar; //添加zbar名稱空間
using namespace cv;
int main(int argc, char*argv[])
{
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
Mat image = imread("image\\10.png");
if (!image.data)
{
cout << "請确認圖檔" << endl;
system("pause");
return 0;
}
Mat imageGray;
cvtColor(image, imageGray, CV_RGB2GRAY);
int width = imageGray.cols;
int height = imageGray.rows;
uchar *raw = (uchar *)imageGray.data;
Image imageZbar(width, height, "Y800", raw, width * height);
scanner.scan(imageZbar); //掃描條碼
Image::SymbolIterator symbol = imageZbar.symbol_begin();
if (imageZbar.symbol_begin() == imageZbar.symbol_end())
{
cout << "查詢條碼失敗,請檢查圖檔!" << endl;
}
for (; symbol != imageZbar.symbol_end(); ++symbol)
{
cout << "類型:" << endl << symbol->get_type_name() << endl << endl;
cout << "條碼:" << endl << symbol->get_data() << endl << endl;
}
imshow("Source Image", image);
waitKey();
imageZbar.set_data(NULL, 0);
return 0;
}
下面附上運作程式的代碼圖:
将圖檔替換成二維碼也同樣可以識别成功
注意:還要講opencv和zbar中bin檔案路徑配置到系統環境變量上。如果編譯報出缺少相應有關zbar的dll檔案,将zbar中bin檔案中的lib檔案考到之前配置的vs工作目錄中即可。