2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <mv_barcode.h>
19 #include <image_helper.h>
20 #include <mv_private.h>
27 #include <libswscale/swscale.h>
28 #include <libavcodec/avcodec.h>
29 #include <libavutil/pixfmt.h>
32 mv_barcode_type_e type;
33 mv_barcode_qr_ecc_e ecc;
34 mv_barcode_qr_mode_e mode;
38 mv_barcode_image_format_e out_image_format;
39 mv_colorspace_e colorspace;
43 unsigned char *out_buffer_ptr;
47 MV_TS_GENERATE_TO_IMAGE_FCN,
48 MV_TS_GENERATE_TO_SOURCE_FCN
51 int convert_rgb_to(unsigned char *src_buffer, unsigned char **dst_buffer,
52 image_data_s image_data, mv_colorspace_e dst_colorspace,
53 unsigned long *cvt_buffer_size)
55 enum PixelFormat pixel_format = PIX_FMT_NONE;
57 MEDIA_VISION_FUNCTION_ENTER();
59 switch (dst_colorspace) {
60 case MEDIA_VISION_COLORSPACE_Y800:
61 pixel_format = PIX_FMT_GRAY8;
63 case MEDIA_VISION_COLORSPACE_I420:
64 pixel_format = PIX_FMT_YUV420P;
66 case MEDIA_VISION_COLORSPACE_NV12:
67 pixel_format = PIX_FMT_NV12;
69 case MEDIA_VISION_COLORSPACE_YV12:
70 /* the same as I420 with inversed U and V */
71 pixel_format = PIX_FMT_YUV420P;
73 case MEDIA_VISION_COLORSPACE_NV21:
74 pixel_format = PIX_FMT_NV21;
76 case MEDIA_VISION_COLORSPACE_YUYV:
77 pixel_format = PIX_FMT_YUYV422;
79 case MEDIA_VISION_COLORSPACE_UYVY:
80 pixel_format = PIX_FMT_UYVY422;
82 case MEDIA_VISION_COLORSPACE_422P:
83 pixel_format = PIX_FMT_YUV422P;
85 case MEDIA_VISION_COLORSPACE_RGB565:
86 pixel_format = PIX_FMT_RGB565BE;
88 case MEDIA_VISION_COLORSPACE_RGBA:
89 pixel_format = PIX_FMT_RGBA;
91 case MEDIA_VISION_COLORSPACE_RGB888:
92 *cvt_buffer_size = image_data.image_width * image_data.image_height * 3;
93 (*dst_buffer) = (unsigned char*)malloc(*cvt_buffer_size);
94 memcpy(*dst_buffer, src_buffer, *cvt_buffer_size);
96 MEDIA_VISION_FUNCTION_LEAVE();
97 return MEDIA_VISION_ERROR_NONE;
99 MEDIA_VISION_FUNCTION_LEAVE();
100 return MEDIA_VISION_ERROR_NOT_SUPPORTED;
103 AVPicture src_picture;
104 AVPicture dst_picture;
106 avpicture_fill(&src_picture, (uint8_t*)src_buffer, PIX_FMT_RGB24,
107 image_data.image_width, image_data.image_height);
109 avpicture_alloc(&dst_picture, pixel_format,
110 image_data.image_width, image_data.image_height);
112 struct SwsContext *context = sws_getContext(
113 image_data.image_width, image_data.image_height, PIX_FMT_RGB24,
114 image_data.image_width, image_data.image_height, pixel_format,
115 SWS_FAST_BILINEAR, 0, 0, 0);
117 sws_scale(context, (const uint8_t * const *)src_picture.data,
118 src_picture.linesize, 0, image_data.image_height,
119 dst_picture.data, dst_picture.linesize);
121 *cvt_buffer_size = avpicture_get_size(pixel_format,
122 image_data.image_width, image_data.image_height);
123 (*dst_buffer) = (unsigned char*)malloc(*cvt_buffer_size);
124 memcpy(*dst_buffer, dst_picture.data[0], *cvt_buffer_size);
126 avpicture_free(&dst_picture);
128 MEDIA_VISION_FUNCTION_LEAVE();
130 return MEDIA_VISION_ERROR_NONE;
133 int find_min_x(const mv_quadrangle_s *quadrangle, int *minX)
135 MEDIA_VISION_FUNCTION_ENTER();
137 if (NULL == quadrangle) {
138 MEDIA_VISION_FUNCTION_LEAVE();
139 return MEDIA_VISION_ERROR_INVALID_PARAMETER;
142 *minX = quadrangle->points[0].x;
143 *minX = quadrangle->points[1].x < *minX ? quadrangle->points[1].x : *minX;
144 *minX = quadrangle->points[2].x < *minX ? quadrangle->points[2].x : *minX;
145 *minX = quadrangle->points[3].x < *minX ? quadrangle->points[3].x : *minX;
147 MEDIA_VISION_FUNCTION_LEAVE();
149 return MEDIA_VISION_ERROR_NONE;
152 int find_min_y(const mv_quadrangle_s *quadrangle, int *minY)
154 MEDIA_VISION_FUNCTION_ENTER();
156 if (NULL == quadrangle) {
157 MEDIA_VISION_FUNCTION_LEAVE();
158 return MEDIA_VISION_ERROR_INVALID_PARAMETER;
161 *minY = quadrangle->points[0].y;
162 *minY = quadrangle->points[1].y < *minY ? quadrangle->points[1].y : *minY;
163 *minY = quadrangle->points[2].y < *minY ? quadrangle->points[2].y : *minY;
164 *minY = quadrangle->points[3].y < *minY ? quadrangle->points[3].y : *minY;
166 MEDIA_VISION_FUNCTION_LEAVE();
168 return MEDIA_VISION_ERROR_NONE;
171 int find_max_x(const mv_quadrangle_s *quadrangle, int *maxX)
173 MEDIA_VISION_FUNCTION_ENTER();
175 if (NULL == quadrangle) {
176 MEDIA_VISION_FUNCTION_LEAVE();
177 return MEDIA_VISION_ERROR_INVALID_PARAMETER;
180 *maxX = quadrangle->points[0].x;
181 *maxX = quadrangle->points[1].x > *maxX ? quadrangle->points[1].x : *maxX;
182 *maxX = quadrangle->points[2].x > *maxX ? quadrangle->points[2].x : *maxX;
183 *maxX = quadrangle->points[3].x > *maxX ? quadrangle->points[3].x : *maxX;
185 MEDIA_VISION_FUNCTION_LEAVE();
187 return MEDIA_VISION_ERROR_NONE;
190 int find_max_y(const mv_quadrangle_s *quadrangle, int *maxY)
192 MEDIA_VISION_FUNCTION_ENTER();
194 if (NULL == quadrangle) {
195 MEDIA_VISION_FUNCTION_LEAVE();
196 return MEDIA_VISION_ERROR_INVALID_PARAMETER;
199 *maxY = quadrangle->points[0].y;
200 *maxY = quadrangle->points[1].y > *maxY ? quadrangle->points[1].y : *maxY;
201 *maxY = quadrangle->points[2].y > *maxY ? quadrangle->points[2].y : *maxY;
202 *maxY = quadrangle->points[3].y > *maxY ? quadrangle->points[3].y : *maxY;
204 MEDIA_VISION_FUNCTION_LEAVE();
206 return MEDIA_VISION_ERROR_NONE;
209 bool _mv_engine_config_supported_attribute(mv_config_attribute_type_e attribute_type,
210 const char *attribute_name, void *user_data)
212 printf("Callback call for engine configuration attribute\n");
214 if (user_data == NULL)
217 mv_engine_config_h mv_engine_config = (mv_engine_config_h *)user_data;
220 double double_value = 0.0;
221 bool bool_value = false;
222 char str_value[1024];
223 switch (attribute_type) {
224 case MV_ENGINE_CONFIG_ATTR_TYPE_DOUBLE:
225 if (MEDIA_VISION_ERROR_KEY_NOT_AVAILABLE ==
226 mv_engine_config_get_double_attribute(
227 mv_engine_config, attribute_name, &double_value)) {
228 printf("Default double attribute %s wasn't set in engine\n",
232 printf("Default double attribute %s was set to %f in engine\n",
233 attribute_name, double_value);
235 case MV_ENGINE_CONFIG_ATTR_TYPE_INTEGER:
236 if (MEDIA_VISION_ERROR_KEY_NOT_AVAILABLE ==
237 mv_engine_config_get_int_attribute(
238 mv_engine_config, attribute_name, &int_value)) {
239 printf("Default integer attribute %s wasn't set in engine\n",
243 printf("Default interget attribute %s was set to %d in engine\n",
244 attribute_name, int_value);
246 case MV_ENGINE_CONFIG_ATTR_TYPE_BOOLEAN:
247 if (MEDIA_VISION_ERROR_KEY_NOT_AVAILABLE ==
248 mv_engine_config_get_bool_attribute(
249 mv_engine_config, attribute_name, &bool_value)) {
250 printf("Default bool attribute %s wasn't set in engine\n",
254 printf("Default bool attribute %s was set to %s in engine\n",
255 attribute_name, bool_value ? "TRUE" : "FALSE");
257 case MV_ENGINE_CONFIG_ATTR_TYPE_STRING:
258 if (MEDIA_VISION_ERROR_KEY_NOT_AVAILABLE ==
259 mv_engine_config_get_string_attribute(
260 mv_engine_config, attribute_name, &str_value)) {
261 printf("Default string ttribute %s wasn't set in engine\n",
265 printf("Default string attribute %s was set to %s in engine\n",
266 attribute_name, str_value);
269 printf("Not supported attribute type\n");
276 void barcode_detected_cb(
278 mv_engine_config_h engine_cfg,
279 const mv_quadrangle_s *barcodes_locations,
280 const char *messages[],
281 const mv_barcode_type_e *types,
282 int number_of_barcodes,
285 MEDIA_VISION_FUNCTION_ENTER();
287 printf("%i barcodes were detected on the image.\n", number_of_barcodes);
288 if (number_of_barcodes > 0) {
289 int is_source_data_loaded = 0;
291 char *file_name = NULL;
292 unsigned char *out_buffer = NULL;
293 unsigned char *draw_buffer = NULL;
294 unsigned int buf_size = 0;
295 image_data_s image_data = { 0, 0, MEDIA_VISION_COLORSPACE_INVALID };
296 /* Check Media Vision source: */
297 if (MEDIA_VISION_ERROR_NONE != mv_source_get_buffer(source, &out_buffer, &buf_size) ||
298 MEDIA_VISION_ERROR_NONE != mv_source_get_width(source, &(image_data.image_width)) ||
299 MEDIA_VISION_ERROR_NONE != mv_source_get_height(source, &(image_data.image_height)) ||
300 MEDIA_VISION_ERROR_NONE != mv_source_get_colorspace(source, &(image_data.image_colorspace)) ||
302 printf("ERROR: Creating out image is impossible.\n");
304 file_name = ((barcode_model_s *)user_data)->out_file_name;
305 draw_buffer = ((barcode_model_s *)user_data)->out_buffer_ptr;
306 image_data.image_colorspace = MEDIA_VISION_COLORSPACE_RGB888;
307 is_source_data_loaded = 1;
311 for (i = 0; i < number_of_barcodes; ++i) {
312 const char *cur_message = messages[i];
313 mv_barcode_type_e cur_type = types[i];
314 const char *str_type = NULL;
319 case MV_BARCODE_UPC_A:
322 case MV_BARCODE_UPC_E:
325 case MV_BARCODE_EAN_8:
326 case MV_BARCODE_EAN_13:
327 str_type = "EAN-8/13";
329 case MV_BARCODE_CODE128:
330 str_type = "CODE128";
332 case MV_BARCODE_CODE39:
335 case MV_BARCODE_I2_5:
339 str_type = "Undetected";
342 printf("\tBarcode %i : type is %s\n", i, str_type);
343 if (cur_message != NULL)
344 printf("\t message is %s\n", cur_message);
346 printf("\t message wasn't detected\n");
348 if (is_source_data_loaded == 1) {
353 if (MEDIA_VISION_ERROR_NONE != find_min_x(&barcodes_locations[i], &minX) ||
354 MEDIA_VISION_ERROR_NONE != find_min_y(&barcodes_locations[i], &minY) ||
355 MEDIA_VISION_ERROR_NONE != find_max_x(&barcodes_locations[i], &maxX) ||
356 MEDIA_VISION_ERROR_NONE != find_max_y(&barcodes_locations[i], &maxY)) {
360 const int rectangle_thickness = 6;
361 const int drawing_color[] = {255, 0, 0};
362 if (MEDIA_VISION_ERROR_NONE != draw_rectangle_on_buffer(
376 if (file_name != NULL &&
377 MEDIA_VISION_ERROR_NONE == save_image_from_buffer(file_name, draw_buffer, &image_data, 100)) {
378 printf("Image was generated as %s\n", file_name);
380 printf("ERROR: Failed to generate output file. Check file name and permissions. \n");
386 MEDIA_VISION_FUNCTION_LEAVE();
389 int generate_barcode_to_image(barcode_model_s model)
391 MEDIA_VISION_FUNCTION_ENTER();
393 if (model.message == NULL ||
394 model.file_name == NULL) {
395 MEDIA_VISION_FUNCTION_LEAVE();
396 return MEDIA_VISION_ERROR_INVALID_PARAMETER;
399 LOGI("Call the mv_barcode_generate_image() function");
401 const int err = mv_barcode_generate_image(
411 model.out_image_format);
413 MEDIA_VISION_FUNCTION_LEAVE();
418 int generate_barcode_to_source(barcode_model_s model)
420 MEDIA_VISION_FUNCTION_ENTER();
422 if (model.message == NULL ||
423 model.file_name == NULL) {
424 MEDIA_VISION_FUNCTION_LEAVE();
426 return MEDIA_VISION_ERROR_INVALID_PARAMETER;
429 LOGI("mv_source_h creation started");
431 mv_source_h source = NULL;
432 int err = mv_create_source(&source);
433 if (MEDIA_VISION_ERROR_NONE != err) {
434 printf("ERROR: Error occurred when trying to create Media Vision "
435 "source. Error code: %i\n", err);
437 MEDIA_VISION_FUNCTION_LEAVE();
442 LOGI("mv_source_h creation finished");
444 LOGI("Call the mv_barcode_generate_source() function");
446 err = mv_barcode_generate_source(
455 if (MEDIA_VISION_ERROR_NONE != err) {
456 printf("ERROR: Error occurred during generation barcode to the "
457 "Media Vision source. Error code: %i\n", err);
459 const int err2 = mv_destroy_source(source);
460 if (MEDIA_VISION_ERROR_NONE != err2) {
461 printf("ERROR: Error occurred when try to destroy Media Vision source."
462 "Error code: %i\n", err2);
465 MEDIA_VISION_FUNCTION_LEAVE();
470 unsigned char *data_buffer = NULL;
471 unsigned int buffer_size = 0;
472 unsigned int image_width = 0;
473 unsigned int image_height = 0;
474 mv_colorspace_e image_colorspace = MEDIA_VISION_COLORSPACE_INVALID;
476 bool is_source_corrupted = false;
477 err = mv_source_get_buffer(source, &data_buffer, &buffer_size);
478 if (MEDIA_VISION_ERROR_NONE != err) {
479 printf("ERROR: Error occurred when trying to get buffer from "
480 "Media Vision source. Error code: %i\n", err);
481 is_source_corrupted = true;
484 err = mv_source_get_width(source, &image_width);
485 if (MEDIA_VISION_ERROR_NONE != err) {
486 printf("ERROR: Error occurred when trying to get width of "
487 "Media Vision source. Error code: %i\n", err);
488 is_source_corrupted = true;
491 err = mv_source_get_height(source, &image_height);
492 if (MEDIA_VISION_ERROR_NONE != err) {
493 printf("ERROR: Error occurred when trying to get height of "
494 "Media Vision source. Error code: %i\n", err);
495 is_source_corrupted = true;
498 err = mv_source_get_colorspace(source, &image_colorspace);
499 if (MEDIA_VISION_ERROR_NONE != err) {
500 printf("ERROR: Error occurred when trying to get colorspace of "
501 "Media Vision source. Error code: %i\n", err);
502 is_source_corrupted = true;
505 if (is_source_corrupted) {
506 err = mv_destroy_source(source);
507 if (MEDIA_VISION_ERROR_NONE != err) {
508 printf("ERROR: Error occurred when trying to destroy Media Vision "
509 "source. Error code: %i\n", err);
512 MEDIA_VISION_FUNCTION_LEAVE();
514 return MEDIA_VISION_ERROR_INTERNAL;
517 const image_data_s image_data = { image_width, image_height, image_colorspace };
519 char *jpeg_file_name = "";
520 if (0 == strcmp(model.file_name + strlen(model.file_name) - 4, ".jpg") ||
521 0 == strcmp(model.file_name + strlen(model.file_name) - 5, ".jpeg")) {
522 jpeg_file_name = (char*)malloc(strlen(model.file_name) + 1);
523 strcpy(jpeg_file_name, model.file_name);
524 jpeg_file_name[strlen(model.file_name)] = '\0';
526 jpeg_file_name = (char*)malloc(strlen(model.file_name) + 5);
527 strcpy(jpeg_file_name, model.file_name);
528 strcpy(jpeg_file_name + strlen(model.file_name), ".jpg");
529 jpeg_file_name[strlen(model.file_name) + 4] = '\0';
532 save_image_from_buffer(jpeg_file_name, data_buffer, &image_data, 100);
534 free(jpeg_file_name);
536 const int err2 = mv_destroy_source(source);
537 if (MEDIA_VISION_ERROR_NONE != err2) {
538 printf("ERROR: Error occurred when try to destroy Media Vision source."
539 "Error code: %i\n", err2);
542 MEDIA_VISION_FUNCTION_LEAVE();
547 int detect_barcode(barcode_model_s model, mv_rectangle_s roi)
549 MEDIA_VISION_FUNCTION_ENTER();
551 unsigned char *data_buffer = NULL;
552 unsigned long buffer_size = 0;
553 image_data_s image_data;
555 int err = load_image_to_buffer(
556 model.file_name, &data_buffer, &buffer_size, &image_data);
557 if (MEDIA_VISION_ERROR_NONE != err) {
558 printf("ERROR: Errors were occurred during opening the file!!! code: %i\n", err);
560 MEDIA_VISION_FUNCTION_LEAVE();
565 unsigned char *converted_buffer = NULL;
566 unsigned long converted_buffer_size = 0;
567 err = convert_rgb_to(data_buffer, &converted_buffer, image_data, model.colorspace, &converted_buffer_size);
568 if (MEDIA_VISION_ERROR_NONE != err) {
569 printf("ERROR: Can't convert to the selected colorspace!!! code: %i\n", err);
571 MEDIA_VISION_FUNCTION_LEAVE();
576 model.out_buffer_ptr = data_buffer;
578 mv_engine_config_h mv_engine_config;
579 err = mv_create_engine_config(&mv_engine_config);
580 if (MEDIA_VISION_ERROR_NONE != err)
581 printf("ERROR: Errors were occurred during creating the media engine config: %i\n", err);
583 mv_engine_config_foreach_supported_attribute(_mv_engine_config_supported_attribute, mv_engine_config);
585 mv_engine_config_set_int_attribute(mv_engine_config, MV_BARCODE_DETECT_ATTR_TARGET, MV_BARCODE_DETECT_ATTR_TARGET_2D_BARCODE);
588 err = mv_create_source(&source);
589 if (MEDIA_VISION_ERROR_NONE != err) {
590 printf("ERROR: Errors were occurred during creating the source!!! code: %i\n", err);
592 MEDIA_VISION_FUNCTION_LEAVE();
597 err = mv_source_fill_by_buffer(source, converted_buffer, converted_buffer_size,
598 image_data.image_width, image_data.image_height, model.colorspace);
599 if (MEDIA_VISION_ERROR_NONE != err) {
600 printf("ERROR: Errors were occurred during filling the source!!! code: %i\n", err);
602 MEDIA_VISION_FUNCTION_LEAVE();
607 if (converted_buffer != NULL)
608 free(converted_buffer);
610 err = mv_barcode_detect(source, mv_engine_config, roi, barcode_detected_cb, &model);
612 if (data_buffer != NULL)
613 destroy_loaded_buffer(data_buffer);
615 if (MEDIA_VISION_ERROR_NONE != err) {
616 printf("ERROR: Errors were occurred during barcode detection!!! code: %i\n", err);
618 MEDIA_VISION_FUNCTION_LEAVE();
623 err = mv_destroy_source(source);
624 if (MEDIA_VISION_ERROR_NONE != err)
625 printf("ERROR: Errors were occurred during destroying the source!!! code: %i\n", err);
627 err = mv_destroy_engine_config(mv_engine_config);
628 if (MEDIA_VISION_ERROR_NONE != err)
629 printf("ERROR: Error were occurred during destroying the source!!! code: %i\n", err);
631 MEDIA_VISION_FUNCTION_LEAVE();
636 int input_string(const char *prompt, size_t max_len, char **string)
638 MEDIA_VISION_FUNCTION_ENTER();
641 printf("%s ", prompt);
643 if (scanf("\n") != 0) {
644 MEDIA_VISION_FUNCTION_LEAVE();
648 char buffer[max_len];
650 buffer[last_char] = '\0';
651 buffer[sizeof(buffer) - 1] = ~'\0';
652 if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
653 MEDIA_VISION_FUNCTION_LEAVE();
656 size_t real_string_len = strlen(buffer);
657 buffer[real_string_len - 1] = '\0';
658 *string = (char*)malloc(real_string_len * sizeof(char));
659 strcpy(*string, buffer);
661 size_t str_len = strlen(*string);
663 MEDIA_VISION_FUNCTION_LEAVE();
668 int input_size(const char *prompt, size_t max_size, size_t *size)
670 MEDIA_VISION_FUNCTION_ENTER();
673 printf("%s ", prompt);
675 if (scanf("%20zu", size) == 0) {
676 if (scanf("%*[^\n]%*c") != 0) {
677 printf("ERROR: Reading the input line error.\n");
678 MEDIA_VISION_FUNCTION_LEAVE();
681 printf("ERROR: Incorrect input.\n");
682 MEDIA_VISION_FUNCTION_LEAVE();
686 int ret = (*size > max_size ? -1 : 0);
688 MEDIA_VISION_FUNCTION_LEAVE();
693 int input_int(const char *prompt, int min_value, int max_value, int *value)
695 MEDIA_VISION_FUNCTION_ENTER();
698 printf("%s ", prompt);
700 if (scanf("%20i", value) == 0) {
701 if (scanf("%*[^\n]%*c") != 0) {
702 printf("ERROR: Reading the input line error.\n");
703 MEDIA_VISION_FUNCTION_LEAVE();
706 printf("ERROR: Incorrect input.\n");
707 MEDIA_VISION_FUNCTION_LEAVE();
711 int ret = (*value < min_value || *value > max_value ? -1 : 0);
713 MEDIA_VISION_FUNCTION_LEAVE();
718 int show_menu(const char *title, const int *options, const char **names, int cnt)
720 MEDIA_VISION_FUNCTION_ENTER();
722 printf("***************************\n");
723 printf("* %23s *\n", title);
724 printf("*-------------------------*\n");
726 for (i = 0; i < cnt; ++i)
727 printf("* %2i. %19s *\n", options[i], names[i]);
729 printf("***************************\n\n");
731 printf("Your choise: ");
732 if (scanf("%20i", &selection) == 0) {
733 if (scanf("%*[^\n]%*c") != 0) {
734 printf("ERROR: Reading the input line error.\n");
735 MEDIA_VISION_FUNCTION_LEAVE();
738 printf("ERROR: Incorrect input.\n");
741 MEDIA_VISION_FUNCTION_LEAVE();
746 mv_barcode_type_e select_type(void)
748 mv_barcode_type_e selected_type = MV_BARCODE_UNDEFINED;
750 const int options[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
751 const char *names[8] = { "qr", "upca", "upce", "ean8", "ean13", "code39", "code128", "interleave25" };
753 MEDIA_VISION_FUNCTION_ENTER();
755 while (sel_opt == 0) {
756 sel_opt = show_menu("Select barcode type:", options, names, 8);
760 selected_type = MV_BARCODE_QR;
763 selected_type = MV_BARCODE_UPC_A;
766 selected_type = MV_BARCODE_UPC_E;
769 selected_type = MV_BARCODE_EAN_8;
772 selected_type = MV_BARCODE_EAN_13;
775 selected_type = MV_BARCODE_CODE39;
778 selected_type = MV_BARCODE_CODE128;
781 selected_type = MV_BARCODE_I2_5;
789 MEDIA_VISION_FUNCTION_LEAVE();
791 return selected_type;
794 mv_barcode_qr_mode_e select_mode(void)
796 mv_barcode_qr_mode_e selected_mode = MV_BARCODE_QR_MODE_UNAVAILABLE;
798 const int options[4] = { 1, 2, 3, 4 };
799 const char *names[4] = { "numeric", "alphanumeric", "byte", "utf8" };
801 MEDIA_VISION_FUNCTION_ENTER();
803 while (sel_opt == 0) {
804 sel_opt = show_menu("Select encoding mode:", options, names, 4);
807 selected_mode = MV_BARCODE_QR_MODE_NUMERIC;
810 selected_mode = MV_BARCODE_QR_MODE_ALPHANUMERIC;
813 selected_mode = MV_BARCODE_QR_MODE_BYTE;
816 selected_mode = MV_BARCODE_QR_MODE_UTF8;
824 MEDIA_VISION_FUNCTION_LEAVE();
826 return selected_mode;
829 mv_barcode_qr_ecc_e select_ecc(void)
831 mv_barcode_qr_ecc_e selected_ecc = MV_BARCODE_QR_ECC_UNAVAILABLE;
833 const int options[4] = { 1, 2, 3, 4 };
834 const char *names[4] = { "low", "medium", "quartile", "high" };
836 MEDIA_VISION_FUNCTION_ENTER();
838 while (sel_opt == 0) {
839 sel_opt = show_menu("Select ECC level:", options, names, 4);
842 selected_ecc = MV_BARCODE_QR_ECC_LOW;
845 selected_ecc = MV_BARCODE_QR_ECC_MEDIUM;
848 selected_ecc = MV_BARCODE_QR_ECC_QUARTILE;
851 selected_ecc = MV_BARCODE_QR_ECC_HIGH;
859 MEDIA_VISION_FUNCTION_LEAVE();
864 int select_version(void)
866 MEDIA_VISION_FUNCTION_ENTER();
869 while (sel_opt == 0) {
870 const int options[2] = {1, 40};
871 const char *names[2] = { "1..", "..40" };
872 sel_opt = show_menu("Select QR version:", options, names, 2);
873 if (sel_opt < 1 || sel_opt > 40)
877 MEDIA_VISION_FUNCTION_LEAVE();
882 generation_fcn_e select_gen_function(void)
884 generation_fcn_e ret_fcn_type = MV_TS_GENERATE_TO_IMAGE_FCN;
886 const int options[2] = { 1, 2 };
887 const char *names[2] = { "Generate to file", "Generate to source" };
889 MEDIA_VISION_FUNCTION_ENTER();
891 while (sel_opt == 0) {
892 sel_opt = show_menu("Select API function:", options, names, 2);
895 ret_fcn_type = MV_TS_GENERATE_TO_IMAGE_FCN;
898 ret_fcn_type = MV_TS_GENERATE_TO_SOURCE_FCN;
906 MEDIA_VISION_FUNCTION_LEAVE();
911 mv_barcode_image_format_e select_file_format(void)
913 mv_barcode_image_format_e image_format = MV_BARCODE_IMAGE_FORMAT_JPG;
915 const int options[3] = { 1, 2, 3 };
916 const char *names[3] = { "BMP", "JPG", "PNG" };
918 MEDIA_VISION_FUNCTION_ENTER();
920 while (sel_opt == 0) {
921 sel_opt = show_menu("Select file format:", options, names, 3);
924 image_format = MV_BARCODE_IMAGE_FORMAT_BMP;
927 image_format = MV_BARCODE_IMAGE_FORMAT_JPG;
930 image_format = MV_BARCODE_IMAGE_FORMAT_PNG;
938 MEDIA_VISION_FUNCTION_LEAVE();
945 MEDIA_VISION_FUNCTION_ENTER();
947 barcode_model_s detect_model = {
948 MV_BARCODE_UNDEFINED,
949 MV_BARCODE_QR_ECC_UNAVAILABLE,
950 MV_BARCODE_QR_MODE_UNAVAILABLE,
952 MV_BARCODE_IMAGE_FORMAT_PNG,
953 MEDIA_VISION_COLORSPACE_INVALID,
954 NULL, NULL, NULL, NULL };
956 while (input_string("Input file name to be analyzed:", 1024, &(detect_model.file_name)) == -1)
957 printf("Incorrect input! Try again.\n");
959 LOGI("Barcode input image has been specified");
961 mv_rectangle_s roi = { {0, 0}, 0, 0 };
963 while (input_int("Input x coordinate for ROI top left vertex:", 0, 10000, &(roi.point.x)) == -1)
964 printf("Incorrect input! Try again.\n");
966 while (input_int("Input y coordinate for ROI top left vertex:", 0, 10000, &(roi.point.y)) == -1)
967 printf("Incorrect input! Try again.\n");
969 while (input_int("Input ROI width:", 0, 10000, &(roi.width)) == -1)
970 printf("Incorrect input! Try again.\n");
972 while (input_int("Input ROI height:", 0, 10000, &(roi.height)) == -1)
973 printf("Incorrect input! Try again.\n");
975 LOGI("Region of interest (ROI) to detect barcode into has been specified");
977 while (input_string("Input file name to be generated:", 1024, &(detect_model.out_file_name)) == -1)
978 printf("Incorrect input! Try again.\n");
980 LOGI("Barcode output image has been specified");
982 const int options[11] = { MEDIA_VISION_COLORSPACE_Y800,
983 MEDIA_VISION_COLORSPACE_I420,
984 MEDIA_VISION_COLORSPACE_NV12,
985 MEDIA_VISION_COLORSPACE_YV12,
986 MEDIA_VISION_COLORSPACE_NV21,
987 MEDIA_VISION_COLORSPACE_YUYV,
988 MEDIA_VISION_COLORSPACE_UYVY,
989 MEDIA_VISION_COLORSPACE_422P,
990 MEDIA_VISION_COLORSPACE_RGB565,
991 MEDIA_VISION_COLORSPACE_RGB888,
992 MEDIA_VISION_COLORSPACE_RGBA };
993 const char *names[11] = { "Y800", "I420", "NV12", "YV12", "NV21",
994 "YUYV", "UYVY", "422P", "RGB565",
998 int sel_opt = show_menu("Select colorspace to test detector on:", options, names, 11);
999 if (sel_opt < MEDIA_VISION_COLORSPACE_Y800 ||
1000 sel_opt > MEDIA_VISION_COLORSPACE_RGBA) {
1003 detect_model.colorspace = (mv_colorspace_e)sel_opt;
1004 LOGI("User selection is %i", sel_opt);
1008 int err = detect_barcode(detect_model, roi);
1010 if (detect_model.file_name != NULL)
1011 free(detect_model.file_name);
1013 if (detect_model.out_file_name != NULL)
1014 free(detect_model.out_file_name);
1016 if (err != MEDIA_VISION_ERROR_NONE)
1017 LOGE("Barcode detection failed with error code (0x%08x)", err);
1019 MEDIA_VISION_FUNCTION_LEAVE();
1024 int perform_generate(void)
1026 MEDIA_VISION_FUNCTION_ENTER();
1028 barcode_model_s generate_model = {
1029 MV_BARCODE_UNDEFINED,
1030 MV_BARCODE_QR_ECC_UNAVAILABLE,
1031 MV_BARCODE_QR_MODE_UNAVAILABLE,
1033 MV_BARCODE_IMAGE_FORMAT_PNG,
1034 MEDIA_VISION_COLORSPACE_INVALID,
1035 NULL, NULL, NULL, NULL };
1037 generation_fcn_e gen_fcn = select_gen_function();
1038 generate_model.type = select_type();
1039 LOGI("Barcode type has been selected");
1041 if (generate_model.type == MV_BARCODE_QR) {
1042 generate_model.mode = select_mode();
1043 LOGI("Barcode encoding mode has been selected");
1044 generate_model.ecc = select_ecc();
1045 LOGI("Barcode ecc level has been selected");
1046 generate_model.version = select_version();
1047 LOGI("Barcode version has been selected");
1050 if (gen_fcn == MV_TS_GENERATE_TO_IMAGE_FCN) {
1051 generate_model.out_image_format = select_file_format();
1052 LOGI("Barcode output image format has been selected");
1055 while (input_string("Input message:", 7089, &generate_model.message) == -1)
1056 printf("Incorrect input! Try again.\n");
1058 LOGI("Barcode message has been specified");
1060 while (input_string("Input file name:", 1024, &generate_model.file_name) == -1)
1061 printf("Incorrect input! Try again.\n");
1063 LOGI("Barcode output file name has been specified");
1065 if (gen_fcn == MV_TS_GENERATE_TO_IMAGE_FCN) {
1066 while (input_size("Input image width:", 10000, &generate_model.width) == -1)
1067 printf("Incorrect input! Try again.\n");
1069 LOGI("Barcode output file width has been specified");
1071 while (input_size("Input image height:", 10000, &generate_model.height) == -1)
1072 printf("Incorrect input! Try again.\n");
1074 LOGI("Barcode output file height has been specified");
1078 gen_fcn == MV_TS_GENERATE_TO_IMAGE_FCN ?
1079 generate_barcode_to_image(generate_model) :
1080 generate_barcode_to_source(generate_model);
1082 if (generate_model.message != NULL)
1083 free(generate_model.message);
1085 if (generate_model.file_name != NULL)
1086 free(generate_model.file_name);
1088 if (err != MEDIA_VISION_ERROR_NONE) {
1089 LOGE("Barcode generation failed with error code (0x%08x)", err);
1090 printf("ERROR: Errors were occurred during barcode generation!!!\n");
1091 MEDIA_VISION_FUNCTION_LEAVE();
1095 LOGI("Barcode output file has been generated");
1096 printf("\nBarcode image was successfully generated.\n");
1098 MEDIA_VISION_FUNCTION_LEAVE();
1105 LOGI("Media Vision Testsuite is launched.");
1107 int err = MEDIA_VISION_ERROR_NONE;
1110 const int options[2] = { 1, 2 };
1111 const char *names[2] = { "Generate", "Detect" };
1113 while (sel_opt == 0) {
1114 sel_opt = show_menu("Select action:", options, names, 2);
1117 LOGI("Start the barcode generation flow");
1118 err = perform_generate();
1121 LOGI("Start the barcode detection flow");
1122 err = perform_detect();
1131 if (err != MEDIA_VISION_ERROR_NONE)
1132 printf("ERROR: Action is finished with error code: %i\n", err);
1135 const int options_last[2] = { 1, 2 };
1136 const char *names_last[2] = { "YES", "NO" };
1138 while (sel_opt == 0) {
1139 sel_opt = show_menu("Perform another action?", options_last, names_last, 2);
1152 LOGI("User selection is %i", sel_opt);
1154 sel_opt = (do_another == 1 ? 0 : sel_opt);
1157 LOGI("Media Vision Testsuite is closed.");