< html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title > Demostración de captura de cámara title > < style > #camera {
width: 100%;
max-width: 500px;
}
#photo {
display: none;
} style > head > < body > < h1 > Acceder a la cámara y capturar foto h1 > < video id = "camera" autoplay > video > < button id = "capture-btn" > Capturar foto button > < canvas id = "photo" > canvas > < script> const video = document . getElementById ( 'cámara' );
const canvas = document . getElementById ( 'foto' );
const captureBtn = document . getElementById ( 'capture-btn' );
const context = canvas. getContext ( '2d' ); navigator. mediaDevices . getUserMedia ({ video : true })
. then ( ( stream ) => {
video. srcObject = stream;
})
. catch ( ( error ) => {
console . error ( "Error al acceder a la cámara: " , error);
}); captureBtn. addEventListener ( 'clic' , () => {
canvas. width = video. videoWidth ;
canvas. height = video. videoHeight ;
context. drawImage (video, 0 , 0 , canvas. width , canvas. height );
}); script > body > html >