Browse Source

Merge pull request #76 from axxel/patch-1

native_zxing.cpp: memcpy and FromUtf8 cleanup
pull/79/head
Khoren Markosyan 2 years ago committed by GitHub
parent
commit
fb72d19743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      src/native_zxing.cpp

31
src/native_zxing.cpp

@ -1,7 +1,6 @@
#include "common.h" #include "common.h"
#include "ReadBarcode.h" #include "ReadBarcode.h"
#include "MultiFormatWriter.h" #include "MultiFormatWriter.h"
#include "TextUtfEncoding.h"
#include "BitMatrix.h" #include "BitMatrix.h"
#include "native_zxing.h" #include "native_zxing.h"
@ -23,6 +22,7 @@ extern "C"
FUNCTION_ATTRIBUTE FUNCTION_ATTRIBUTE
char const *version() char const *version()
{ {
// TODO: use ZXING_VERSION_STR after zxing-cpp 2.1 is released
return "2.0.0"; return "2.0.0";
} }
@ -31,11 +31,7 @@ extern "C"
{ {
long long start = get_now(); long long start = get_now();
long length = width * height; ImageView image{reinterpret_cast<const uint8_t*>(bytes), width, height, ImageFormat::Lum};
auto *data = new uint8_t[length];
memcpy(data, bytes, length);
ImageView image{data, width, height, ImageFormat::Lum};
if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height) if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height)
{ {
image = image.cropped(width / 2 - cropWidth / 2, height / 2 - cropHeight / 2, cropWidth, cropHeight); image = image.cropped(width / 2 - cropWidth / 2, height / 2 - cropHeight / 2, cropWidth, cropHeight);
@ -43,12 +39,11 @@ extern "C"
DecodeHints hints = DecodeHints().setTryHarder(tryHarder).setTryRotate(tryRotate).setFormats(BarcodeFormat(format)).setTryInvert(tryInvert).setReturnErrors(true); DecodeHints hints = DecodeHints().setTryHarder(tryHarder).setTryRotate(tryRotate).setFormats(BarcodeFormat(format)).setTryInvert(tryInvert).setReturnErrors(true);
Result result = ReadBarcode(image, hints); Result result = ReadBarcode(image, hints);
delete[] bytes;
struct CodeResult code; struct CodeResult code;
resultToCodeResult(&code, result); resultToCodeResult(&code, result);
delete[] data;
delete[] bytes;
int evalInMillis = static_cast<int>(get_now() - start); int evalInMillis = static_cast<int>(get_now() - start);
code.duration = evalInMillis; code.duration = evalInMillis;
code.pos->imageWidth = width; code.pos->imageWidth = width;
@ -62,20 +57,14 @@ extern "C"
{ {
long long start = get_now(); long long start = get_now();
long length = width * height; ImageView image{reinterpret_cast<const uint8_t*>(bytes), width, height, ImageFormat::Lum};
auto *data = new uint8_t[length];
memcpy(data, bytes, length);
ImageView image{data, width, height, ImageFormat::Lum};
if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height) if (cropWidth > 0 && cropHeight > 0 && cropWidth < width && cropHeight < height)
{ {
image = image.cropped(width / 2 - cropWidth / 2, height / 2 - cropHeight / 2, cropWidth, cropHeight); image = image.cropped(width / 2 - cropWidth / 2, height / 2 - cropHeight / 2, cropWidth, cropHeight);
} }
DecodeHints hints = DecodeHints().setTryHarder(tryHarder).setTryRotate(tryRotate).setFormats(BarcodeFormat(format)).setTryInvert(tryInvert).setReturnErrors(true); DecodeHints hints = DecodeHints().setTryHarder(tryHarder).setTryRotate(tryRotate).setFormats(BarcodeFormat(format)).setTryInvert(tryInvert);
Results results = ReadBarcodes(image, hints); Results results = ReadBarcodes(image, hints);
delete[] bytes;
// remove invalid results
results.erase(remove_if(results.begin(), results.end(), [](Result const &result) { return !result.isValid(); }), results.end());
int evalInMillis = static_cast<int>(get_now() - start); int evalInMillis = static_cast<int>(get_now() - start);
platform_log("Read Barcode in: %d ms\n", evalInMillis); platform_log("Read Barcode in: %d ms\n", evalInMillis);
@ -92,8 +81,7 @@ extern "C"
codes[i] = code; codes[i] = code;
i++; i++;
} }
delete[] data;
delete[] bytes;
return {i, codes, evalInMillis}; return {i, codes, evalInMillis};
} }
@ -106,7 +94,7 @@ extern "C"
try try
{ {
auto writer = MultiFormatWriter(BarcodeFormat(format)).setMargin(margin).setEccLevel(eccLevel).setEncoding(CharacterSet::UTF8); auto writer = MultiFormatWriter(BarcodeFormat(format)).setMargin(margin).setEccLevel(eccLevel).setEncoding(CharacterSet::UTF8);
auto bitMatrix = writer.encode(TextUtfEncoding::FromUtf8(string(contents)), width, height); auto bitMatrix = writer.encode(contents, width, height);
result.data = ToMatrix<int8_t>(bitMatrix).data(); result.data = ToMatrix<int8_t>(bitMatrix).data();
result.length = bitMatrix.width() * bitMatrix.height(); result.length = bitMatrix.width() * bitMatrix.height();
result.isValid = true; result.isValid = true;
@ -138,6 +126,7 @@ extern "C"
code->format = static_cast<int>(result.format()); code->format = static_cast<int>(result.format());
// TODO: this needs to be allocated and coped as well (see text above). Will also require a delete in some flutter code, I assume
code->bytes = result.bytes().data(); code->bytes = result.bytes().data();
code->length = result.bytes().size(); code->length = result.bytes().size();

Loading…
Cancel
Save