Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to choose devices camera (from multiple back cameras, i want the main) for barcodescanning with QuaggaJS? #479

Open
vinitdev1995 opened this issue Nov 24, 2021 · 1 comment

Comments

@vinitdev1995
Copy link

How to choose devices camera (from multiple back cameras, i want the main) for barcode scanning with QuaggaJS in react js?

@rcasher
Copy link

rcasher commented Dec 20, 2021

This is what I use for detecting multiple back cameras. And choosing the last back camera gives you the correct camera for iPhones with multiple cameras.

navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
devices.forEach(function(device) {
// alert('device - ' + JSON.stringify(device));
if ( device.kind === 'videoinput' && device.label.match(/back/) != null ) {
// alert('Back found! - ' + device.label);
backCameraList.push({'deviceLabel': device.label, 'deviceId': device.deviceId});
}
});

    // alert('backCameraList: ' + JSON.stringify(backCameraList));
    // alert('arrayLength: ' + backCameraList.length);
    // alert('finalBackCamera: ' + JSON.stringify(backCameraList[backCameraList.length - 1], null, 2));
    // alert('finalBackCameraId' + backCameraList[backCameraList.length - 1]['deviceId']);

    if (backCameraList.length > 0 && backCameraList[backCameraList.length - 1]['deviceId'] !== undefined) {
      Quagga.init({
        locator: {
          patchSize: 'medium',
          halfSample: false,
        },
        numOfWorkers: 1,
        decoder: {
          // readers: ['ean_reader', 'code_128_reader', 'ean_8_reader', 'code_39_reader', 'code_39_vin_reader',
          //   'codabar_reader', 'upc_reader', 'upc_e_reader', 'i2of5_reader'],
          readers: oneDReaders,
        },
        locate: true,
        inputStream: {
          type: 'LiveStream',
          constraints: {
            width: 640,
            height:  480,
            deviceId: backCameraList[backCameraList.length - 1]['deviceId']
          },
          frequency: 10,
          singleChannel: true
        }
      }, (err: any) => {
        if (err) {
          return console.error(err);
        }
        Quagga.start();
      });
    } else {
      Quagga.init({
        locator: {
          patchSize: 'medium',
          halfSample: false,
        },
        numOfWorkers: 1,
        decoder: {
          // readers: ['ean_reader', 'code_128_reader', 'ean_8_reader', 'code_39_reader', 'code_39_vin_reader',
          //   'codabar_reader', 'upc_reader', 'upc_e_reader', 'i2of5_reader'],
          readers: oneDReaders,
        },
        locate: true,
        inputStream: {
          type: 'LiveStream',
          constraints: {
            width: 640,
            height:  480,
            facingMode: 'environment'
          },
          frequency: 10,
          singleChannel: true
        }
      }, (err: any) => {
        // if (err) {
        //   return console.error(err);
        // }
        Quagga.start();
      });
    }

  })
  .catch(function(err) {
    console.log(err.name + ': ' + err.message);
  });

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants