zbar:Qt调用zbar做条码识别

编译:

zbar Windows编译:https://gitee.com/vvvj/zbar-windows

下载下来后,直接使用vs来编译就可以了。

 

zbar官网:https://zbar.sourceforge.net/download.html

 

zbar Mac和Linux编译:

Mac版zbar源码压缩包,0.10版本,下载地址:https://wwqx.lanzoul.com/ihCqf18ctuqh

解压缩,然后按照下面的命令来编译就可以了

# Linux编译需要加参数 CFLAGS, ARM架构、Loongarch架构需要指定编译架构
# ./configure CFLAGS="" --prefix=pwd/build --with-pic=yes --disable-video --without-imagemagick --without-gtk --without-qt --without-python
# ./configure CFLAGS="" --prefix=pwd/build --with-pic=yes --disable-video --without-imagemagick --without-gtk --without-qt --without-python --build=arm-linux
# ./configurt CFIAGS="" --prefix=pwd/build --with-pic=yes --disable-video --without-imagemagick --without-gtk --without-qt --without-python --build=loongarch64-unknown-linux
.
/configure --prefix=pwd/build --with-pic=yes --disable-video --without-imagemagick --without-gtk --without-qt make make install

 

 

主要代码:

#include "zbar.h"
using namespace zbar;
void heihei::tool_tiaoma()
{
    // qDebug() << "条码 === begin =====";
    QImage img;
    img.load("1.png");   // QImage加载jpg,可能失败

    unsigned char* pData = new unsigned char[img.width() * img.height()];
    for (int n = 0; n < img.height(); n++)
        for (int m = 0; m < img.width(); m++)
            pData[n * img.width() + m] = qGray(img.pixel(m, n));
    
    // 传入的img data需要这样写才行,要是直接调用QImage::bits,会没有数据               
    Image imagezbar(img.width(), img.height(), "Y800", pData, img.width() * img.height());
    ImageScanner scanner;
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
    /*int codeCount = */scanner.scan(imagezbar);
    // qDebug() << "识别到的条码数量:" << codeCount;

    Image::SymbolIterator sym = imagezbar.symbol_begin();
    if(imagezbar.symbol_begin() == imagezbar.symbol_end())
    {
        qDebug() << "识别失败";
    }
    else
    {
        for(;sym != imagezbar.symbol_end(); ++sym)
        {
            qDebug() << "条码内容:" << QString::fromStdString(sym->get_data());
        }
    }

    // 释放
    delete[] pData;
    pData = NULL;
}

 

然后就可以拉

 

posted @ 2023-08-24 16:54  十一的杂文录  阅读(450)  评论(0编辑  收藏  举报