樹莓派 二維碼(QR)識別 c語言篇
阿新 • • 發佈:2018-12-24
樹莓派 二維碼(QR)識別 c語言篇
先看一下執行結果:
識別圖片二維碼,源程式:
#include "zbar.h" #include "cv.h" #include "highgui.h" #include <iostream> 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("helloword.jpg"); 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-二維碼檢測--攝像頭檢測
源程式:
#include "zbar.h" #include "cv.h" #include "highgui.h" #include <iostream> 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("helloword.jpg"); Mat image; VideoCapture cpt; cpt.open(0); char key=0; key = waitKey(50); while (key != 27) { key = waitKey(50); //獲取使用者和按鍵的互動資訊,並延遲50 cpt >> image; //獲取每一幀 ,顯示到 Mat image 上 // imshow("視訊", image); 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(30); /////////////////////////////////////////// //要有一定延遲才能顯示視訊 imageZbar.set_data(NULL, 0); } return 0; }
希望對你有幫助。