zbar解析二维码demo
阅读原文时间:2023年07月08日阅读:5

开发环境;ubuntu 18.04

IDE:clion 2019

源文件.cpp

#include
#include

using namespace cv;
using namespace std;
using namespace zbar;

typedef struct
{
string type;
string data;
vector location;
} decodedObject;

// 查找和解码条形码和二维码
void decode(Mat &im, vector&decodedObjects)
{

// 创建ZBAR扫描器  
ImageScanner scanner;

// 配置扫描器  
scanner.set\_config(ZBAR\_NONE, ZBAR\_CFG\_ENABLE, 1);

// 将图像转换为灰度  
Mat imGray;  
cvtColor(im, imGray, COLOR\_BGR2GRAY);  
imshow("gray",imGray);  
// 在ZBAR图像中包装图像数据  
Image image(im.cols, im.rows, "Y800", (uchar \*)imGray.data, im.cols \* im.rows);

// 扫描图像中的条形码和二维码  
int n = scanner.scan(image);

// 打印输出结果  
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;

    // Obtain location  
    for (int i = 0; i < symbol->get\_location\_size(); i++)  
    {  
        obj.location.push\_back(Point(symbol->get\_location\_x(i), symbol->get\_location\_y(i)));  
    }

    decodedObjects.push\_back(obj);  
}  

}

// 显示条形码和二维码位置
void display(Mat &im, vector&decodedObjects)
{
// 循环所有解码对象
for (int i = 0; i < decodedObjects.size(); i++) { vector points = decodedObjects[i].location;
vector hull;

    // 如果这些点不形成四边形,则找到凸包。  
    if (points.size() > 4)  
        convexHull(points, hull);  
    else  
        hull = points;

    // 凸包中的点数  
    int n = hull.size();

    for (int j = 0; j < n; j++)  
    {  
        line(im, hull\[j\], hull\[(j + 1) % n\], Scalar(255, 0, 0), 3);  
    }

}

// 显示结果  
imshow("Results", im);  
waitKey(0);

}

int main(int argc, char* argv[])
{

// Read image  
Mat im = imread("/home/leoxae/图片/qrcode2.png");

// Variable for decoded objects  
vector<decodedObject> decodedObjects;

// Find and decode barcodes and QR codes  
decode(im, decodedObjects);

// Display location  
display(im, decodedObjects);  
waitKey(0);  
return 0;  

}

cmakelist配置

cmake_minimum_required(VERSION 3.14)
project(QRcode)

set(CMAKE_CXX_STANDARD 14)

add_executable(QRcode main.cpp)

#找到opencv的包
set(TARGET_LIBS "/usr/local/include/opencv4/opencv2/")
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

#zbar
set(zbar_include_dir "/usr/local/include")
set(zbar_link_dir "/usr/local/lib")

include_directories(${zbar_include_dir})
link_directories(${zbar_link_dir})
link_libraries(zbar)

#jsoncpp
set(jsoncpp_include_dir "/home/leoxae/apps/jsoncpp/1.8.4/include")
set(jsoncpp_link_dir "/home/leoxae/apps/jsoncpp/1.8.4/lib")

include_directories(${jsoncpp_include_dir})
link_directories(${jsoncpp_link_dir})
link_libraries(jsoncpp)

#动态链接
#target_link_libraries(pedestrian_detection ${OpenCV_LIBS} )
target_link_libraries(QRcode ${OpenCV_LIBS} )
target_link_libraries(QRcode jsoncpp)
target_link_libraries(QRcode zbar)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章