我在 nodeJS 中有一个项目,我在系统和移动设备上使用它。
我需要执行以下步骤 -
1.添加手电筒开关按钮。
2. 仅当该功能支持时,才会显示该按钮
手机和浏览器
3. 灯应默认关闭
当我使用下面提到的代码时,它从我的系统启用我的网络摄像头闪光灯,但它在我的手机上不起作用。
闪光灯开/关
//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
}
有人能提出解决方案吗?
对于网络 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);
}
}
希望这会有所帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)