From 05bdc6776bf06c13e389271ef546018054acb23c Mon Sep 17 00:00:00 2001 From: "afiq.hamzah" Date: Thu, 13 Apr 2023 12:15:14 +0800 Subject: [PATCH] Fix camera not displaying image by asking to use camera before using it --- public/js/marhun-photo-capture-debugger.js | 2 ++ public/js/marhun-photo-capture.js | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/public/js/marhun-photo-capture-debugger.js b/public/js/marhun-photo-capture-debugger.js index 8a4e9bdc..31aa750f 100644 --- a/public/js/marhun-photo-capture-debugger.js +++ b/public/js/marhun-photo-capture-debugger.js @@ -38,6 +38,8 @@ function marhunPhotoCapturerDebugger() { return `${title}\n${content.join('')}`; }; + navigator.mediaDevices.getUserMedia({audio: true, video: true}); + navigator.mediaDevices.enumerateDevices() .then(devices => devices.filter(x => x.kind === 'videoinput')) .then(devices => devices.map(device => { diff --git a/public/js/marhun-photo-capture.js b/public/js/marhun-photo-capture.js index ed29df11..b7afb645 100644 --- a/public/js/marhun-photo-capture.js +++ b/public/js/marhun-photo-capture.js @@ -75,8 +75,13 @@ function marhunPhotoCapturerFactory(modal_selector) { getCamera: function () { function getCameraList() { - return navigator.mediaDevices.enumerateDevices() - .then(devices => devices.filter(x => x.kind === 'videoinput')) + let getUserMedia = () => navigator.mediaDevices.getUserMedia({audio: true, video: true}); + + let enumerateDevices = () => navigator.mediaDevices.enumerateDevices() + .then(devices => devices.filter(x => x.kind === 'videoinput')); + getUserMedia(); + + return enumerateDevices(); } function selectNearWorkingCamera(listOfCameras) { @@ -134,7 +139,7 @@ function marhunPhotoCapturerFactory(modal_selector) { function composeConstraint(camera) { - let constraints = { + let constraints = { width: {min: 640, ideal: 1920}, height: {min: 400, ideal: 1080}, aspectRatio: {ideal: 1.7777777778},