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()); } }
然后就可以拉
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源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