zbar:Qt调用zbar做条码识别
需求:Qt开发的一个程序,妹子总是说需要条码识别功能,没办法,只能加上这个功能
zbar编译:https://gitee.com/vvvj/zbar-windows
主要代码:
#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; }
然后就可以拉