-
-
Notifications
You must be signed in to change notification settings - Fork 464
Add hex value to wasm result #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for the effort to contribute. I don't think passing the |
@axxel Trying to pass the binary data was my first attempt, but i failed and wasnt able to do it and i still am failing. This pr exists out of pure necessarity, because i really need the binary, not the string data. Maybe we can take this changes temporarily until someone manages to find a better solution (passing the bin directly)? |
This is supposed to implement #584 in a 'proper' way (still not what I hoped it would be, see comment about `typed_memory_view`).
Please have a look at my last commit. I tested it with a few binary samples. If this works for you, please close the PR. |
@axxel Hey can you elaborate on your comments here:
Because I just tried the memory views method and I didn't see any garbage bytes returned. Do you have any failing samples to share? I really think this should be the correct way to pass binaries to the JS side. |
@Sec-ant thanks for chiming in. I completely agree with you. To reproduce the problem, apply the patch below, then start diff --git a/wrappers/wasm/BarcodeReader.cpp b/wrappers/wasm/BarcodeReader.cpp
index d749aec4..0a2da8ad 100644
--- a/wrappers/wasm/BarcodeReader.cpp
+++ b/wrappers/wasm/BarcodeReader.cpp
@@ -20,16 +20,16 @@ struct ReadResult
{
std::string format{};
std::string text{};
- std::string bytes{};
+// std::string bytes{};
+ ByteArray bytes;
std::string error{};
Position position{};
std::string symbologyIdentifier{};
// The following seemed like the way to go, because the bytes member on the JS side would then automatically be a Uint8Array
// but unfortunatelly, I don't understand something about the memory management, because the resulting array could contain 8 bytes of garbage.
-// ByteArray bytes;
-// emscripten::val get_bytes() const { return emscripten::val(emscripten::typed_memory_view(bytes.size(), bytes.data())); }
-// void set_bytes(emscripten::val) {} // dummy setter
+ emscripten::val get_bytes() const { return emscripten::val(emscripten::typed_memory_view(bytes.size(), bytes.data())); }
+ void set_bytes(emscripten::val) {} // dummy setter
};
std::vector<ReadResult> readBarcodes(ImageView iv, bool tryHarder, const std::string& format, int maxSymbols)
@@ -50,7 +50,7 @@ std::vector<ReadResult> readBarcodes(ImageView iv, bool tryHarder, const std::st
readResults.reserve(results.size());
for (auto&& result : results) {
- readResults.push_back({ToString(result.format()), result.text(), std::string(result.bytes().asString()),
+ readResults.push_back({ToString(result.format()), result.text(), std::move(result.bytes()),
ToString(result.error()), result.position(), result.symbologyIdentifier()});
}
@@ -97,7 +97,7 @@ EMSCRIPTEN_BINDINGS(BarcodeReader)
value_object<ReadResult>("ReadResult")
.field("format", &ReadResult::format)
.field("text", &ReadResult::text)
- .field("bytes", &ReadResult::bytes)
+ .field("bytes", &ReadResult::get_bytes, &ReadResult::set_bytes)
.field("error", &ReadResult::error)
.field("position", &ReadResult::position)
.field("symbologyIdentifier", &ReadResult::symbologyIdentifier);
diff --git a/wrappers/wasm/demo_reader.html b/wrappers/wasm/demo_reader.html
index 99829f0a..86134626 100644
--- a/wrappers/wasm/demo_reader.html
+++ b/wrappers/wasm/demo_reader.html
@@ -97,7 +97,7 @@ function showResults(results) {
const { format, text, bytes, error } = results.get(i);
resultsDiv.innerHTML += "<li>Format: <strong>" + format + "</strong>"
+ "<pre>" + (escapeTags(text) || '<font color="red">Error: ' + error + '</font>') + "</pre>"
- + "<pre>" + u8a2hex(str2u8a(bytes)) + "</pre>"
+ + "<pre>" + u8a2hex(bytes) + "</pre>"
+ "</li>";
}
} |
@axxel OK I can reproduce this problem by simply calling the
If "memory views on stack data" is the case, then I can only think of two ways to solve this:
I don't think barcodes can hold much raw binary data so I think what you did is the best option we have. |
@axxel Most Cpp things are beyond my knowledge but what do you think of this writeup? Keeping the memory views binding while hiding memory management from the JS side would be great. Edit: I changed the code accordingly and tested locally, I noticed no garbage bytes returned. I submitted a PR: #588, please check if there's any problem I'm not aware of. 😄 |
The barcode may not always contain readable text data. Therefore the "hex" field contains a hexadecimal representation of the barcode "00 0a 0b..."
No idea how i could link raw bytes but i guess hex strings are okay