fix camera upload for mobile (#686)

Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
Chirag Chhatrala 2025-02-19 15:53:26 +05:30 committed by GitHub
parent 55b0f57741
commit 83ef18f453
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 56 additions and 17 deletions

View File

@ -1,12 +1,18 @@
<template> <template>
<div class="relative border"> <div class="relative border">
<video <video
id="webcam" id="webcam"
autoplay autoplay
playsinline playsinline
:class="[{ hidden: !isCapturing }, theme.fileInput.minHeight, theme.fileInput.borderRadius]" muted
width="1280" :class="[
height="720" { hidden: !isCapturing },
theme.fileInput.minHeight,
theme.fileInput.borderRadius,
'w-full h-full object-cover'
]"
webkit-playsinline
/> />
<canvas <canvas
id="canvas" id="canvas"
@ -146,25 +152,58 @@ export default {
}, },
methods: { methods: {
openCameraUpload() { async openCameraUpload() {
this.isCapturing = true this.isCapturing = true
this.capturedImage = null this.capturedImage = null
this.webcam
.start() try {
.then(() => { // Get video element
this.cameraPermissionStatus = "allowed" const webcamElement = document.getElementById("webcam")
if (this.isBarcodeMode) { const canvasElement = document.getElementById("canvas")
this.initQuagga()
// iOS specific constraints
const constraints = {
audio: false,
video: {
facingMode: this.isBarcodeMode ? 'environment' : 'user',
width: { ideal: 1280 },
height: { ideal: 720 }
}
}
// Try getting the stream directly first
const stream = await navigator.mediaDevices.getUserMedia(constraints)
// Attach stream to video element
webcamElement.srcObject = stream
// Create webcam instance with the stream
this.webcam = new Webcam(
webcamElement,
this.isBarcodeMode ? 'environment' : 'user',
canvasElement
)
// Wait for video to be ready
await new Promise((resolve) => {
webcamElement.onloadedmetadata = () => {
webcamElement.play()
resolve()
} }
}) })
.catch((err) => {
console.error(err) this.cameraPermissionStatus = "allowed"
if (err.toString() === "NotAllowedError: Permission denied") { if (this.isBarcodeMode) {
this.cameraPermissionStatus = "blocked" this.initQuagga()
return }
} } catch (err) {
console.error('Camera error:', err)
if (err.name === 'NotAllowedError' || err.toString().includes('Permission denied')) {
this.cameraPermissionStatus = "blocked"
} else {
this.cameraPermissionStatus = "unknown" this.cameraPermissionStatus = "unknown"
}) }
}
}, },
initQuagga() { initQuagga() {
if (!this.quaggaInitialized) { if (!this.quaggaInitialized) {