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_generate_open.h"
19 #include "mv_common_c.h"
20 #include "BarcodeGenerator.h"
22 #include <mv_private.h>
27 using namespace MediaVision::Barcode;
30 int alphanumToUpper(std::string& strToTransform)
32 std::string tempString = strToTransform;
33 std::transform(tempString.begin(), tempString.end(),
34 tempString.begin(), ::toupper);
36 if (std::string::npos != tempString.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:")) {
37 LOGE("Barcode message can't be converted according to support "
38 "alphanumeric (0..9, A..Z, space, $, %, *, +, -, ., /, :) "
39 "mode: %s", strToTransform.c_str());
40 return BARCODE_ERROR_INVALID_DATA;
43 LOGI("Barcode message was converted according to support alphanumeric "
44 "mode: %s -> %s", strToTransform.c_str(), tempString.c_str());
45 strToTransform = tempString;
46 return BARCODE_ERROR_NONE;
49 BarcodeType convertBarcodeType(mv_barcode_type_e type)
51 BarcodeType barcodeType = BARCODE_QR;
53 case MV_BARCODE_UPC_A:
54 barcodeType = BARCODE_UPCA;
56 case MV_BARCODE_UPC_E:
57 barcodeType = BARCODE_UPCE;
59 case MV_BARCODE_EAN_8:
60 barcodeType = BARCODE_EAN8;
62 case MV_BARCODE_EAN_13:
63 barcodeType = BARCODE_EAN13;
65 case MV_BARCODE_CODE128:
66 barcodeType = BARCODE_CODE128;
68 case MV_BARCODE_CODE39:
69 barcodeType = BARCODE_CODE39;
72 barcodeType = BARCODE_INTERLEAVE_2_5;
78 LOGI("Media vision barcode type has been converted to ZInt barcode type "
79 "(%i -> %i)", type, barcodeType);
83 BarcodeQREncodingMode convertEncodingMode(mv_barcode_qr_mode_e mode)
85 BarcodeQREncodingMode encodingMode = BARCODE_QR_MODE_ALPHANUMERIC;
88 case MV_BARCODE_QR_MODE_NUMERIC:
89 encodingMode = BARCODE_QR_MODE_NUMERIC;
91 case MV_BARCODE_QR_MODE_BYTE:
92 encodingMode = BARCODE_QR_MODE_BYTE;
94 case MV_BARCODE_QR_MODE_UTF8:
95 encodingMode = BARCODE_QR_MODE_UTF8;
101 LOGI("Media vision QRCode encoding mode has been converted to "
102 "ZInt encoding mode (%i -> %i)", mode, encodingMode);
106 BarcodeQRErrorCorrectionLevel convertECC(mv_barcode_qr_ecc_e ecc)
108 BarcodeQRErrorCorrectionLevel ecclevel = BARCODE_QR_ECC_LOW;
111 case MV_BARCODE_QR_ECC_MEDIUM:
112 ecclevel = BARCODE_QR_ECC_MEDIUM;
114 case MV_BARCODE_QR_ECC_QUARTILE:
115 ecclevel = BARCODE_QR_ECC_QUARTILE;
117 case MV_BARCODE_QR_ECC_HIGH:
118 ecclevel = BARCODE_QR_ECC_HIGH;
124 LOGI("Media vision ECC level has been converted to "
125 "ZInt ECC level (%i -> %i)", ecc, ecclevel);
129 int convertBarcodeError(int barcodeError)
131 int mvError = MEDIA_VISION_ERROR_NONE;
133 switch (barcodeError) {
134 case BARCODE_WARNING_INVALID_OPTION:
135 mvError = MEDIA_VISION_ERROR_INVALID_PARAMETER;
137 case BARCODE_ERROR_TOO_LONG:
138 mvError = MEDIA_VISION_ERROR_MSG_TOO_LONG;
140 case BARCODE_ERROR_INVALID_DATA:
141 mvError = MEDIA_VISION_ERROR_INVALID_DATA;
143 case BARCODE_ERROR_INVALID_CHECK:
144 mvError = MEDIA_VISION_ERROR_INVALID_PARAMETER;
146 case BARCODE_ERROR_INVALID_OPTION:
147 mvError = MEDIA_VISION_ERROR_INVALID_PARAMETER;
149 case BARCODE_ERROR_ENCODING_PROBLEM:
150 mvError = MEDIA_VISION_ERROR_INTERNAL;
152 case BARCODE_ERROR_FILE_ACCESS:
153 mvError = MEDIA_VISION_ERROR_PERMISSION_DENIED;
155 case BARCODE_ERROR_MEMORY:
156 mvError = MEDIA_VISION_ERROR_OUT_OF_MEMORY;
158 case BARCODE_ERROR_INVALID_PATH:
159 mvError = MEDIA_VISION_ERROR_INVALID_PATH;
164 LOGI("ZInt error code has been converted to the media vision error code "
165 "(%i -> (0x%08x))", barcodeError, mvError);
169 BarcodeImageFormat convertImageFormat(mv_barcode_image_format_e format)
171 BarcodeImageFormat imageFormat = BARCODE_IMAGE_PNG;
174 case MV_BARCODE_IMAGE_FORMAT_JPG:
175 imageFormat = BARCODE_IMAGE_JPG;
177 case MV_BARCODE_IMAGE_FORMAT_BMP:
178 imageFormat = BARCODE_IMAGE_BMP;
184 LOGI("Media vision image format has been converted to "
185 "internal image format (%i -> %i)", format, imageFormat);
189 } /* anonymous namespace */
191 int mv_barcode_generate_source_open(
192 mv_engine_config_h engine_cfg,
194 mv_barcode_type_e type,
195 mv_barcode_qr_mode_e qr_enc_mode,
196 mv_barcode_qr_ecc_e qr_ecc,
200 std::string messageStr = std::string(message);
202 if (qr_enc_mode == MV_BARCODE_QR_MODE_NUMERIC &&
203 messageStr.find_first_not_of("0123456789") != std::string::npos) {
204 LOGE("Barcode message can't be used according to support "
205 "numeric (0..9) mode: %s", messageStr.c_str());
206 return MEDIA_VISION_ERROR_INVALID_DATA;
209 int error = BARCODE_ERROR_NONE;
210 if (MV_BARCODE_QR == type &&
211 MV_BARCODE_QR_MODE_ALPHANUMERIC == qr_enc_mode) {
212 error = alphanumToUpper(messageStr);
213 if (BARCODE_ERROR_NONE != error) {
214 return convertBarcodeError(error);
218 unsigned char *imageBuffer = NULL;
219 unsigned int imageWidth = 0u;
220 unsigned int imageHeight = 0u;
221 unsigned int imageChannels = 0u;
225 char *fgcolour = NULL;
226 char *bgcolour = NULL;
228 if (engine_cfg != NULL) {
229 error = mv_engine_config_get_int_attribute(engine_cfg, "MV_BARCODE_GENERATE_ATTR_TEXT", &showText);
230 if (error != MEDIA_VISION_ERROR_NONE) {
231 LOGW("mv_engine_config_get_int_attribute failed");
235 if (showText == BARCODE_GEN_TEXT_VISIBLE && type == MV_BARCODE_QR) {
236 LOGW("QR code generation with visible text is not supported");
237 return MEDIA_VISION_ERROR_INVALID_OPERATION;
240 /* set color value */
241 error = mv_engine_config_get_string_attribute(engine_cfg, "MV_BARCODE_GENERATE_ATTR_COLOR_FRONT", &fgcolour);
242 if (error != MEDIA_VISION_ERROR_NONE) {
248 LOGW("mv_engine_config_get_string_attribute failed");
252 error = mv_engine_config_get_string_attribute(engine_cfg, "MV_BARCODE_GENERATE_ATTR_COLOR_BACK", &bgcolour);
253 if (error != MEDIA_VISION_ERROR_NONE) {
264 LOGW("mv_engine_config_get_string_attribute failed");
269 The input colorspace is RGB but the generators' is BGR.
270 Replace the value of R with that of B
273 fgcolour[0] = fgcolour[4];
277 fgcolour[1] = fgcolour[5];
281 bgcolour[0] = bgcolour[4];
285 bgcolour[1] = bgcolour[5];
289 error = BarcodeGenerator::generateBarcodeToBuffer(
295 convertBarcodeType(type),
296 convertEncodingMode(qr_enc_mode),
303 if (fgcolour != NULL) {
308 if (bgcolour != NULL) {
313 if (error != BARCODE_ERROR_NONE) {
314 LOGE("Barcode generation to the buffer failed");
315 if (NULL != imageBuffer) {
316 LOGI("Delete temporal buffer");
317 delete[] imageBuffer;
319 return convertBarcodeError(error);
322 const unsigned int imageBufferSize = imageWidth * imageHeight * imageChannels;
324 LOGI("Barcode has been generated to the buffer: "
325 "Buffer size = %ui x %ui; Channels = %ui; Message = %s",
326 imageWidth, imageHeight, imageChannels, messageStr.c_str());
328 error = mv_source_fill_by_buffer_c(
334 MEDIA_VISION_COLORSPACE_RGB888);
336 if (error != MEDIA_VISION_ERROR_NONE) {
337 LOGE("Meidiavision source fill by generated buffer failed");
340 if (NULL != imageBuffer) {
341 LOGI("Delete temporal buffer");
342 delete[] imageBuffer;
349 int mv_barcode_generate_image_open(
350 mv_engine_config_h engine_cfg,
354 mv_barcode_type_e type,
355 mv_barcode_qr_mode_e qr_enc_mode,
356 mv_barcode_qr_ecc_e qr_ecc,
358 const char *image_path,
359 mv_barcode_image_format_e image_format)
361 std::string messageStr = std::string(message);
363 if (qr_enc_mode == MV_BARCODE_QR_MODE_NUMERIC &&
364 messageStr.find_first_not_of("0123456789") != std::string::npos) {
365 LOGE("Barcode message can't be used according to support "
366 "numeric (0..9) mode: %s", messageStr.c_str());
367 return MEDIA_VISION_ERROR_INVALID_DATA;
370 if (NULL == image_path) {
371 LOGE("Can't save barcode image to the path[%p]. The path has to be specified", image_path);
372 return MEDIA_VISION_ERROR_INVALID_PATH;
375 int error = BARCODE_ERROR_NONE;
376 if (MV_BARCODE_QR == type &&
377 MV_BARCODE_QR_MODE_ALPHANUMERIC == qr_enc_mode) {
378 error = alphanumToUpper(messageStr);
379 if (BARCODE_ERROR_NONE != error) {
380 return convertBarcodeError(error);
386 char *fgcolour = NULL;
387 char *bgcolour = NULL;
389 if (engine_cfg != NULL) {
390 /* set visible text attribute */
391 error = mv_engine_config_get_int_attribute(engine_cfg, "MV_BARCODE_GENERATE_ATTR_TEXT", &showText);
392 if (error != MEDIA_VISION_ERROR_NONE) {
393 LOGW("mv_engine_config_get_int_attribute failed");
397 if (showText == BARCODE_GEN_TEXT_VISIBLE && type == MV_BARCODE_QR) {
398 LOGW("QR code generation with visible text is not supported");
399 return MEDIA_VISION_ERROR_INVALID_OPERATION;
402 /* set color value */
403 error = mv_engine_config_get_string_attribute(engine_cfg, "MV_BARCODE_GENERATE_ATTR_COLOR_FRONT", &fgcolour);
404 if (error != MEDIA_VISION_ERROR_NONE) {
410 LOGW("mv_engine_config_get_string_attribute failed");
414 error = mv_engine_config_get_string_attribute(engine_cfg, "MV_BARCODE_GENERATE_ATTR_COLOR_BACK", &bgcolour);
415 if (error != MEDIA_VISION_ERROR_NONE) {
426 LOGW("mv_engine_config_get_string_attribute failed");
431 The input colorspace is RGB but the generators' is BGR.
432 Replace the value of R with that of B
435 fgcolour[0] = fgcolour[4];
439 fgcolour[1] = fgcolour[5];
443 bgcolour[0] = bgcolour[4];
447 bgcolour[1] = bgcolour[5];
451 error = BarcodeGenerator::generateBarcodeToImage(
452 std::string(image_path),
453 convertImageFormat(image_format),
457 convertBarcodeType(type),
458 convertEncodingMode(qr_enc_mode),
465 if (fgcolour != NULL) {
470 if (bgcolour != NULL) {
475 if (error != BARCODE_ERROR_NONE) {
476 LOGE("Barcode generation to the image file failed");
478 LOGI("Barcode has been generated to the image: "
479 "Image size = %ui x %ui; Message = %s",
480 image_width, image_height, messageStr.c_str());
483 return convertBarcodeError(error);