[zbar]zbar条码扫描器解析示例

 

//
// Created by leoxae on 2020/3/30.
//

#include "BarCodeRecogntion.h"

string BarCode::BarCodeRecognition(Mat image) {
    string result;
    zbar::ImageScanner scanner;
    scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);

    Mat imageGray;
    cvtColor(image, imageGray, COLOR_RGB2GRAY);
    int width = imageGray.cols;
    int height = imageGray.rows;
    auto *raw = (uchar *) imageGray.data;
    zbar::Image imageZbar(width, height, "Y800", raw, width * height);
    //扫描条码
    scanner.scan(imageZbar);
    zbar::Image::SymbolIterator symbol = imageZbar.symbol_begin();
    if (imageZbar.symbol_begin() == imageZbar.symbol_end()) {
        cout << "查询条码失败,请检查图片!" << endl;
    }
    for (; symbol != imageZbar.symbol_end(); ++symbol) {
        cout << "类型:" << symbol->get_type_name() << endl;
        cout << "条码:" << symbol->get_data() << endl;
        result = symbol->get_data();
    }
    imshow("Source Image", image);
    waitKey();
    imageZbar.set_data(NULL, 0);

    return result;
}

 

posted @ 2020-03-30 16:46  Xu_Lin  阅读(198)  评论(0编辑  收藏  举报