Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
How to select the highest resolution among multiple rear camera in getUserMedia()

I am using an Android phone. When I try to display the rear camera image in a webpage, the image is always "enlarged". I expect the camera image is the same as what I see in native camera, however, it is not.

Recently, I just discovered that when I listed out the camera source, there are multiple rear camera sources. So I think the "enlarged image" above is simply cause I used the first default listed rear camera.

I wonder how to choose the suitable rear camera?

A same case that found in the internet enter image description here

Sorry for the poor presentation. If you don't understand my question, I can further explain.

'use strict';

var videoElement = document.querySelector('video');
var audioSelect = document.querySelector('select#audioSource');
var videoSelect = document.querySelector('select#videoSource');

audioSelect.onchange = getStream;
videoSelect.onchange = getStream;

getStream().then(getDevices).then(gotDevices);

function getDevices() {
  return navigator.mediaDevices.enumerateDevices();
}

function gotDevices(deviceInfos) {
  window.deviceInfos = deviceInfos; // make available to console
  console.log('Available input and output devices:', deviceInfos);
  for (const deviceInfo of deviceInfos) {
    const option = document.createElement('option');
    option.value = deviceInfo.deviceId;
    if (deviceInfo.kind === 'audioinput') {
      option.text = deviceInfo.label || `Microphone ${audioSelect.length + 1}`;
      audioSelect.appendChild(option);
    } else if (deviceInfo.kind === 'videoinput') {
      option.text = deviceInfo.label || `Camera ${videoSelect.length + 1}`;
      videoSelect.appendChild(option);
    }
  }
}

function getStream() {
  if (window.stream) {
    window.stream.getTracks().forEach(track => {
      track.stop();
    });
  }
  const audioSource = audioSelect.value;
  const videoSource = videoSelect.value;
  const constraints = {
    audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
    video: {deviceId: videoSource ? {exact: videoSource} : undefined}
  };
  return navigator.mediaDevices.getUserMedia(constraints).
    then(gotStream).catch(handleError);
}

function gotStream(stream) {
  window.stream = stream; // make stream available to console
  audioSelect.selectedIndex = [...audioSelect.options].
    findIndex(option => option.text === stream.getAudioTracks()[0].label);
  videoSelect.selectedIndex = [...videoSelect.options].
    findIndex(option => option.text === stream.getVideoTracks()[0].label);
  videoElement.srcObject = stream;
}

function handleError(error) {
  console.error('Error: ', error);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I am not sure if it is correct. But at least at this moment, it works with my Android phones that had problems before this fix. Seems the one with 0 is always the suitable one.

var constraints = {
   facingMode: 'environment'
};
    
function gotDevices(mediaDevices) {   
 mediaDevices.forEach(mediaDevice => {
  if (mediaDevice.kind === 'videoinput') {
   if( !isIOS && mediaDevice.label.includes("0") ){
    constraints = {
     deviceId : mediaDevice.deviceId
    };
    return;
   }
  }
 });
}
             
navigator.mediaDevices.enumerateDevices().then(gotDevices).then(function(){
     navigator.mediaDevices
     .getUserMedia({
      audio: false,
      video: constraints
     })
     .then(function(stream){
      window.stream = stream;
      video.srcObject = stream;
     })
     .catch(handleError);
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda