quirc是一款专用于识别二维码(不能识别一维码)的开源软件。它比zbar轻量一些,跟zbar一样能扫描码流和静态图片识别条形码,
库文件总共6个文件,非常的小,编译之后的静态库只有25kb。官方代码是依赖libjpg库和linpng库进行解码转换成灰度后再进行二维码检测的。本次仅仅传入灰度图片进行解码,将不依赖jpeg和png进行二维码检测功能
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- static void rgb888tomonochrome(uint8_t *rgb_data, int width, int height, uint8_t *dst)
- {
- int i, j;
- for (i = 0; i < height; i++)
- {
- for (j = 0; j < width; j++)
- {
- unsigned char *pixel = rgb_data + (i * width + j) * 3;
- unsigned char r = pixel[0];
- unsigned char g = pixel[1];
- unsigned char b = pixel[2];
-
- *(dst++) = (r + g + b) / 3;
- }
- }
- }
-
- int main(int argc, char **argv)
- {
- /*读rgb数据*/
- if (argc != 4)
- {
- printf("failed:argc=%d\n", argc);
- return 0;
- }
- /*读取文件*/
- int image_width = atoi(argv[1]);
- int image_hight = atoi(argv[2]);
- const int iamge_size = image_width * image_hight * 3;
- uint8_t *image_buffer = (uint8_t *)malloc(iamge_size);
- if (image_buffer == NULL)
- {
- printf("malloc failed \n");
- return 0;
- }
- int fd = open(argv[3], O_RDONLY);
- read(fd, image_buffer, iamge_size);
- close(fd);
-
- /*创建解码器*/
- struct quirc *qr_coder = quirc_new();
- if (qr_coder == NULL)
- {
- printf("create quirc new failed \n");
- return -1;
- }
-
- /*设置解码的分辨率*/
- if (quirc_resize(qr_coder, image_width, image_hight) < 0)
- {
- printf("quirce size failed \n");
- goto finish;
- }
-
- uint8_t *image_mono_buf = quirc_begin(qr_coder, NULL, NULL);
-
- rgb888tomonochrome(image_buffer, image_width, image_hight, image_mono_buf);
-
-
- quirc_end(qr_coder);
- int qr_count = quirc_count(qr_coder);
- printf("find qrcode count:%d\n", qr_count);
- for (int i = 0; i < qr_count; i++)
- {
- struct quirc_code code;
- struct quirc_data data;
- quirc_extract(qr_coder, i, &code);
- quirc_decode_error_t err = quirc_decode(&code, &data);
-
- if (err == QUIRC_ERROR_DATA_ECC)
- {
- quirc_flip(&code);
- err = quirc_decode(&code, &data);
- }
-
- if (!err)
- {
- printf("%s\n", data.payload);
- }
- }
- quirc_destroy(qr_coder);
- finish:
- if (image_buffer)
- {
- free(image_buffer);
- }
- return -1;
- }