0
Guys... I’m trying to take a photo with a webcam... I’m relatively new in Javascript and I’m taking a choir!
Follows my code:
//função para iniciar a camera
function startCamera(){
navigator.mediaDevices.getUserMedia({
video:{facingMode:'environment'},
audio:true
})
.then((stream) => {
document.getElementById('video').srcObject=stream
})
}
//função para parar a camera
function stopCamera(){
document.getElementById('video')
.srcObject
.getVideoTracks()
.forEach(track => track.stop())
}
//ligar a camer automaticamente
window.addEventListener("DOMContentLoaded", startCamera())
//função para tirar foto
document.querySelector('#btnStart').addEventListener('click', event => {
const canvas = document.getElementById('pic')
const context = canvas.getContext('2d')
const video = document.getElementById('video')
//tamanho da foto mesmo tamanho do video
canvas.width = video.offsetWidth
canvas.height = video.offsetHeight
//desenha o video no canvas
context.drawImage(video, 0, 0, canvas.witdh, canvas.height)
//metodo do canvas para pegar um objeto Blob
canvas.toBlob(function(blob){
const url = URL.createObjectURL(blob)
stopCamera()
}, 'image/jpeg', 0.95)
//closeCamera()
})
<video src="" id="video" muted autoplay></video>
<canvas id="pic"></canvas>
<input type="button" id="btnStart" value="Tirar Foto">
So far I could only turn on the camera and show 'LIVE'... I could not make the photo appear on the canvas... it is all black when I click on the button to take the photo.
I reversed the issue dear @reculos, because if it does not misread the answer, I am checking the problem and I already add a possible resolution ;)
– Guilherme Nascimento