summaryrefslogtreecommitdiffstats
path: root/src/image/zbar.cpp
blob: f11e751a0682d24dda75590db83ca22c0ded5c87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/******************************************************************************
 *	Copyright (C) 2018	Alejandro Colomar Andrés		      *
 *	SPDX-License-Identifier:	GPL-2.0-only			      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include "vision-artificial/image/zbar.hpp"

#include <climits>
#include <cstddef>
#include <cstdio>

#include <opencv2/opencv.hpp>
#include <zbar.h>

#include "vision-artificial/image/iface.hpp"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* enums ****************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* structs / unions *****************************************************
 ******************************************************************************/


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* static functions (prototypes) ****************************************
 ******************************************************************************/
static	void	img_zb_decode	(class cv::Mat  *imgptr, const void *data);


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
void	img_zb_act	(class cv::Mat  *imgptr, int action, const void *data)
{

	switch (action) {
	case IMG_ZB_ACT_DECODE:
		img_zb_decode(imgptr, data);
		break;
	}
}


/******************************************************************************
 ******* static functions (definitions) ***************************************
 ******************************************************************************/
static	void	img_zb_decode	(class cv::Mat  *imgptr, const void *data)
{
	const struct zbar::zbar_symbol_s	*symbol;
	struct zbar::zbar_image_scanner_s	*scanner;
	struct zbar::zbar_image_s		*image_zb;
	enum zbar::zbar_symbol_type_e		code_type;
	void					*imgdata;
	ptrdiff_t				rows;
	ptrdiff_t				cols;

	imgdata	= (void *)imgptr->data;
	rows	= imgptr->rows;
	cols	= imgptr->cols;

	/* Type of code to scan */
	code_type = (enum zbar::zbar_symbol_type_e)
			((const struct Img_Iface_Data_Decode *)data)->code_type;

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

	/* wrap image data */
	image_zb = zbar::zbar_image_create();
	zbar::zbar_image_set_format(image_zb, *(int*)"GREY");
	zbar::zbar_image_set_size(image_zb, cols, rows);
	zbar::zbar_image_set_data(image_zb, imgdata, cols * rows, NULL);

	/* scan the image for barcodes */
	zb_codes.n = zbar::zbar_scan_image(scanner, image_zb);
	if (!zb_codes.n)
		goto not_found;

	/* extract results */
	symbol = zbar::zbar_image_first_symbol(image_zb);
	for (ptrdiff_t i = 0; i < ZB_CODES_MAX && symbol; i++) {
		zb_codes.arr[i].type = zbar::zbar_symbol_get_type(symbol);
		code_type = (enum zbar::zbar_symbol_type_e)zb_codes.arr[i].type;
		snprintf(zb_codes.arr[i].sym_name, 80, "%s",
					zbar::zbar_get_symbol_name(code_type));
		snprintf(zb_codes.arr[i].data, ZBAR_LEN_MAX, "%s",
					zbar::zbar_symbol_get_data(symbol));

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

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


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