html5-qrcode icon indicating copy to clipboard operation
html5-qrcode copied to clipboard

Default to Back Camera (if available) when using the Html5QrcodeScanner instead of Html5Qrcode

Open imSurya-georgian opened this issue 4 years ago • 9 comments

There is no option to default to the back camera option when using the Html5QrcodeScanner class. If there is an option. Please let me know how to use it.

imSurya-georgian avatar Nov 16 '20 02:11 imSurya-georgian

I've modified html5-qrcode-scanner.js to achieve this goal. The idea is:

  1. Use an array to keep the camera ids on calling Html5Qrcode.getCameras()
  2. Use an integer to keep the index of current camera id, initialize it to the last one (If there is only one camera, set index to 0; if there are two ,set index to 1)
  3. Pass the selected camera id (Not the index) on calling html5Qrcode.start()
  4. Add a button to switch camera, change the index and recall html5Qrcode.stop() & html5Qrcode.start() when clicked

georgewei avatar Nov 21 '20 01:11 georgewei

Per APIs there is no reliable way to determine which camera Id is back and which is front. In the past versions the support for facingMode was added to Html5Qrcode (not Html5QrcodeScanner class). Here's a demo for that - link to demo

While I do see benefits to adding this to Html5QrcodeScanner directly, the problem is this is not scalable across mobile and desktops as the facing mode aren't supported first class on desktop/laptop environment. I can expose this as a configuration to developers to choose (i.e. cameraId model or facing mode model in Html5QrcodeScanner) but I'd need some feedbacks for the same on how you'd prefer it.

@imSurya-georgian @georgewei - based on the scenarios / use-cases you are using this library how'd you like this issue be solved cross platform?

Another direction would be to try to detect if the browser is from a mobile or non mobile browser and show the selection accordingly.

Ideas are welcome, happy to implement this.

mebjas avatar Nov 21 '20 06:11 mebjas

Detecting the brower type is not reliable for some mobile browers can run in 'desktop' mode. I prefer to expose the camera info (id, name, etc.) and let the developer to choose which is the default one.

georgewei avatar Nov 21 '20 13:11 georgewei

@mebjas Sorry for the late response. I am using Html5QrcodeScanner render function to give UI to the 'div' element. This is already implemented. Now that I want the Rendered UI to use the back camera by default whenever it is available and if not use the only available camera (Front Camera).

imSurya-georgian avatar Nov 29 '20 19:11 imSurya-georgian

Hello did we find a solution to default load which camera to use on load?

smiley486 avatar May 27 '21 14:05 smiley486

listen the selectbox with video devices and use localstorage to save it.

ROBERT-MCDOWELL avatar May 27 '21 14:05 ROBERT-MCDOWELL

Thanks

smiley486 avatar May 27 '21 15:05 smiley486

For my use case, I would love to ignore the front facing camera if the back facing one exist. I would prefer not to give the user the choice and directly start scanning using the back one.

With the current version the user has to clock on dropdown, change and click to start scanning which is very poor UX IMO.

yelhouti avatar Jun 12 '22 06:06 yelhouti

@yelhouti your case is a special one that you must develop yourself, nothing to do with the UX

ROBERT-MCDOWELL avatar Jun 12 '22 10:06 ROBERT-MCDOWELL