diff --git a/wrappers/wasm/BarcodeReader.cpp b/wrappers/wasm/BarcodeReader.cpp index d749aec4cf..2f216d5f3f 100644 --- a/wrappers/wasm/BarcodeReader.cpp +++ b/wrappers/wasm/BarcodeReader.cpp @@ -7,6 +7,7 @@ #include "ReadBarcode.h" #include +#include #include #include #include @@ -20,16 +21,10 @@ struct ReadResult { std::string format{}; std::string text{}; - std::string bytes{}; + emscripten::val 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 }; std::vector readBarcodes(ImageView iv, bool tryHarder, const std::string& format, int maxSymbols) @@ -49,9 +44,18 @@ std::vector readBarcodes(ImageView iv, bool tryHarder, const std::st std::vector readResults{}; readResults.reserve(results.size()); + thread_local const emscripten::val Uint8Array = emscripten::val::global("Uint8Array"); + for (auto&& result : results) { - readResults.push_back({ToString(result.format()), result.text(), std::string(result.bytes().asString()), - ToString(result.error()), result.position(), result.symbologyIdentifier()}); + const ByteArray& bytes = result.bytes(); + readResults.push_back({ + ToString(result.format()), + result.text(), + Uint8Array.new_(emscripten::typed_memory_view(bytes.size(), bytes.data())), + ToString(result.error()), + result.position(), + result.symbologyIdentifier() + }); } return readResults; diff --git a/wrappers/wasm/demo_reader.html b/wrappers/wasm/demo_reader.html index 99829f0a23..86b6af1358 100644 --- a/wrappers/wasm/demo_reader.html +++ b/wrappers/wasm/demo_reader.html @@ -74,14 +74,6 @@ return htmlStr.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } -function str2u8a(str) { - var bufView = new Uint8Array(new ArrayBuffer(str.length)); - for (var i = 0; i < bufView.length; i++) { - bufView[i] = str.charCodeAt(i); - } - return bufView; -} - function u8a2hex(bytes) { return bytes.reduce((a, b) => a + b.toString(16).padStart(2, '0') + ' ', ''); } @@ -97,7 +89,7 @@ const { format, text, bytes, error } = results.get(i); resultsDiv.innerHTML += "
  • Format: " + format + "" + "
    " + (escapeTags(text) || 'Error: ' + error + '') + "
    " - + "
    " + u8a2hex(str2u8a(bytes)) + "
    " + + "
    " + u8a2hex(bytes) + "
    " + "
  • "; } } pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy