Added logic of init camera in ocr

This commit is contained in:
dhanabalan
2025-10-27 11:10:15 +05:30
parent bd34dbfc4d
commit cefac4e8f3

View File

@@ -756,32 +756,49 @@ function cameraCapture() {
capturedPhoto: null, // store captured image capturedPhoto: null, // store captured image
serialNumbers: [], serialNumbers: [],
// async initCamera() {
// try {
// if (this.stream) this.stream.getTracks().forEach(track => track.stop());
// const video = this.$refs.video;
// this.stream = await navigator.mediaDevices.getUserMedia({
// video: { facingMode: this.currentFacingMode }
// });
// video.srcObject = this.stream;
// await new Promise(resolve => video.onloadedmetadata = resolve);
// video.play();
// // Overlay size matches video
// const overlay = this.$refs.overlay;
// overlay.width = video.videoWidth;
// overlay.height = video.videoHeight;
// this.startDetection();
// } catch (err) {
// console.error("Camera error:", err);
// alert("Camera error:\n" + (err.message || err));
// this.stopDetection();
// }
// },
async initCamera() { async initCamera() {
try { try {
if (this.stream) this.stream.getTracks().forEach(track => track.stop()); if (this.stream) this.stream.getTracks().forEach(track => track.stop());
const video = this.$refs.video;
this.stream = await navigator.mediaDevices.getUserMedia({ this.stream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: this.currentFacingMode } video: { facingMode: this.currentFacingMode }
}); });
video.srcObject = this.stream; this.$refs.video.srcObject = this.stream;
await new Promise(resolve => video.onloadedmetadata = resolve);
video.play();
// Overlay size matches video
const overlay = this.$refs.overlay;
overlay.width = video.videoWidth;
overlay.height = video.videoHeight;
this.startDetection(); this.startDetection();
} catch (err) { } catch (err) {
console.error("Camera error:", err); console.error("Camera error:", err);
alert("Camera error:\n" + (err.message || err)); alert("Cannot access camera. Enable permissions or use HTTPS.");
this.stopDetection();
} }
}, },
async switchCamera() { async switchCamera() {
this.currentFacingMode = this.currentFacingMode === 'user' ? 'environment' : 'user'; this.currentFacingMode = this.currentFacingMode === 'user' ? 'environment' : 'user';
await this.initCamera(); await this.initCamera();