densen2014 / ZXingBlazor

Using ZXing Scan barcode/QR code in blazor
Apache License 2.0
102 stars 41 forks source link

Selection of camera #14

Closed Solbstr closed 2 years ago

Solbstr commented 2 years ago

Hi,

Thanks for this great library.

Is it possible to auto select the camera with best resolution?

densen2014 commented 2 years ago

it is a open source projet,you can fork and try something you want and pr it . 😀

densen2014 commented 2 years ago

Hi,

Thanks for this great library.

Is it possible to auto select the camera with best resolution?

maybe default is the best resolution. because it use browser API

Solbstr commented 2 years ago

it is a open source projet,you can fork and try something you want and pr it . 😀

Not sure if that's something I'm capable of, but thanks for the quick reply. 👍

Solbstr commented 2 years ago

What about doing something like this and adding constraints? It would also be great if the back camera is selected by default.

//----------------------------------------------------------------------
//  Here we list all media devices, in order to choose between
//  the front and the back camera.
//      videoDevices[0] : Front Camera
//      videoDevices[1] : Back Camera
//  I used an array to save the devices ID 
//  which i get using devices.forEach()
//  Then set the video resolution.
//----------------------------------------------------------------------
navigator.mediaDevices.enumerateDevices()
.then(devices => {
    var videoDevices = [0,0];
    var videoDeviceIndex = 0;
    devices.forEach(function(device) {
    console.log(device.kind + ": " + device.label +
        " id = " + device.deviceId);
    if (device.kind == "videoinput") {  
        videoDevices[videoDeviceIndex++] =  device.deviceId;    
    }
    });

    var constraints =  {width: { min: 1024, ideal: 1280, max: 1920 },
    height: { min: 776, ideal: 720, max: 1080 },
    deviceId: { exact: videoDevices[1]  } 
};
return navigator.mediaDevices.getUserMedia({ video: constraints });

})
.then(stream => {
    if (window.webkitURL) {
    video.src = window.webkitURL.createObjectURL(stream);
    localMediaStream = stream;
    } else if (video.mozSrcObject !== undefined) {
    video.mozSrcObject = stream;
    } else if (video.srcObject !== undefined) {
    video.srcObject = stream;
    } else {
    video.src = stream;
    }})
.catch(e => console.error(e));

Ref : https://stackoverflow.com/questions/16807133/select-the-camera-while-using-navigate-getusermedia

EmilAlipiev commented 10 months ago

@densen2014 Is there a possibility to switch cameras if i have front and back camera?

yes. now is select the second camera. because first camera is font camera. but you can select camera what you want and automatically save your selected,for next time auto open and use it. try the demo website

densen2014 commented 10 months ago

i think i can not control the resolution,because the decode core is from zxing.js, i have another libs is for camera may be you can review and find some way to make it

https://blazor.app1.es/screencapture

image

EmilAlipiev commented 10 months ago

This looks good. Is it using same library or another? if it is another, which one?

densen2014 commented 10 months ago

it just a camera,not decode barcode, script edit by myself, and is open source , you can fork and build it