From 492d1c848745d00bd959597089d5338791bb1387 Mon Sep 17 00:00:00 2001 From: Kwanghoon Son Date: Thu, 16 Feb 2023 18:27:08 +0900 Subject: Add DesignQR embed logo image 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 --- .../barcode_generator/src/BarcodeGenerator.cpp | 26 +++++++++++++++++----- test/testsuites/barcode/test_designqr.cpp | 14 ++++++++---- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/mv_barcode/barcode_generator/src/BarcodeGenerator.cpp b/mv_barcode/barcode_generator/src/BarcodeGenerator.cpp index 9b1e757a..28fd094d 100644 --- a/mv_barcode/barcode_generator/src/BarcodeGenerator.cpp +++ b/mv_barcode/barcode_generator/src/BarcodeGenerator.cpp @@ -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; } diff --git a/test/testsuites/barcode/test_designqr.cpp b/test/testsuites/barcode/test_designqr.cpp index 06b30e30..fa27b505 100644 --- a/test/testsuites/barcode/test_designqr.cpp +++ b/test/testsuites/barcode/test_designqr.cpp @@ -3,10 +3,12 @@ #include #define LARGE_COMB_SET 0 +#define IMAGE1_PATH MV_CONFIG_PATH "/res/inference/images/banana.jpg" + using namespace std; using DesignQRCombParams = tuple, 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 { @@ -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_RECT, MV_BARCODE_GENERATE_ATTR_SHAPE_ROUND_RECT, - MV_BARCODE_GENERATE_ATTR_SHAPE_CIRCLE }))); + MV_BARCODE_GENERATE_ATTR_SHAPE_CIRCLE }), + testing::ValuesIn(vector { "", IMAGE1_PATH }))); -- cgit v1.2.3