Added logic of init camera in ocr
This commit is contained in:
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user