最近项目上需要使用二维码识别,网上简单的搜索了下相关知识,这里简单的笔记下流水账。
CONTENTS
zbar
首先下载zbar源码,官方网址:
解压后发现是configure这种方式编译的。
正常编译
cstriker1407:/zbar/zbar-0.10$ ./configure --help `configure' configures zbar 0.10 to adapt to many kinds of systems. 。。。。。。 。。。。。。 Report bugs to <spadix@users.sourceforge.net>.
由于项目只用于识别二维码,因此简单起见,去掉所有的编译可选项。
cstriker1407:/zbar/zbar-0.10$ ./configure --disable-video --without-imagemagick --without-xv --without-xshm --without-gtk --without-python -without-qt --disable-assert 。。。。。。。。 。。。。。。。。 cstriker1407:/zbar/zbar-0.10$ make 。。。。。。。。 。。。。。。。。
编译完成后的库就在 zbar/.libs/ 中。
交叉编译
cstriker1407:/zbar/zbar-0.10$ CC=XXXXXXXX/bin/arm-unknown-linux-uclibcgnueabi-gcc ./configure --disable-video --without-imagemagick --without-xv --without-xshm --without-gtk --without-python -without-qt --disable-assert --build=x86_64 -host=arm-unknown-linux-uclibcgnueabi --disable-shared cstriker1407:/zbar/zbar-0.10$ make
libjpeg
libjpeg的下载地址
【 https://sourceforge.net/projects/libjpeg/ 】
正常编译
cstriker1407:/libjpeg/jpeg-9b$ ./configure cstriker1407:/libjpeg/jpeg-9b$ make
交叉编译
cstriker1407:/libjpeg/jpeg-9b$ CC=usr/bin/arm-unknown-linux-uclibcgnueabi-gcc ./configure --build=x86_64 -host=arm-unknown-linux-uclibcgnueabi cstriker1407:/libjpeg/jpeg-9b$ make
DEMO
#include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <zbar.h> #include <jpeglib.h> #include <jerror.h> static void _convert_data (const unsigned char *p_jpg_buffer, unsigned int jpg_size, int *width, int *height, void **raw) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr err; cinfo.err = jpeg_std_error(&err); jpeg_create_decompress(&cinfo); jpeg_mem_src(&cinfo, (unsigned char *)p_jpg_buffer, jpg_size); (void)jpeg_read_header(&cinfo, TRUE); cinfo.out_color_space = JCS_GRAYSCALE; // cinfo.two_pass_quantize = TRUE; (void)jpeg_start_decompress(&cinfo); *width = cinfo.image_width; *height = cinfo.image_height; *raw = (void *)malloc(cinfo.output_width * cinfo.output_height * 3); unsigned bpl = cinfo.output_width * cinfo.output_components; JSAMPROW buf = (void *)*raw; JSAMPARRAY line = &buf; for (; cinfo.output_scanline < cinfo.output_height; buf += bpl) { jpeg_read_scanlines(&cinfo, line, 1); } (void)jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); } int main (int argc, char **argv) { if(argc < 2) return(1); FILE *pFile = fopen(argv[1], "r+b"); unsigned char *p_ReadBuffer = malloc(81920); memset(p_ReadBuffer, 0, 81920); int freadResult = fread(p_ReadBuffer, 1, 81920, pFile); printf("read %d \r\n", freadResult); zbar_image_scanner_t *scanner = NULL; scanner = zbar_image_scanner_create(); zbar_image_scanner_set_config(scanner, ZBAR_QRCODE, ZBAR_CFG_ENABLE, 1); /* obtain image data */ int width = 0, height = 0; void *raw = NULL; _convert_data(p_ReadBuffer, freadResult, &width, &height, &raw); free(p_ReadBuffer); printf("width:%d height:%d \r\n", width, height); /* 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); /* extract results */ const zbar_symbol_t *symbol = zbar_image_first_symbol(image); for(; symbol; symbol = zbar_symbol_next(symbol)) { /* do something useful with results */ zbar_symbol_type_t typ = zbar_symbol_get_type(symbol); const char *data = zbar_symbol_get_data(symbol); printf("decoded %s symbol \"%s\"\n", zbar_get_symbol_name(typ), data); } /* clean up */ zbar_image_destroy(image); zbar_image_scanner_destroy(scanner); return 0; }
发表评论