ZBar源码分析:zbar_decode_width

2021SC@SDUSC

zbar的工作流程图:

 zbar文件夹中的decoder.c中的zbar_decode_width方法会将扫描器结构变量保存下来的明暗宽度流进行处理,该函数内部处理对象为当前行目前保存下来的宽度流,通过计算各宽度之间的宽度信息提取扫码特征,依次通过几种一维码二维码的检测标准,寻找到符合标准的扫码种类时更新扫描器结构变量中的type成员,并且更新lock成员以增加当前种类判断的置信度:

zbar_symbol_type_t zbar_decode_width (zbar_decoder_t *dcode,
                                      unsigned w)
{
    zbar_symbol_type_t tmp, sym = ZBAR_NONE;

    dcode->w[dcode->idx & (DECODE_WINDOW - 1)] = w;
    dbprintf(1, "    decode[%x]: w=%d (%g)\n", dcode->idx, w, (w / 32.));

    /* update shared character width */
    dcode->s6 -= get_width(dcode, 7);
    dcode->s6 += get_width(dcode, 1);

    /* each decoder processes width stream in parallel */
#ifdef ENABLE_QRCODE
    if(TEST_CFG(dcode->qrf.config, ZBAR_CFG_ENABLE) &&
       (tmp = _zbar_find_qr(dcode)) > ZBAR_PARTIAL)
        sym = tmp;
#endif
#ifdef ENABLE_EAN
    if((dcode->ean.enable) &&
       (tmp = _zbar_decode_ean(dcode)))
        sym = tmp;
#endif
#ifdef ENABLE_CODE39
    if(TEST_CFG(dcode->code39.config, ZBAR_CFG_ENABLE) &&
       (tmp = _zbar_decode_code39(dcode)) > ZBAR_PARTIAL)
        sym = tmp;
#endif
#ifdef ENABLE_CODE93
    if(TEST_CFG(dcode->code93.config, ZBAR_CFG_ENABLE) &&
       (tmp = _zbar_decode_code93(dcode)) > ZBAR_PARTIAL)
        sym = tmp;
#endif
#ifdef ENABLE_CODE128
    if(TEST_CFG(dcode->code128.config, ZBAR_CFG_ENABLE) &&
       (tmp = _zbar_decode_code128(dcode)) > ZBAR_PARTIAL)
        sym = tmp;
#endif
#ifdef ENABLE_DATABAR
    if(TEST_CFG(dcode->databar.config | dcode->databar.config_exp,
                ZBAR_CFG_ENABLE) &&
       (tmp = _zbar_decode_databar(dcode)) > ZBAR_PARTIAL)
        sym = tmp;
#endif
#ifdef ENABLE_CODABAR
    if(TEST_CFG(dcode->codabar.config, ZBAR_CFG_ENABLE) &&
       (tmp = _zbar_decode_codabar(dcode)) > ZBAR_PARTIAL)
        sym = tmp;
#endif
#ifdef ENABLE_I25
    if(TEST_CFG(dcode->i25.config, ZBAR_CFG_ENABLE) &&
       (tmp = _zbar_decode_i25(dcode)) > ZBAR_PARTIAL)
        sym = tmp;
#endif
#ifdef ENABLE_PDF417
    if(TEST_CFG(dcode->pdf417.config, ZBAR_CFG_ENABLE) &&
       (tmp = _zbar_decode_pdf417(dcode)) > ZBAR_PARTIAL)
        sym = tmp;
#endif

    dcode->idx++;
    dcode->type = sym;
    if(sym) {
        if(dcode->lock && sym > ZBAR_PARTIAL && sym != ZBAR_QRCODE)
            release_lock(dcode, sym);
        if(dcode->handler)
            dcode->handler(dcode);
    }
    return(sym);
}

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include <iostream> #include <chrono> #include <thread> #include <DjiRtspImageSource.h> #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" static inline int64_t now() { return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); } static int write_data_to_file(const char* name, uint8_t* data, int size) { FILE* fd = fopen(name, "wb"); if(fd) { int w = (int)fwrite(data, 1, size, fd); fclose(fd); return w; } else { return -1; } } char rtsp_url = "rtsp://192.168.42.142:8554/live"; int main(int argc, char** argv) { if(argc < 1) return -1; if(argc == 1) { std::cout << "Usage : " << argv[0] << " <url>" << std::endl; return -1; } int64_t ts = now(); DjiRtspImageSource service(rtsp_url); service.setImageCallback(nullptr, [&ts](void* handler, uint8_t* frmdata, int frmsize, int width, int height, int pixfmt) -> void { printf("Image %d@%p -- %dx%d -- %d\n", frmsize, frmdata, width, height, pixfmt); if(frmdata) { int64_t t = now(); if(t - ts > 1000) { ts = t; char name[64]; static int counter = 0; sprintf(name, "pictures/%dx%d-%d_%d.jpg", width, height, pixfmt, ++counter); if(pixfmt == 5) stbi_write_jpg(name, width, height, 3, frmdata, 80); } } }); service.start(); for(;;) //for(int i=0; i<30; i++) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } service.stop(); std::cout << "done." << std::endl; return 0; } 利用上述代码实现提取并解码二维码的信息,并将解码结果保存到tta文件夹下保存为文件名为 list_of_goods,给出c++
07-16
以下是使用 C++ 代码从图像中提取和解码二维码信息,并将解码结果保存到名为 "list_of_goods.txt" 的文件中: ```cpp #include <stdio.h> #include <iostream> #include <chrono> #include <thread> #include <DjiRtspImageSource.h> #include <zbar.h> #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" using namespace std; using namespace zbar; static inline int64_t now() { return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count(); } static int write_data_to_file(const char* name, uint8_t* data, int size) { FILE* fd = fopen(name, "wb"); if(fd) { int w = (int)fwrite(data, 1, size, fd); fclose(fd); return w; } else { return -1; } } void save_qr_code_data(const char* data) { FILE* file = fopen("tta/list_of_goods.txt", "a"); if (file) { fprintf(file, "%s\n", data); fclose(file); } } char rtsp_url[] = "rtsp://192.168.42.142:8554/live"; int main(int argc, char** argv) { if(argc < 1) return -1; if(argc == 1) { cout << "Usage : " << argv[0] << " <url>" << endl; return -1; } int64_t ts = now(); DjiRtspImageSource service(rtsp_url); service.setImageCallback(nullptr, [&ts](void* handler, uint8_t* frmdata, int frmsize, int width, int height, int pixfmt) -> void { printf("Image %d@%p -- %dx%d -- %d\n", frmsize, frmdata, width, height, pixfmt); if(frmdata) { int64_t t = now(); if(t - ts > 1000) { ts = t; char name[64]; static int counter = 0; sprintf(name, "pictures/%dx%d-%d_%d.jpg", width, height, pixfmt, ++counter); if(pixfmt == 5) { stbi_write_jpg(name, width, height, 3, frmdata, 80); // Decode QR code ImageScanner scanner; scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); Image image(width, height, "Y800", frmdata, width * height); int n = scanner.scan(image); for (Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) { cout << "QR code data: " << symbol->get_data() << endl; save_qr_code_data(symbol->get_data().c_str()); } } } } }); service.start(); for(;;) //for(int i=0; i<30; i++) { this_thread::sleep_for(chrono::milliseconds(1000)); } service.stop(); cout << "done." << endl; return 0; } ``` 注意,上述代码使用了 `zbar` 库来进行二维码解码。确保在编译和运行之前已经安装了该库,并根据需要修改保存文件的路径。同时,需要确保 "tta" 文件夹已经存在,以便保存解码结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值