mv_barcode: Export zint symbol 93/304793/7
authorKwanghoon Son <k.son@samsung.com>
Thu, 18 Jan 2024 07:38:36 +0000 (16:38 +0900)
committerKwanghoon Son <k.son@samsung.com>
Mon, 29 Jan 2024 02:27:30 +0000 (11:27 +0900)
barcode_generate_{source,image} had a common code but was separated
into completely different codes generateBarcode{ToImage, Mat}.
Also zint symbol copied to mv_source or opencv mat multiple time,
it wasted performance. So just export one symbol and use it

Diagram is,
zint_symbol -> mv_source = mv_barcode_gen_source
            \> opencv mat -> post processing = mv_barcode_gen_image

Needs to merge BarcodeGenerator.h and mv_barcode_generate_open.h later

Change-Id: Ie77a3befd01324ecbc6890f0cda3d33845360678
Signed-off-by: Kwanghoon Son <k.son@samsung.com>
mv_barcode/barcode_generator/include/BarcodeGenerator.h
mv_barcode/barcode_generator/include/mv_barcode_generate_open.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 199dfb8ce008873652d2c597f87c0d354d888b88..4aa0ec2c1ca74b829a69e3c6f94b3a5b4b4e5d99 100644 (file)
 #define __MEDIA_VISION_BARCODE_GENERATOR_H__
 
 #include "BarcodeOptions.h"
-
+#include <zint.h>
 #include <string>
 #include <EngineConfig.h>
 #include <opencv2/core/mat.hpp>
+#include <mv_barcode_type.h>
 
 /**
  * @file BarcodeGenerator.h
@@ -32,61 +33,14 @@ namespace MediaVision
 {
 namespace Barcode
 {
-/**
- * @brief This class implements barcode generation.
- * @details 1D Barcodes and 2D QR codes are supported.
- *
- * @since_tizen 2.4
- */
-class BarcodeGenerator
-{
-public:
-       /**
-        * @brief This method generates Barcodes image according to options.
-        *
-        * @since_tizen 2.4
-        * @param [in] imageFileName    Image file name which will be generated
-        * @param [in] imageFormat      Image file format which will be generated
-        * @param [in] imageWidth       Image file width which will be generated
-        * @param [in] imageHeight      Image file height which will be generated
-        * @param [in] message          Input message to be encoded
-        * @param [in] type             Barcode type (1D barcode or 2D QR code)
-        * @param [in] encodingMode     Encoding mode (for QR codes only)
-        * @param [in] correctionLevel  Error correction level (for QR codes only)
-        * @param [in] qrVersion        QR code version (1 ~ 40, 0 for 1D barcodes)
-        * @param [in] showText         Show text or not
-        * @param [in] fgcolour         Foreground colour of barcode
-        * @param [in] bgcolour         Background colour of barcode
-        * @return BARCODE_ERROR_NONE from BarcodeError which is 0 if success,
-        *         BarcodeError value otherwise
-        */
-       static int generateBarcodeToImage(const std::string &imageFileName, BarcodeImageFormat imageFormat,
-                                                                         const int imageWidth, const int imageHeight, const std::string &message,
-                                                                         BarcodeType type, Common::EngineConfig *engineCfg,
-                                                                         BarcodeQREncodingMode encodingMode = BARCODE_QR_MODE_UNAVAILABLE,
-                                                                         BarcodeQRErrorCorrectionLevel correctionLevel = BARCODE_QR_ECC_UNAVAILABLE,
-                                                                         int qrVersion = 0, int showText = 0, char *fgcolour = NULL, char *bgcolur = NULL);
+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);
+
+int write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, mv_barcode_image_format_e image_format,
+                                               const int imageWidth, const int imageHeight, mv_engine_config_h engine_cfg);
 
-       /**
-        * @brief This method generates Barcodes image buffer according to options.
-        *
-        * @since_tizen 2.4
-        * @param [in] message          Input message to be encoded
-        * @param [in] type             Barcode type (1D barcode or 2D QR code)
-        * @param [in] encodingMode     Encoding mode (for QR codes only)
-        * @param [in] correctionLevel  Error correction level (for QR codes only)
-        * @param [in] qrVersion        QR code version (1 ~ 40, 0 for 1D barcodes)
-        * @param [in] showText         Show text or not
-        * @param [in] fgcolour         Foreground colour of barcode
-        * @param [in] bgcolour         Background colour of barcode
-        * @param [out] buf             Opencv Mat image buffer to be generated
-        * @return BARCODE_ERROR_NONE from BarcodeError which is 0 if success,
-        *         BarcodeError value otherwise
-        */
-       static int generateBarcodeMat(const std::string &message, BarcodeType type, Common::EngineConfig *engineCfg,
-                                                                 BarcodeQREncodingMode encodingMode, BarcodeQRErrorCorrectionLevel correctionLevel,
-                                                                 int qrVersion, int showText, char *fgcolour, char *bgcolur, cv::Mat &buf);
-};
+BarcodeImageFormat convertImageFormat(mv_barcode_image_format_e format);
 
 } /* Barcode */
 } /* MediaVision */
index 7fe9c5398158e6441fee3ec7e8c249c066624e74..8ecd486e301303fdcec474c65a3ae77b2496494d 100644 (file)
 #define __MEDIA_VISION_BARCODE_GENERATE_OPEN_H__
 
 #include "mv_barcode_generate.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
+#include "BarcodeGenerator.h"
+#include <zint.h>
 
 /**
  * @file  mv_barcode_generate_open.h
@@ -44,8 +42,8 @@ extern "C" {
  *                              @ref MV_BARCODE_QR_ECC_UNAVAILABLE)
  * @param [in]  qr_version      QR code version (for 1D barcodes set this
  *                              parameter to 0)
- * @param [in, out] image       The media source handle which will be used to
- *                              fill by the buffer with generated image
+ * @param [out] symbol       The media source handle which will be used to
+ *                              fill by the buffer with generated symbol
  * @return @c 0 on success, otherwise a negative error value
  * @retval #MEDIA_VISION_ERROR_NONE Successful
  * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
@@ -54,45 +52,8 @@ extern "C" {
  *
  * @see mv_barcode_generate_image_open()
  */
-int mv_barcode_generate_source_open(mv_engine_config_h engine_cfg, const char *message, mv_barcode_type_e type,
-                                                                       mv_barcode_qr_mode_e qr_enc_mode, mv_barcode_qr_ecc_e qr_ecc, int qr_version,
-                                                                       mv_source_h image);
-
-/**
- * @brief Generates image file with barcode.
- *
- * @since_tizen 2.4
- * @param [in] engine_cfg      The handle to the configuration of the engine
- * @param [in] message         The message to be encoded in the barcode
- * @param [in] image_width     The width of the generated image
- * @param [in] image_height    The height of the generated image
- * @param [in] type            Type of the barcode to be generated
- * @param [in] qr_enc_mode     Encoding mode for the message (only for QR codes;
- *                             for 1D barcodes set this parameter to
- *                             @ref MV_BARCODE_QR_MODE_UNAVAILABLE)
- * @param [in] qr_ecc          Error correction level (only for QR codes; for
- *                             1D barcodes set this parameter to
- *                             @ref MV_BARCODE_QR_ECC_UNAVAILABLE)
- * @param [in] qr_version      QR code version (for 1D barcodes set this
- *                             parameter to 0)
- * @param [in] image_path      The path to the file that has to be generated
- * @param [in] image_format    The format of the output image
- * @return @c 0 on success, otherwise a negative error value
- * @retval #MEDIA_VISION_ERROR_NONE Successful
- * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MEDIA_VISION_ERROR_MSG_TOO_LONG Too long or short message
- * @retval #MEDIA_VISION_ERROR_PERMISSION_DENIED Permission denied
- * @retval #MEDIA_VISION_ERROR_INVALID_PATH Invalid path
- *
- * @see mv_barcode_generate_source_open()
- */
-int mv_barcode_generate_image_open(mv_engine_config_h engine_cfg, const char *message, int image_width,
-                                                                  int image_height, mv_barcode_type_e type, mv_barcode_qr_mode_e qr_enc_mode,
-                                                                  mv_barcode_qr_ecc_e qr_ecc, int qr_version, const char *image_path,
-                                                                  mv_barcode_image_format_e image_format);
-
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
+int mv_barcode_generate_symbol(mv_engine_config_h engine_cfg, const char *message, mv_barcode_type_e type,
+                                                          mv_barcode_qr_mode_e qr_enc_mode, mv_barcode_qr_ecc_e qr_ecc, int qr_version,
+                                                          zint_symbol *symbol);
 
 #endif /* __MEDIA_VISION_BARCODE_GENERATE_OPEN_H__ */
index 481adbabdd5c4350cc4aab92a5afeb01e03755d6..2ad8be8a061d30a23b48e3ea7cd91f0740568e22 100644 (file)
@@ -36,6 +36,27 @@ namespace MediaVision
 {
 namespace Barcode
 {
+BarcodeImageFormat convertImageFormat(mv_barcode_image_format_e format)
+{
+       BarcodeImageFormat imageFormat = BARCODE_IMAGE_PNG;
+
+       switch (format) {
+       case MV_BARCODE_IMAGE_FORMAT_JPG:
+               imageFormat = BARCODE_IMAGE_JPG;
+               break;
+       case MV_BARCODE_IMAGE_FORMAT_BMP:
+               imageFormat = BARCODE_IMAGE_BMP;
+               break;
+       default:
+               break;
+       }
+
+       LOGI("Media vision image format has been converted to "
+                "internal image format (%i -> %i)",
+                format, imageFormat);
+       return imageFormat;
+}
+
 int getFormatEncodingInfo(BarcodeImageFormat imageFormat, std::vector<std::string> &extensions,
                                                  std::vector<int> &compressionParams)
 {
@@ -231,8 +252,8 @@ static int __draw_logo(cv::Mat &image, Common::EngineConfig *engineCfg)
        return error;
 }
 
-static int __write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, BarcodeImageFormat imageFormat,
-                                                                const int imageWidth, const int imageHeight, Common::EngineConfig *engineCfg)
+int write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, mv_barcode_image_format_e image_format,
+                                               const int imageWidth, const int imageHeight, mv_engine_config_h engine_cfg)
 {
        /* find directory */
        std::string prefix_imageFileName = imageFileName.substr(0, imageFileName.find_last_of('/'));
@@ -247,7 +268,7 @@ static int __write_buffer_to_img(zint_symbol *symbol, const std::string &imageFi
        /* check current extension */
        std::vector<std::string> expectedExtensions;
        std::vector<int> compressionParams;
-
+       auto imageFormat = convertImageFormat(image_format);
        int error = getFormatEncodingInfo(imageFormat, expectedExtensions, compressionParams);
 
        if (BARCODE_ERROR_NONE != error || expectedExtensions.empty()) {
@@ -279,6 +300,7 @@ static int __write_buffer_to_img(zint_symbol *symbol, const std::string &imageFi
        cv::Mat image(symbol->bitmap_height, symbol->bitmap_width, CV_8UC3, symbol->bitmap);
        cv::resize(image, image, cv::Size(imageWidth, imageHeight), 0, 0, cv::INTER_AREA);
 
+       auto engineCfg = static_cast<Common::EngineConfig *>(engine_cfg);
        if (engineCfg) {
                error = __draw_logo(image, engineCfg);
                if (error != MEDIA_VISION_ERROR_NONE) {
@@ -296,70 +318,5 @@ static int __write_buffer_to_img(zint_symbol *symbol, const std::string &imageFi
 
        return error;
 }
-
-int BarcodeGenerator::generateBarcodeToImage(const std::string &imageFileName, BarcodeImageFormat imageFormat,
-                                                                                        const int imageWidth, const int imageHeight, const std::string &message,
-                                                                                        BarcodeType type, Common::EngineConfig *engineCfg,
-                                                                                        BarcodeQREncodingMode encodingMode,
-                                                                                        BarcodeQRErrorCorrectionLevel correctionLevel, int qrVersion, int showText,
-                                                                                        char *fgcolour, char *bgcolour)
-{
-       zint_symbol *symbol = ZBarcode_Create();
-
-       if (symbol == NULL) {
-               LOGE("ZBarcode creation failed");
-               return BARCODE_ERROR_ENCODING_PROBLEM;
-       }
-
-       int error = createBarcode(message, type, encodingMode, correctionLevel, qrVersion, showText, fgcolour, bgcolour,
-                                                         symbol, engineCfg);
-
-       if (error != BARCODE_ERROR_NONE) {
-               LOGE("Barcode creation failed, clean memory");
-               goto zbarcode_delete;
-       }
-
-       error = __write_buffer_to_img(symbol, imageFileName, imageFormat, imageWidth, imageHeight, engineCfg);
-       if (error != BARCODE_ERROR_NONE)
-               LOGE("Barcode [%s] file write fail, clean memory", imageFileName.c_str());
-       else
-               LOGI("Barcode image [%s] is successfully generated, clean memory", imageFileName.c_str());
-
-zbarcode_delete:
-       ZBarcode_Delete(symbol);
-       return error;
-}
-
-int BarcodeGenerator::generateBarcodeMat(const std::string &message, BarcodeType type, Common::EngineConfig *engineCfg,
-                                                                                BarcodeQREncodingMode encodingMode,
-                                                                                BarcodeQRErrorCorrectionLevel correctionLevel, int qrVersion, int showText,
-                                                                                char *fgcolour, char *bgcolour, cv::Mat &buf)
-{
-       zint_symbol *symbol = ZBarcode_Create();
-
-       if (symbol == NULL) {
-               LOGE("ZBarcode creation failed");
-
-               return BARCODE_ERROR_ENCODING_PROBLEM;
-       }
-
-       int error = createBarcode(message, type, encodingMode, correctionLevel, qrVersion, showText, fgcolour, bgcolour,
-                                                         symbol, engineCfg);
-
-       if (error != BARCODE_ERROR_NONE) {
-               LOGE("Barcode creation failed, clean memory");
-               ZBarcode_Delete(symbol);
-               return error;
-       }
-
-       /* fill output buffer */
-       buf = cv::Mat(symbol->bitmap_height, symbol->bitmap_width, CV_8UC3, symbol->bitmap).clone();
-
-       LOGI("Barcode buffer has been successfully generated, clean memory");
-       ZBarcode_Delete(symbol);
-
-       return BARCODE_ERROR_NONE;
-}
-
 } /* Barcode */
 } /* MediaVision */
index 7e95534cf90b006cce85f46a87fc3322ec4c2c2e..08e40a08c3866c5c57db18d6a79bacd37fd8be84 100644 (file)
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
+#include <string>
 #include <system_info.h>
-
+#include <zint.h>
 #include "mv_private.h"
 #include "mv_feature_key.h"
 #include "mv_barcode_generate.h"
@@ -23,6 +24,7 @@
 /* Include headers of open barcode generate module here. */
 #include "mv_barcode_generate_open.h"
 
+using namespace std;
 /**
  * @file  mv_barcode_generate.c
  * @brief This file contains the porting layer for Media Vision barcode module.
@@ -66,9 +68,22 @@ 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");
 
-       /* Use open barcode generate functionality here. */
-       int ret = mv_barcode_generate_source_open(engine_cfg, message, type, qr_enc_mode, qr_ecc, qr_version, image);
+       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);
+       if (ret != MEDIA_VISION_ERROR_NONE) {
+               ZBarcode_Delete(symbol);
+               return ret;
+       }
 
+       ret = mv_source_fill_by_buffer(image, (unsigned char *) symbol->bitmap,
+                                                                  symbol->bitmap_width * symbol->bitmap_height * 3, symbol->bitmap_width,
+                                                                  symbol->bitmap_height, MEDIA_VISION_COLORSPACE_RGB888);
+       ZBarcode_Delete(symbol);
        MEDIA_VISION_FUNCTION_LEAVE();
        return ret;
 }
@@ -91,10 +106,22 @@ int mv_barcode_generate_image(mv_engine_config_h engine_cfg, const char *message
                return MEDIA_VISION_ERROR_INVALID_PARAMETER;
        }
 
-       /* Use open barcode generate functionality here. */
-       int ret = mv_barcode_generate_image_open(engine_cfg, message, image_width, image_height, type, qr_enc_mode, qr_ecc,
-                                                                                        qr_version, image_path, image_format);
+       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);
+       if (ret != MEDIA_VISION_ERROR_NONE) {
+               ZBarcode_Delete(symbol);
+               return ret;
+       }
+
+       ret = MediaVision::Barcode::write_buffer_to_img(symbol, string(image_path), image_format, image_width, image_height,
+                                                                                                       engine_cfg);
 
+       ZBarcode_Delete(symbol);
        MEDIA_VISION_FUNCTION_LEAVE();
        return ret;
 }
index ab29f150d11d8d4941658ff38e0a0360e683d08d..7dc507e0bcebc09a73924021e969ac95f19f3702 100644 (file)
@@ -192,27 +192,6 @@ int convertBarcodeError(int barcodeError)
        return mvError;
 }
 
-BarcodeImageFormat convertImageFormat(mv_barcode_image_format_e format)
-{
-       BarcodeImageFormat imageFormat = BARCODE_IMAGE_PNG;
-
-       switch (format) {
-       case MV_BARCODE_IMAGE_FORMAT_JPG:
-               imageFormat = BARCODE_IMAGE_JPG;
-               break;
-       case MV_BARCODE_IMAGE_FORMAT_BMP:
-               imageFormat = BARCODE_IMAGE_BMP;
-               break;
-       default:
-               break;
-       }
-
-       LOGI("Media vision image format has been converted to "
-                "internal image format (%i -> %i)",
-                format, imageFormat);
-       return imageFormat;
-}
-
 static int prepareMessageToGenerate(const char *message, mv_barcode_type_e type, mv_barcode_qr_mode_e qr_enc_mode,
                                                                        std::string &messageStr)
 {
@@ -285,9 +264,9 @@ static int getColorFromEngine(mv_engine_config_h engine_cfg, mv_barcode_type_e t
        return MEDIA_VISION_ERROR_NONE;
 }
 
-int mv_barcode_generate_source_open(mv_engine_config_h engine_cfg, const char *message, mv_barcode_type_e type,
-                                                                       mv_barcode_qr_mode_e qr_enc_mode, mv_barcode_qr_ecc_e qr_ecc, int qr_version,
-                                                                       mv_source_h image)
+int mv_barcode_generate_symbol(mv_engine_config_h engine_cfg, const char *message, mv_barcode_type_e type,
+                                                          mv_barcode_qr_mode_e qr_enc_mode, mv_barcode_qr_ecc_e qr_ecc, int qr_version,
+                                                          zint_symbol *symbol)
 {
        std::string messageStr;
        int error = prepareMessageToGenerate(message, type, qr_enc_mode, messageStr);
@@ -307,71 +286,15 @@ int mv_barcode_generate_source_open(mv_engine_config_h engine_cfg, const char *m
                LOGE("getColorFromEngine failed");
                return error;
        }
-
-       cv::Mat buf;
-       error = BarcodeGenerator::generateBarcodeMat(messageStr, convertBarcodeType(type), engineCfg,
-                                                                                                convertEncodingMode(qr_enc_mode), convertECC(qr_ecc), qr_version,
-                                                                                                showText, fgcolour, bgcolour, buf);
+       error = createBarcode(message, convertBarcodeType(type), convertEncodingMode(qr_enc_mode), convertECC(qr_ecc),
+                                                 qr_version, showText, fgcolour, bgcolour, symbol, engineCfg);
 
        free(fgcolour);
        free(bgcolour);
-
        if (error != BARCODE_ERROR_NONE) {
-               LOGE("Barcode generation to the buffer failed");
-               return convertBarcodeError(error);
+               const auto channel = 3;
+               LOGI("Barcode has been generated to the buffer: Buffer size = %ui x %ui; Channels = %ui; Message = %s",
+                        symbol->bitmap_width, symbol->bitmap_height, channel, messageStr.c_str());
        }
-
-       LOGI("Barcode has been generated to the buffer: Buffer size = %ui x %ui; Channels = %ui; Message = %s", buf.cols,
-                buf.rows, buf.channels(), messageStr.c_str());
-
-       error = mv_source_fill_by_buffer(image, buf.data, buf.total() * buf.elemSize(), buf.cols, buf.rows,
-                                                                        MEDIA_VISION_COLORSPACE_RGB888);
-
-       if (error != MEDIA_VISION_ERROR_NONE)
-               LOGE("Meidiavision source fill by generated buffer failed");
-
-       return error;
-}
-
-int mv_barcode_generate_image_open(mv_engine_config_h engine_cfg, const char *message, int image_width,
-                                                                  int image_height, mv_barcode_type_e type, mv_barcode_qr_mode_e qr_enc_mode,
-                                                                  mv_barcode_qr_ecc_e qr_ecc, int qr_version, const char *image_path,
-                                                                  mv_barcode_image_format_e image_format)
-{
-       std::string messageStr;
-       int error = prepareMessageToGenerate(message, type, qr_enc_mode, messageStr);
-       if (error != MEDIA_VISION_ERROR_NONE) {
-               LOGE("prepareMessageToGenerate failed");
-               return error;
-       }
-
-       int showText = 0;
-       char *fgcolour = NULL;
-       char *bgcolour = NULL;
-
-       auto engineCfg = static_cast<MediaVision::Common::EngineConfig *>(engine_cfg);
-
-       error = getColorFromEngine(engine_cfg, type, showText, &fgcolour, &bgcolour);
-       if (error != MEDIA_VISION_ERROR_NONE) {
-               LOGE("getColorFromEngine failed");
-               return error;
-       }
-
-       error = BarcodeGenerator::generateBarcodeToImage(std::string(image_path), convertImageFormat(image_format),
-                                                                                                        image_width, image_height, messageStr, convertBarcodeType(type),
-                                                                                                        engineCfg, convertEncodingMode(qr_enc_mode), convertECC(qr_ecc),
-                                                                                                        qr_version, showText, fgcolour, bgcolour);
-
-       free(fgcolour);
-       free(bgcolour);
-
-       if (error != BARCODE_ERROR_NONE) {
-               LOGE("Barcode generation to the image file failed");
-       } else {
-               LOGI("Barcode has been generated to the image: "
-                        "Image size = %ui x %ui; Message = %s",
-                        image_width, image_height, messageStr.c_str());
-       }
-
        return convertBarcodeError(error);
 }