c - 在 C 中使用带有 ZBar 的 OpenCV 的问题

标签 c opencv zbar

我正在尝试使用 OpenCV 从文件加载 jpg 图像并将其传递给 zbar 库以解码条形码。但是,没有条形码被正确解码,即使当我使用 libpng 中的函数加载图像时下面的代码有效。我没有错误,我也不知道问题出在哪里,因为我已经检查了我能找到的所有帖子,但没有任何效果。

提前致谢。

#include <stdio.h>
#include <stdlib.h>
#include <zbar.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>


zbar_image_scanner_t *scanner = NULL;
IplImage* cvLoadImage(const char* filename, int iscolor);

int main (int argc, char **argv)
{

    // create a reader
    scanner = zbar_image_scanner_create();

    // configure the reader
    zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);

    // obtain image data with opencv
    IplImage* img = 0;
    int height,width,step,channels;
    img = cvLoadImage(argv[1], 1);
    height = img->height;
    width = img->width;
    step = img->widthStep;
    channels = img->nChannels;
    void *raw = (void *)(img->imageData);
    printf("Processing a %dx%d image \n",height,width);

    // wrap image data
    zbar_image_t *image = zbar_image_create();
    zbar_image_set_format(image, *(int*)"Y800");
    zbar_image_set_size(image, width, height);
    zbar_image_set_data(image, raw, width * height, zbar_image_free_data);


    // scan the image for barcodes
    int n = zbar_scan_image(scanner, image);

    if (n==0){
        printf("No barcode detected for image %s\n", argv[1]);
        return 1;
    }

    // extract results
    if (n!=0) {
        const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
        printf("symbol extracted \n");
        for(; symbol; symbol = zbar_symbol_next(symbol)) {
            // do something useful with results
            zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
            const char *dataZ = zbar_symbol_get_data(symbol);
            printf("decoded %s symbol \"%s\" of image %s \n", zbar_get_symbol_name(typ), dataZ, argv[1]);
        }
    }

    // clean up
    zbar_image_destroy(image);
    zbar_image_scanner_destroy(scanner);

return 0;
}

最佳答案

该代码非常适合我。我在我的程序中使用它并做了一些小改动:

我没有说明如何获取 'struct _IplImage *' {aka 'IplImage *'} 因为它是在另一个文件中完成的,并将其作为参数获取,但它当然是用 cvLoadImage() 完成。

我使用 "GREY" 而不是 "Y800",但我尝试了 "Y800" 并且也成功了,因为它们基本上是一样。

这有效(至少对于 openCV 2.4.9;openCV 正在弃用其 C API,而应使用其 C++ API(我反对这一点,但无能为力 :( )):

/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
/* Standard C ----------------------------------------------------------------*/
        /* snprintf() */
    #include <stdio.h>

/* Packages ------------------------------------------------------------------*/
        /* opencv */
    #include <cv.h>
        /* zbar */
    #include <zbar.h>

/* Module --------------------------------------------------------------------*/
    #include "this_file.h"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
    # define    ZB_CODES_MAX        (10)
    # define    ZBAR_LEN_MAX        (1048576)


/******************************************************************************
 ******* structs **************************************************************
 ******************************************************************************/
    struct  ZB_Codes {
        int n;
        struct {
            int type;
            char    sym_name [80];
            char    data [ZBAR_LEN_MAX];
        } arr [ZB_CODES_MAX];
    };


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/
    struct ZB_Codes zb_codes;


/******************************************************************************
 ******* functions ************************************************************
 ******************************************************************************/
void    img_zb_decode   (struct _IplImage *imgptr)
{
    struct zbar_image_scanner_s *scanner;
    struct zbar_image_s     *image_zb;
    const struct zbar_symbol_s  *symbol;

    /* Type of code to scan */
    /* 0 for all;  set to another if used only for a specific barcode */
    int code_type;
    code_type   = 0;

    /* create & configure a reader */
    scanner = zbar_image_scanner_create();
    zbar_image_scanner_set_config(scanner, code_type, ZBAR_CFG_ENABLE, 1);

    /* wrap image data */
    image_zb    = zbar_image_create();
    zbar_image_set_format(image_zb, *(int *)"GREY");
    zbar_image_set_size(image_zb, imgptr->width, imgptr->height);
    zbar_image_set_data(image_zb, (void *)(imgptr->imageData),
                    (imgptr->width * imgptr->height), NULL);

    /* scan the image for barcodes */
    int i;
    zb_codes.n  = zbar_scan_image(scanner, image_zb);
    if (zb_codes.n) {
        /* extract results */
        symbol  = zbar_image_first_symbol(image_zb);
        for (i = 0; i < ZB_CODES_MAX && symbol; i++) {
            /* Write results into array */
            zb_codes.arr[i].type    = zbar_symbol_get_type(symbol);
            snprintf(zb_codes.arr[i].sym_name, 80, "%s",
                        zbar_get_symbol_name(
                            zb_codes.arr[i].type));
            snprintf(zb_codes.arr[i].data, ZBAR_LEN_MAX, "%s",
                        zbar_symbol_get_data(symbol));

            /* Load next symbol */
            symbol  = zbar_symbol_next(symbol);
        }
    }

    /* clean up */
    zbar_image_destroy(image_zb);
    zbar_image_scanner_destroy(scanner);
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/

关于c - 在 C 中使用带有 ZBar 的 OpenCV 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44371953/

相关文章:

c++ - 再次包含库和 Error LNK2001 : unresolved external symbol

c - C 中的 Readline 函数输出奇怪的结果

android - 如何更改扫描 Zbar 的区域?

opencv - 严重错误:zbar.h:没有此类文件或目录

python - 如何使用 OpenCV 只获取图像中的中心对象?

opencv - 使用 Kinect 进行人脸识别

python - Blob 检测+前景检测

c - 使用链表反向打印字符串

c - 使用 FastCGI 和 Lighttpd 上传大文件

c - GMP 库 : Can we use only one functionality say gmp_printf without Building Complete GMP lib?