Add DesignQR embed logo image 00/289500/1
authorKwanghoon Son <k.son@samsung.com>
Thu, 16 Feb 2023 09:27:08 +0000 (18:27 +0900)
committerKwanghoon Son <k.son@samsung.com>
Thu, 9 Mar 2023 02:00:00 +0000 (11:00 +0900)
QRcode generate MV_BARCODE_GENERATE_ATTR_EMBED_IMG_PATH and it's size
will be 1/5 of output image. This will 4% of total image size so ok with
qrcode ecc recovery.

Change-Id: I9bf902cada3d7a03d7dff424b676bb7344968b2a
Signed-off-by: Kwanghoon Son <k.son@samsung.com>
mv_barcode/barcode_generator/src/BarcodeGenerator.cpp
test/testsuites/barcode/test_designqr.cpp

index 9b1e757ab9c003d3b59bf4cae67a46c0d29b345f..28fd094d9c90789506914b26aac7642b8fc28910 100644 (file)
@@ -149,8 +149,8 @@ int createBarcode(const std::string &message, BarcodeType type, BarcodeQREncodin
        return error;
 }
 
-int writeBufferToImageFile(zint_symbol *symbol, const std::string &imageFileName, BarcodeImageFormat imageFormat,
-                                                  const int imageWidth, const int imageHeight)
+static int __write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, BarcodeImageFormat imageFormat,
+                                                                const int imageWidth, const int imageHeight, const std::string &logoPath)
 {
        if (imageWidth <= 0 || imageHeight <= 0) {
                LOGE("Barcode image size is invalid: %i x %i. Terminate write to "
@@ -204,6 +204,12 @@ int writeBufferToImageFile(zint_symbol *symbol, const std::string &imageFileName
        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);
 
+       if (!logoPath.empty()) {
+               cv::Mat logo = cv::imread(logoPath.c_str(), cv::IMREAD_COLOR);
+               cv::resize(logo, logo, cv::Size(imageWidth / 5, imageHeight / 5), 0, 0, cv::INTER_AREA);
+               logo.copyTo(image(cv::Rect(2 * logo.cols, 2 * logo.rows, logo.cols, logo.rows)));
+       }
+
        error = cv::imwrite(resultFilePath, image, compressionParams) ? BARCODE_ERROR_NONE : BARCODE_ERROR_INVALID_DATA;
 
        if (BARCODE_ERROR_NONE != error) {
@@ -230,23 +236,31 @@ int BarcodeGenerator::generateBarcodeToImage(const std::string &imageFileName, B
                return BARCODE_ERROR_ENCODING_PROBLEM;
        }
 
+       std::string logoPath;
        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;
+               goto zbarcode_delete;
        }
 
-       error = writeBufferToImageFile(symbol, imageFileName, imageFormat, imageWidth, imageHeight);
+       if (engineCfg) {
+               error = engineCfg->getStringAttribute(std::string(MV_BARCODE_GENERATE_ATTR_EMBED_IMG_PATH), &logoPath);
+               if (error != BARCODE_ERROR_NONE) {
+                       LOGE("getStringAttribute failed error : %d", error);
+                       goto zbarcode_delete;
+               }
+       }
+
+       error = __write_buffer_to_img(symbol, imageFileName, imageFormat, imageWidth, imageHeight, logoPath);
        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;
 }
 
index 06b30e30c0ad5ea798d9e5b89f1dc1a20f74a455..fa27b505166facacb23a44a713751dae4b638cb5 100644 (file)
@@ -3,10 +3,12 @@
 #include <mv_barcode_generate.h>
 
 #define LARGE_COMB_SET 0
+#define IMAGE1_PATH MV_CONFIG_PATH "/res/inference/images/banana.jpg"
+
 using namespace std;
 
 using DesignQRCombParams = tuple<string, mv_barcode_qr_mode_e, mv_barcode_qr_ecc_e, int, pair<string, string>, int,
-                                                                mv_barcode_generate_attr_shape_e, mv_barcode_generate_attr_shape_e>;
+                                                                mv_barcode_generate_attr_shape_e, mv_barcode_generate_attr_shape_e, string>;
 
 class CombinationsTest : public testing::TestWithParam<DesignQRCombParams>
 {
@@ -28,7 +30,7 @@ protected:
        mv_barcode_qr_mode_e qr_enc_mode = MV_BARCODE_QR_MODE_UTF8;
        mv_barcode_qr_ecc_e qr_ecc = MV_BARCODE_QR_ECC_LOW;
        int qr_version, img_size;
-       string test_file;
+       string test_file, logo_path;
        mv_barcode_image_format_e image_format = MV_BARCODE_IMAGE_FORMAT_PNG;
        string message, foreground_color, background_color;
        mv_barcode_generate_attr_shape_e data_shape, finder_shape;
@@ -37,7 +39,7 @@ protected:
 TEST_P(CombinationsTest, DesignQR)
 {
        forward_as_tuple(message, qr_enc_mode, qr_ecc, qr_version, tie(foreground_color, background_color), img_size,
-                                        data_shape, finder_shape) = GetParam();
+                                        data_shape, finder_shape, logo_path) = GetParam();
        ASSERT_EQ(mv_engine_config_set_string_attribute(engine_cfg, MV_BARCODE_GENERATE_ATTR_COLOR_FRONT,
                                                                                                        foreground_color.c_str()),
                          MEDIA_VISION_ERROR_NONE);
@@ -48,6 +50,9 @@ TEST_P(CombinationsTest, DesignQR)
                          MEDIA_VISION_ERROR_NONE);
        ASSERT_EQ(mv_engine_config_set_int_attribute(engine_cfg, MV_BARCODE_GENERATE_ATTR_FINDER_SHAPE, finder_shape),
                          MEDIA_VISION_ERROR_NONE);
+       ASSERT_EQ(mv_engine_config_set_string_attribute(engine_cfg, MV_BARCODE_GENERATE_ATTR_EMBED_IMG_PATH,
+                                                                                                       logo_path.c_str()),
+                         MEDIA_VISION_ERROR_NONE);
        EXPECT_EQ(mv_barcode_generate_image(engine_cfg, message.c_str(), img_size, img_size, type, qr_enc_mode, qr_ecc,
                                                                                qr_version, test_file.c_str(), image_format),
                          MEDIA_VISION_ERROR_NONE);
@@ -72,4 +77,5 @@ INSTANTIATE_TEST_CASE_P(
                                                                 MV_BARCODE_GENERATE_ATTR_SHAPE_RECT, MV_BARCODE_GENERATE_ATTR_SHAPE_CIRCLE }),
                                                 testing::ValuesIn(vector<mv_barcode_generate_attr_shape_e> {
                                                                 MV_BARCODE_GENERATE_ATTR_SHAPE_RECT, MV_BARCODE_GENERATE_ATTR_SHAPE_ROUND_RECT,
-                                                                MV_BARCODE_GENERATE_ATTR_SHAPE_CIRCLE })));
+                                                                MV_BARCODE_GENERATE_ATTR_SHAPE_CIRCLE }),
+                                                testing::ValuesIn(vector<string> { "", IMAGE1_PATH })));