Replies: 2 comments 3 replies
-
i figured this discussion may be helpful, you can call native api's. |
Beta Was this translation helpful? Give feedback.
2 replies
-
I had a fiddle with some code proof of concept code. https://stackoverflow.com/a/78089783, which uses the barcode scanner API. This has poor browser support, but there is a polyfill for it. Amazingly it seems to work, at least on Desktop with Camera. Proof of concept code only though. I can't think right now, but wondering:
#[component]
fn Barcode() -> Element {
use_effect(move || {
document::eval(
r#"
console.log("00000");
//WebAssembly polyfill for some browsers
try { window['BarcodeDetector'].getSupportedFormats() }
catch { window['BarcodeDetector'] = barcodeDetectorPolyfill.BarcodeDetectorPolyfill };
console.log("1111");
// Define video as the video element. You can pass the entire element to the barcode detector!
const video = document.querySelector('video');
console.log("2222");
// Get a stream for the rear camera, else the front (or side?) camera, and show it in the video element.
video.srcObject = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } });
console.log("33333");
// Create a BarcodeDetector for simple retail operations.
const barcodeDetector = new BarcodeDetector({ formats: ["ean_13", "ean_8", "upc_a", "upc_e"] });
console.log("4444");
while(true) {
try {
// Try to detect barcodes in the current video frame.
let barcodes = await barcodeDetector.detect(video);
// Continue loop if no barcode was found.
if (barcodes.length == 0)
{
// Scan interval 50 ms like in other barcode scanner demos.
// The higher the interval the longer the battery lasts.
await new Promise(r => setTimeout(r, 50));
continue;
}
console.log("xxxx", barcodes);
// We expect a single barcode.
// It's possible to compare X/Y coordinates to get the center-most one.
// One can also do "preferred symbology" logic here.
// document.getElementById("barcode").innerText = barcodes[0].rawValue;
// Notify user that a barcode has been found.
// navigator.vibrate(200);
// Give the user time to find another product to scan
await new Promise(r => setTimeout(r, 1000));
}
catch(err) {
console.log("failed", err);
//Wait till video is ready
//barcodeDetector.detect(video) might fail the first time
await new Promise(r => setTimeout(r, 200));
}
}
"#,
);
});
rsx! {
script { src: "https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.9.15/dist/index.js" }
script { src: "https://cdn.jsdelivr.net/npm/@undecaf/barcode-detector-polyfill@0.9.21/dist/index.js" }
h1 { "Penguins Rule"}
video { autoplay: true, playsinline: true, }
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
Crazy idea maybe, but I would like a wasm front end written in dioxus to be able to prompt the user to scan a barcode.
Not sure where to start... Or if this is feasible.
I guess there are two distinct steps:
For step 2, It looks like there is on official detection API, https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API
But by the looks of it browser support might be limited. Not sure if Mobile firefox on Android supports it, might be limited to Chrome on Android.
Then somebody has written a component for yew: https://gitlab.com/voltfang-public/yew-scanner
Code here: https://gitlab.com/voltfang-public/yew-scanner/-/blob/main/src/lib.rs?ref_type=heads
It looks like this grabs an image from a
HtmlVideoElement
and passes to the bardecoder crate for decoding (not sure I entirely understand how theHtmlVideoElement
gets its image.https://github.com/piderman314/bardecoder
But it does certain things which I am not sure of in Dioxus, e.g. grabbing references to rendered html elements.
Then there is some JavaScript code:
https://stackoverflow.com/questions/67078359/barcode-scanning-via-mobile-browser
And I see this library looks like it might have Camera support in the future, but isn't quite there yet:
https://github.com/DioxusLabs/sdk
Any suggestions on which approach I should take?
Beta Was this translation helpful? Give feedback.
All reactions