zbar配置
比对了下QZXing库,还是zbar强大点。这两个都可以识别0、90、180、270、360角度的条形码。zbar可以识别多个条形码。而且获取条码类型。
1、下载安装zbar,选择full全部安装。也可以从这里下载。
2、由于官方给的是32位的,继续下载64位的zbar
替换文件:
- 将ZBarWin64-master\lib 文件夹下 libzbar64-0.lib 文件复制到ZBar安装路径下的 lib 文件夹;
- 将 ZBarWin64-master\lib 文件夹下 libzbar64-0.dll 文件复制到ZBar安装路径下的 bin 文件夹;
- 将ZBarWin64-master\zbar\libiconv\dll_x64文件夹下 libiconv.dll 复制到ZBar安装路径下的bin 文件夹;
3、将bin目录添加到环境变量
【实战】
VS创建控制台应用
#include <opencv.hpp> #include <zbar.h> #include <iostream> using namespace cv; using namespace zbar; using namespace std; int main() { //定义扫描器 ImageScanner scanner; scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); //加载图片 Mat image = imread("D:/qrcode.jpg"); if (!image.data) { cout << "图片为空,请确认!" << endl; system("pause"); return 0; } //图片转换,Mat转Image Mat imageGray; cvtColor(image, imageGray, COLOR_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; cout << "条码:" << endl << symbol->get_data() << endl; } //显示 imshow("Source Image", image); waitKey(); imageZbar.set_data(NULL, 0);//清除缓存 return 0; std::cout << "Hello World!\n"; }
我的属性表.props如下:
标黄部分修改为自己的版本,VS中添加属性表即可。
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ImportGroup Label="PropertySheets" /> <PropertyGroup Label="UserMacros" /> <PropertyGroup /> <ItemDefinitionGroup> <ClCompile> <AdditionalIncludeDirectories>C:\opencv\debug\build\include;C:\opencv\debug\build\include\opencv2;C:\Program Files %28x86%29\ZBar\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> </ClCompile> <Link> <AdditionalLibraryDirectories>C:\opencv\debug\build\lib;C:\Program Files %28x86%29\ZBar\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalDependencies>opencv_calib3d455d.lib;opencv_core455d.lib;opencv_dnn455d.lib;opencv_features2d455d.lib;opencv_flann455d.lib;opencv_gapi455d.lib;opencv_highgui455d.lib;opencv_imgcodecs455d.lib;opencv_imgproc455d.lib;opencv_ml455d.lib;opencv_objdetect455d.lib;opencv_photo455d.lib;opencv_stitching455d.lib;opencv_video455d.lib;opencv_videoio455d.lib;libzbar64-0.lib;%(AdditionalDependencies)</AdditionalDependencies> </Link> </ItemDefinitionGroup> <ItemGroup /> </Project>
图中有2个条码,识别结果可能带有非数字的特殊符号,需要筛选下。
#include <opencv.hpp> #include <zbar.h> #include <iostream> using namespace cv; using namespace zbar; using namespace std; //只保留字符串中的数字 int ExtractNum(string ss, char* ch) { const char* c = ss.c_str(); int amount = 0; int i = 0; while (c[i] != '\0') { if (c[i] >= '0' && c[i] <= '9') { ch[amount] = c[i]; amount++; } i++; } return amount; } int main() { //定义扫描器 ImageScanner scanner; scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); //加载图片 Mat image = imread("./barcode/条形码.jpg"); if (!image.data) { cout << "图片为空,请确认!" << endl; system("pause"); return 0; } //图片转换,Mat转Image Mat imageGray; cvtColor(image, imageGray, COLOR_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) { char ch[100]; cout << "类型:" << endl << symbol->get_type_name() << endl; cout << "条码:" << endl << symbol->get_data() << endl; int amount = ExtractNum(symbol->get_data(), ch); cout << "条码:" << endl << ch << endl; } //显示 imshow("Source Image", image); waitKey(); imageZbar.set_data(NULL, 0);//清除缓存 return 0; std::cout << "Hello World!\n"; }