开发环境;ubuntu 18.04

IDE:clion 2019

源文件.cpp

#include <opencv2/opencv.hpp>
#include <zbar.h>

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

typedef struct
{
    string type;
    string data;
    vector <Point> location;
} decodedObject;

// 查找和解码条形码和二维码
void decode(Mat &im, vector<decodedObject>&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<decodedObject>&decodedObjects)
{
    // 循环所有解码对象
    for (int i = 0; i < decodedObjects.size(); i++)
    {
        vector<Point> points = decodedObjects[i].location;
        vector<Point> 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)
03-29 16:55