1. OpenCV3.4.1和zbar文件夾放到指定的路徑下,我把它們放在了”D:\二維碼\環境“中。
zbar:鏈接:https://pan.baidu.com/s/11eCDVHVA_R7ktQaX_l6gww 密碼:ubzj
OpenCV3.4.1:鏈接:https://pan.baidu.com/s/1OVbLeG6qJ9aNqdF4UnFFLg 密碼:460q
2. 環境變量配置:
右鍵“此電腦”——屬性——高級系統設置——環境變量。
在系統變量中找到“Path”,編輯。
新建兩個環境變量
D:\二維碼\環境\opencv3.4.1\build\x64\vc14\bin
D:\二維碼\環境\ZBar\bin
3. 在VS2010中新建項目。選擇C++——Win32——Win32控制台應用程序,在附加選項中選擇空項目。
4. 生成的項目中,在解決方案資源管理器中,右鍵解決方案——屬性。配置屬性——配置,配置選活動(Debug),平台選活動(x64)。
5. 視圖——其他窗口——屬性管理器。
6. 配置屬性:
雙擊項目名——配置屬性——VC++目錄,在包含目錄中加入:
D:\二維碼\環境\ZBar\include
D:\二維碼\環境\opencv3.4.1\build\include\opencv2
D:\二維碼\環境\opencv3.4.1\build\include\opencv
D:\二維碼\環境\opencv3.4.1\build\include
在庫目錄中加入:
D:\二維碼\環境\ZBar\lib
D:\二維碼\環境\opencv3.4.1\build\x64\vc14\lib
鏈接器——輸入,在附加依賴項中加入:
opencv_world341d.lib
libzbar-0.lib
libzbar64-0.lib
測試代碼如下,直接選擇二維碼圖片識別,二維碼是一個QRCODE,如果一幅圖片上有多個二維碼,也可以識別出來。該程序在網盤中,圖片識別二維碼。
圖片識別二維碼工程:鏈接:https://pan.baidu.com/s/1Bd-JJDarvc2yvXYQuwKPeA 密碼:dyzz
#include "opencv2/opencv.hpp" #include "zbar.h" #include "cv.h" #include "highgui.h" #include <iostream> using namespace std; using namespace zbar; using namespace cv; int main(int argc, char** argv) { zbar::ImageScanner scanner; scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); string fileName="E:\\QR_CODE.bmp"; Mat image = imread(fileName); double start=clock(); 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; string s=symbol->get_data(); } double end=clock(); double ts=end-start; cout<<ts<<endl; imshow("Source Image", image); waitKey(); imageZbar.set_data(NULL, 0); system("pause"); return 0; }
調用攝像頭代碼:
攝像頭識別二維碼工程:鏈接:https://pan.baidu.com/s/1nyl6Hj6nI9fy4rNt3I-PDA 密碼:6rur
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> using namespace cv; int main(int argc, char** argv) { VideoCapture cap(0);//0-前置攝像頭,1-后置 Mat frame; while (1) { cap >> frame; imshow("調用攝像頭", frame); waitKey(1); } return 0; }
用frame代替上例中的image就可以用攝像頭掃描二維碼了。