Skip to content

Commit 8a0c458

Browse files
committed
WASM: return the bytes member and fix issue with '<', '>', etc. chars
This is supposed to implement #584 in a 'proper' way (still not what I hoped it would be, see comment about `typed_memory_view`).
1 parent f9a65ef commit 8a0c458

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

wrappers/wasm/BarcodeReader.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ struct ReadResult
2020
{
2121
std::string format{};
2222
std::string text{};
23+
std::string bytes{};
2324
std::string error{};
2425
Position position{};
2526
std::string symbologyIdentifier{};
27+
28+
// The following seemed like the way to go, because the bytes member on the JS side would then automatically be a Uint8Array
29+
// but unfortunatelly, I don't understand something about the memory management, because the resulting array could contain 8 bytes of garbage.
30+
// ByteArray bytes;
31+
// emscripten::val get_bytes() const { return emscripten::val(emscripten::typed_memory_view(bytes.size(), bytes.data())); }
32+
// void set_bytes(emscripten::val) {} // dummy setter
2633
};
2734

2835
std::vector<ReadResult> readBarcodes(ImageView iv, bool tryHarder, const std::string& format, int maxSymbols)
@@ -42,15 +49,16 @@ std::vector<ReadResult> readBarcodes(ImageView iv, bool tryHarder, const std::st
4249
std::vector<ReadResult> readResults{};
4350
readResults.reserve(results.size());
4451

45-
for (auto& result : results) {
46-
readResults.push_back({ToString(result.format()), result.text(), ToString(result.error()), result.position(), result.symbologyIdentifier()});
52+
for (auto&& result : results) {
53+
readResults.push_back({ToString(result.format()), result.text(), std::string(result.bytes().asString()),
54+
ToString(result.error()), result.position(), result.symbologyIdentifier()});
4755
}
4856

4957
return readResults;
5058
} catch (const std::exception& e) {
51-
return {{"", "", e.what()}};
59+
return {{"", "", {}, e.what()}};
5260
} catch (...) {
53-
return {{"", "", "Unknown error"}};
61+
return {{"", "", {}, "Unknown error"}};
5462
}
5563
return {};
5664
}
@@ -62,7 +70,7 @@ std::vector<ReadResult> readBarcodesFromImage(int bufferPtr, int bufferLength, b
6270
stbi_load_from_memory(reinterpret_cast<const unsigned char*>(bufferPtr), bufferLength, &width, &height, &channels, 1),
6371
stbi_image_free);
6472
if (buffer == nullptr)
65-
return {{"", "", "Error loading image"}};
73+
return {{"", "", {}, "Error loading image"}};
6674

6775
return readBarcodes({buffer.get(), width, height, ImageFormat::Lum}, tryHarder, format, maxSymbols);
6876
}
@@ -89,6 +97,7 @@ EMSCRIPTEN_BINDINGS(BarcodeReader)
8997
value_object<ReadResult>("ReadResult")
9098
.field("format", &ReadResult::format)
9199
.field("text", &ReadResult::text)
100+
.field("bytes", &ReadResult::bytes)
92101
.field("error", &ReadResult::error)
93102
.field("position", &ReadResult::position)
94103
.field("symbologyIdentifier", &ReadResult::symbologyIdentifier);

wrappers/wasm/demo_cam_reader.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,16 @@ <h2>zxing-cpp/wasm live demo</h2>
111111
}
112112
}
113113

114+
function escapeTags(htmlStr) {
115+
return htmlStr.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
116+
}
117+
114118
const processFrame = function () {
115119
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
116120

117121
const code = readBarcodeFromCanvas(canvas, format.value, mode.value === 'true');
118122
if (code.format) {
119-
resultElement.innerText = code.format + ": " + code.text;
123+
resultElement.innerText = code.format + ": " + escapeTags(code.text);
120124
drawResult(code)
121125
} else {
122126
resultElement.innerText = "No barcode found";

wrappers/wasm/demo_reader.html

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@
7070
img.src = URL.createObjectURL(file)
7171
}
7272

73+
function escapeTags(htmlStr) {
74+
return htmlStr.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
75+
}
76+
77+
function str2u8a(str) {
78+
var bufView = new Uint8Array(new ArrayBuffer(str.length));
79+
for (var i = 0; i < bufView.length; i++) {
80+
bufView[i] = str.charCodeAt(i);
81+
}
82+
return bufView;
83+
}
84+
85+
function u8a2hex(bytes) {
86+
return bytes.reduce((a, b) => a + b.toString(16).padStart(2, '0') + ' ', '');
87+
}
88+
7389
function showResults(results) {
7490
const resultsDiv = document.getElementById("results");
7591
resultsDiv.innerHTML = "";
@@ -78,8 +94,11 @@
7894
}
7995
else {
8096
for (let i = 0; i < results.size(); i += 1) {
81-
const { error, format, text } = results.get(i);
82-
resultsDiv.innerHTML += "<li>Format: <strong>" + format + "</strong><pre>" + (text || '<font color="red">Error: ' + error + '</font>') + "</pre></li>";
97+
const { format, text, bytes, error } = results.get(i);
98+
resultsDiv.innerHTML += "<li>Format: <strong>" + format + "</strong>"
99+
+ "<pre>" + (escapeTags(text) || '<font color="red">Error: ' + error + '</font>') + "</pre>"
100+
+ "<pre>" + u8a2hex(str2u8a(bytes)) + "</pre>"
101+
+ "</li>";
83102
}
84103
}
85104
}

0 commit comments

Comments
 (0)
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