<环境配置>——Zbar 在Linux/Ubuntu下的安装编译

在C++中我们常用的二维码解析库是zbar和opencv4.0,前者效果更佳,后者刚刚面世。
在Java中我们常用的是Zxing库来解析二维码,Zxing库的使用已经十分成熟了。

  • 1.wget http://downloads.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10.tar.gz
    //官网下载所需编译的库,最好挂个vpn否则可能出现域名解析失败
//解压进入源码
tar xvf zbar-0.10.tar.gz

cd zbar-0.10

//安装依赖项
sudo apt-get install libqt4-dev
sudo apt-get install libv4l-dev
sudo apt-get install python-gtk2-dev
sudo apt-get install imagemagick libmagickwand-dev

//添加依赖
 ln -s /usr/include/libv4l1-videodev.h /usr/include/linux/videodev.h

//生成编译文件
///
//./configure --enable-shared --disable-video --without-python --without-gtk --without-qt --without-imagemagick CFLAGS=""
//根据要求选择自己生成的编译文件,一般如下即可
 ./configure CFLAGS=""

sudo make
sudo make install

简单的测试一下,使用一张二维码或者条形码

zbarimg xxx.jpg

若编译成功,则会显示结果

常见错误:可以参考一下两篇博文
https://blog.csdn.net/Visrul/article/details/80406830
https://blog.csdn.net/felix86/article/details/41443741
附上opencv+zbar 环境下的测试代码

#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;

// Find and decode barcodes and QR codes
void decode(Mat &im, vector<decodedObject>&decodedObjects)
{

    // Create zbar scanner
    ImageScanner scanner;

    // Configure scanner
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

    // Convert image to grayscale
    Mat imGray;
    cvtColor(im, imGray, COLOR_BGR2GRAY);

    // Wrap image data in a zbar image
    Image image(im.cols, im.rows, "Y800", (uchar *)imGray.data, im.cols * im.rows);

    // Scan the image for barcodes and QRCodes
    int n = scanner.scan(image);

    // Print results
    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);
    }
}

// Display barcode and QR code location
void display(Mat &im, vector<decodedObject>&decodedObjects)
{
    // Loop over all decoded objects
    for (int i = 0; i < decodedObjects.size(); i++)
    {
        vector<Point> points = decodedObjects[i].location;
        vector<Point> hull;

        // If the points do not form a quad, find convex hull
        if (points.size() > 4)
            convexHull(points, hull);
        else
            hull = points;

        // Number of points in the convex hull
        int n = hull.size();

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

    }

    // Display results
    imshow("Results", im);
    waitKey(0);

}

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

    // Read image
    Mat im = imread("2.bmp");

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

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

    // Display location
    display(im, decodedObjects);

    return EXIT_SUCCESS;
}

  • 0
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值