try fixing scanner

This commit is contained in:
Darius
2025-11-21 01:48:04 +01:00
parent fcc0b2bb0a
commit af77d5e774
2 changed files with 27 additions and 27 deletions

View File

@@ -4,7 +4,6 @@ import { defineConfig } from "astro/config";
// https://astro.build/config // https://astro.build/config
export default defineConfig({ export default defineConfig({
output: "server", output: "server",
base: "/foodscanner",
adapter: node({ adapter: node({
mode: "standalone", mode: "standalone",
}), }),

View File

@@ -2,7 +2,7 @@
// Astro component - no props needed // Astro component - no props needed
--- ---
<style> <style is:global>
/* Reset and Base Styles */ /* Reset and Base Styles */
* { * {
margin: 0; margin: 0;
@@ -11,6 +11,8 @@
} }
body { body {
margin: 0;
padding: 0;
font-family: font-family:
system-ui, system-ui,
-apple-system, -apple-system,
@@ -610,7 +612,7 @@ body {
</div> </div>
<p class="scanner-info"> <p class="scanner-info">
📸 Point the camera at the barcode or QR code Point the camera at the barcode or QR code
</p> </p>
<form id="scanner-barcode-form" class="form"> <form id="scanner-barcode-form" class="form">
@@ -620,7 +622,7 @@ body {
placeholder="Or enter barcode manually" placeholder="Or enter barcode manually"
class="input" class="input"
/> />
<button type="submit" class="btn btn-green"> <button id="startButton" type="submit" class="btn btn-green">
Search Search
</button> </button>
</form> </form>
@@ -905,23 +907,21 @@ import { analyzeProduct } from "../utils/productAnalysis";
barcodeDisplay.textContent = ""; barcodeDisplay.textContent = "";
showView(scannerView); showView(scannerView);
let selectedDeviceId : string;
codeReader = new window.ZXing.BrowserMultiFormatReader(); codeReader = new window.ZXing.BrowserMultiFormatReader();
const videoInputDevices = codeReader.listVideoInputDevices().then((videoInputDevices: unknown) => {
await window.ZXing.BrowserCodeReader.listVideoInputDevices(); selectedDeviceId = videoInputDevices[0].deviceId
if (videoInputDevices.length >= 1) {
if (videoInputDevices.length === 0) { videoInputDevices.forEach((element) => {
showError("No camera found"); const sourceOption = document.createElement('option')
scanning = false; sourceOption.text = element.label
return; sourceOption.value = element.deviceId
})
} }
const selectedDeviceId = videoInputDevices[0].deviceId; document.getElementById('startButton').addEventListener('click', () => {
codeReader.decodeFromVideoDevice(selectedDeviceId, 'video', (result, err) => {
await codeReader.decodeFromVideoDevice(
selectedDeviceId,
videoElement,
(result: any, err: any) => {
if (result) { if (result) {
const code = result.getText(); const code = result.getText();
barcodeDisplay.textContent = code; barcodeDisplay.textContent = code;
@@ -929,8 +929,9 @@ import { analyzeProduct } from "../utils/productAnalysis";
codeReader.reset(); codeReader.reset();
searchProduct(code); searchProduct(code);
} }
}, })
); })
})
} catch (err) { } catch (err) {
console.error("Scanner error:", err); console.error("Scanner error:", err);
showError( showError(