Add BarcodeConfig
authorKwanghoon Son <k.son@samsung.com>
Thu, 27 Jun 2024 03:46:07 +0000 (12:46 +0900)
committerKwanghoon Son <k.son@samsung.com>
Fri, 5 Jul 2024 02:05:52 +0000 (11:05 +0900)
Collect barcode options to a single structure for the following reasons:

 - The common parsing code was scattered in multiple places,
making it difficult to read and understand.
 - Since the mv_engine_config was separate,
there was a need to parse it in the middle of the calculation logic.
 - function paramter is too long and almost same to next sub-function.

Change-Id: I9bf18ec88ed5bc48aff9ab3aabcb25c5ec0d83cd
Signed-off-by: Kwanghoon Son <k.son@samsung.com>
mv_barcode/barcode_generator/include/BarcodeGenerator.h
mv_barcode/barcode_generator/src/BarcodeGenerator.cpp
mv_barcode/barcode_generator/src/mv_barcode_generate.cpp
mv_barcode/barcode_generator/src/mv_barcode_generate_open.cpp

index 77d8093967ff6b63b1141b8033e33e9437616c22..1d11babb96bae86160476343c18bb92d40207d61 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "BarcodeOptions.h"
 #include <EngineConfig.h>
+#include <mv_barcode_generate.h>
 #include <mv_barcode_type.h>
 #include <opencv2/core/mat.hpp>
 #include <string>
@@ -33,6 +34,27 @@ namespace MediaVision
 {
 namespace Barcode
 {
+struct BarcodeConfig {
+       int update(mv_engine_config_h engine_cfg);
+
+       std::string message {};
+       mv_barcode_type_e type { MV_BARCODE_UNKNOWN };
+       mv_barcode_qr_mode_e qr_enc_mode { MV_BARCODE_QR_MODE_UNAVAILABLE };
+       mv_barcode_qr_ecc_e qr_ecc { MV_BARCODE_QR_ECC_UNAVAILABLE };
+       int qr_version {};
+       std::string image_path {};
+       int image_width {};
+       int image_height {};
+       mv_barcode_image_format_e image_format { MV_BARCODE_IMAGE_FORMAT_UNAVAILABLE };
+
+       //Engine config
+       int text_opt {};
+       std::string fgcolor { "000000" };
+       std::string bgcolor { "ffffff" };
+       mv_barcode_generate_attr_shape_e data_shape { MV_BARCODE_GENERATE_ATTR_SHAPE_RECT };
+       mv_barcode_generate_attr_shape_e finder_shape { MV_BARCODE_GENERATE_ATTR_SHAPE_RECT };
+};
+
 int createBarcode(const std::string &message, BarcodeType type, BarcodeQREncodingMode encodingMode,
                                  BarcodeQRErrorCorrectionLevel correctionLevel, int qrVersion, int showText, char *fgcolour,
                                  char *bgcolour, zint_symbol *symbol, Common::EngineConfig *engineCfg);
@@ -42,6 +64,8 @@ int write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, m
 
 BarcodeImageFormat convertImageFormat(mv_barcode_image_format_e format);
 
+void swapBR(uint16_t *color);
+
 } /* Barcode */
 } /* MediaVision */
 
index bb036dc5b03b3a16a9fce2a01ff27f61b09a2570..be72ccf861b4a5de27a7c04c89a4f66247910481 100644 (file)
@@ -318,5 +318,55 @@ int write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, m
 
        return error;
 }
+
+/*
+The input colorspace is RGB but the generators' is BGR.
+Replace the value of R with that of B
+*/
+void swapBR(uint16_t *color)
+{
+       std::swap(color[0], color[2]);
+}
+
+int BarcodeConfig::update(mv_engine_config_h engine_cfg)
+{
+       if (!engine_cfg)
+               return MEDIA_VISION_ERROR_NONE;
+
+       int showText;
+       int error = mv_engine_config_get_int_attribute(engine_cfg, MV_BARCODE_GENERATE_ATTR_TEXT, &showText);
+       if (error != MEDIA_VISION_ERROR_NONE) {
+               LOGE("mv_engine_config_get_int_attribute failed");
+               return error;
+       }
+
+       /* get color value */
+       char *fgcolor;
+       error = mv_engine_config_get_string_attribute(engine_cfg, MV_BARCODE_GENERATE_ATTR_COLOR_FRONT, &fgcolor);
+       if (error != MEDIA_VISION_ERROR_NONE) {
+               LOGE("mv_engine_config_get_string_attribute failed");
+               return error;
+       }
+       char *bgcolor;
+       error = mv_engine_config_get_string_attribute(engine_cfg, MV_BARCODE_GENERATE_ATTR_COLOR_BACK, &bgcolor);
+       if (error != MEDIA_VISION_ERROR_NONE) {
+               free(fgcolor);
+               LOGE("mv_engine_config_get_string_attribute failed");
+               return error;
+       }
+
+       swapBR((uint16_t *) fgcolor);
+       swapBR((uint16_t *) bgcolor);
+
+       text_opt = showText;
+       this->fgcolor = fgcolor;
+       this->bgcolor = bgcolor;
+
+       free(fgcolor);
+       free(bgcolor);
+
+       return MEDIA_VISION_ERROR_NONE;
+}
+
 } /* Barcode */
 } /* MediaVision */
index 873f5a1f8d18c9a0bd0187f5445b4f29271a4bdf..f50add9281b79a308668baeba1a5ee1c4ff9d3a4 100644 (file)
@@ -24,6 +24,8 @@
 #include "mv_barcode_generate_open.h"
 
 using namespace std;
+using namespace MediaVision::Barcode;
+
 /**
  * @file  mv_barcode_generate.c
  * @brief This file contains the porting layer for Media Vision barcode module.
@@ -67,13 +69,20 @@ int mv_barcode_generate_source(mv_engine_config_h engine_cfg, const char *messag
        MEDIA_VISION_INSTANCE_CHECK(image);
        MEDIA_VISION_CHECK_ERR(__check_barcode_param(message, type, qr_enc_mode, qr_ecc, qr_version), "Invalid parameter");
 
+       BarcodeConfig config { message, type, qr_enc_mode, qr_ecc, qr_version };
+
+       int ret = config.update(engine_cfg);
+       if (ret != MEDIA_VISION_ERROR_NONE) {
+               return ret;
+       }
+
        zint_symbol *symbol = ZBarcode_Create();
        if (symbol == NULL) {
                LOGE("ZBarcode creation failed");
                return MEDIA_VISION_ERROR_INTERNAL;
        }
 
-       int ret = mv_barcode_generate_symbol(engine_cfg, message, type, qr_enc_mode, qr_ecc, qr_version, symbol);
+       ret = mv_barcode_generate_symbol(engine_cfg, message, type, qr_enc_mode, qr_ecc, qr_version, symbol);
        if (ret != MEDIA_VISION_ERROR_NONE) {
                ZBarcode_Delete(symbol);
                return ret;
@@ -108,13 +117,20 @@ int mv_barcode_generate_image(mv_engine_config_h engine_cfg, const char *message
                return MEDIA_VISION_ERROR_INVALID_DATA;
        }
 
+       BarcodeConfig config { message, type, qr_enc_mode, qr_ecc, qr_version, image_path, image_height, image_format };
+
+       int ret = config.update(engine_cfg);
+       if (ret != MEDIA_VISION_ERROR_NONE) {
+               return ret;
+       }
+
        zint_symbol *symbol = ZBarcode_Create();
        if (symbol == NULL) {
                LOGE("ZBarcode creation failed");
                return MEDIA_VISION_ERROR_INTERNAL;
        }
 
-       int ret = mv_barcode_generate_symbol(engine_cfg, message, type, qr_enc_mode, qr_ecc, qr_version, symbol);
+       ret = mv_barcode_generate_symbol(engine_cfg, message, type, qr_enc_mode, qr_ecc, qr_version, symbol);
        if (ret != MEDIA_VISION_ERROR_NONE) {
                ZBarcode_Delete(symbol);
                return ret;
index edb71f82c8ff385b9313ee73bbe62ba788e5c2ac..0ba5eb4c95bb9f96f94c434ee3764f74038c88fe 100644 (file)
@@ -218,15 +218,6 @@ static int prepareMessageToGenerate(const char *message, mv_barcode_type_e type,
        return MEDIA_VISION_ERROR_NONE;
 }
 
-/*
-The input colorspace is RGB but the generators' is BGR.
-Replace the value of R with that of B
-*/
-static void swapBR(uint16_t *color)
-{
-       std::swap(color[0], color[2]);
-}
-
 static int getColorFromEngine(mv_engine_config_h engine_cfg, mv_barcode_type_e type, int &showText, char **fgcolour,
                                                          char **bgcolour)
 {