Open camera via a button
This commit is contained in:
@@ -22,32 +22,34 @@ export default {
|
||||
}
|
||||
},
|
||||
};
|
||||
this.el.addEventListener("camera_request", () => {
|
||||
navigator.mediaDevices
|
||||
.getUserMedia(constraints)
|
||||
.then((mediaStream) => {
|
||||
console.debug("Camera access allowed")
|
||||
this.pushEventTo(this.cameraPreview, "camera_allowed", {});
|
||||
this.cameraPreview.srcObject = mediaStream;
|
||||
this.cameraPreview.onloadedmetadata = () => {
|
||||
this.cameraPreview.play();
|
||||
this.scanInterval = window.setInterval(async () => {
|
||||
const barcodes = await barcodeDetector.detect(this.cameraPreview);
|
||||
if (barcodes.length <= 0) return;
|
||||
|
||||
navigator.mediaDevices
|
||||
.getUserMedia(constraints)
|
||||
.then((mediaStream) => {
|
||||
console.debug("Camera access allowed")
|
||||
this.pushEventTo(this.cameraPreview, "camera_allowed", {});
|
||||
this.cameraPreview.srcObject = mediaStream;
|
||||
this.cameraPreview.onloadedmetadata = () => {
|
||||
this.cameraPreview.play();
|
||||
this.scanInterval = window.setInterval(async () => {
|
||||
const barcodes = await barcodeDetector.detect(this.cameraPreview);
|
||||
if (barcodes.length <= 0) return;
|
||||
barcodes.forEach(barcode => {
|
||||
if (!detectedBarcodes.has(barcode.rawValue)) {
|
||||
this.pushEventTo(this.cameraPreview, "barcode_scanned", { number: barcode.rawValue });
|
||||
detectedBarcodes.add(barcode.rawValue);
|
||||
};
|
||||
});
|
||||
}, 500)
|
||||
};
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`${err.name}: ${err.message}`);
|
||||
this.pushEventTo(this.cameraPreview, "camera_denied", {});
|
||||
});
|
||||
})
|
||||
|
||||
barcodes.forEach(barcode => {
|
||||
if (!detectedBarcodes.has(barcode.rawValue)) {
|
||||
this.pushEventTo(this.cameraPreview, "barcode_scanned", { number: barcode.rawValue });
|
||||
detectedBarcodes.add(barcode.rawValue);
|
||||
};
|
||||
});
|
||||
}, 500)
|
||||
};
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`${err.name}: ${err.message}`);
|
||||
this.pushEventTo(this.cameraPreview, "camera_denied", {});
|
||||
});
|
||||
},
|
||||
destroyed() {
|
||||
this.cameraPreview.srcObject.getTracks().forEach(track => track.stop());
|
||||
|
||||
Reference in New Issue
Block a user