我在nodeJS中有一个项目,我在系统上使用以及在移动。
我需要执行以下步骤- 1.添加打开/关闭手电筒的按钮。2.只有当手机和浏览器支持该功能时,才应显示该按钮。3.指示灯应默认为关闭
当我使用下面提到的代码时,它正在从我的系统启用我的WebCam闪存,并且它不能在我的手机上工作。
闪光灯开/关
//Test browser support
const SUPPORTS_MEDIA_DEVICES = 'mediaDevices' in navigator;
if (SUPPORTS_MEDIA_DEVICES) {
//Get the environment camera (usually the second one)
navigator.mediaDevices.enumerateDevices().then(devices => {
const cameras = devices.filter((device) => device.kind === 'videoinput');
if (cameras.length === 0) {
throw 'No camera found on this device.';
}
const camera = cameras[cameras.length - 1];
// Create stream and get video track
navigator.mediaDevices.getUserMedia({
video: true
}).then(stream => {
const track = stream.getVideoTracks()[0];
track.applyConstraints({
advanced: [{torch: false}]
});
//Create image capture object and get camera capabilities
const imageCapture = new ImageCapture(track)
const photoCapabilities = imageCapture.getPhotoCapabilities().then(() => {
//todo: check if camera has a torch
//let there be light!
const btn = document.querySelector('.switch');
btn.addEventListener('click', function(){
isTorchOn = !isTorchOn;
track.applyConstraints({
advanced: [{torch: isTorchOn}]
});
});
});
});
});
//The light will be on as long the track exists
}
有人能给出一个解决方案吗?
发布于 2020-06-24 23:57:18
对于Web RTC,在设备上使用此选项进行Torch支持检查
var imageCapture = new ImageCapture(videoTrack);
var photoCapability = imageCapture.getPhotoCapabilities();
在浏览器中使用此选项进行Torch支持检查
var mediaCapabilities = navigator.mediaDevices.getSupportedConstraints ()
if (mediaCapabilities.torch && photoCapability.fillLightMode.length > 0){
document.getElementById('torchButton').classList.remove("hidden");
document.getElementById('torchButton').classList.add("block");
console.log("Torch is enabled");
}
然后基于函数中的调用
function startTorch(){
var torchCheckBox = document.getElementById("torchButton");
if(torchCheckBox.checked == true){
videoTrack.applyConstraints({
advanced: [{torch: true}]
}).then(function() {
//Do Success code here
}).catch(handleError);
}
else{
videoTrack.applyConstraints({
advanced: [{torch: false}]
}).then(function() {
//success code here
}).catch(handleError);
}
}
希望这能有所帮助!
https://stackoverflow.com/questions/61814177
复制相似问题