skip to Main Content

Is there any way to detect whether the user has a front, rear, or both cameras in their device? For example, on a laptop, we normally have just one user camera, and it doesn’t have a rear camera. On a device, it may have both cameras, and I want a way to detect the user camera type

2

Answers


  1. You can detect whether a user’s device has a front, rear, or both types of cameras using the MediaDevices API.

    Example from mdn:

    if (!navigator.mediaDevices?.enumerateDevices) {
      console.log("enumerateDevices() not supported.");
    } else {
      // List cameras and microphones.
      navigator.mediaDevices
        .enumerateDevices()
        .then((devices) => {
          devices.forEach((device) => {
            console.log(`${device.kind}: ${device.label} id = ${device.deviceId}`);
          });
        })
        .catch((err) => {
          console.error(`${err.name}: ${err.message}`);
        });
    }
    
    Login or Signup to reply.
  2. navigator.mediaDevices.enumerateDevices()

    .then(devices => {
        const cameras = devices.filter(device => device.kind === 'videoinput');
    
        if (cameras.length === 0) {
            console.log('No cameras found');
    
        } else if (cameras.length === 1) {
            console.log('camera found');
    
            console.log('Camera label:', cameras[0].label);
    
        } else {
            console.log(' cameras found');
    
            console.log(' camera label:', cameras[0].label);
    
            console.log('Rear camera label:', cameras[1].label);
    
        }
    })
    .catch(err => console.error('Error accessing media devices:', err));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search