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;
}
复制代码

 

简化版,使用QImage转灰度

复制代码
QImage img;
img.load("1.jpg");  
img = img.convertToFormat(QImage::Format_Grayscale8);   // 灰度化

Image imagezbar(img.width(), img.height(), "Y800", img.bits(), 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());
    }
}
复制代码

 

 

然后就可以拉

 

posted @   十一的杂文录  阅读(602)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-08-24 Qt 桌面服务 QDesktopServices
2021-08-24 Qt 排序 QSort
点击右上角即可分享
微信分享提示