首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NodeJS -在移动安卓/IPhone中打开和关闭触摸屏/手电筒

NodeJS -在移动安卓/IPhone中打开和关闭触摸屏/手电筒
EN

Stack Overflow用户
提问于 2020-05-15 15:41:06
回答 1查看 1K关注 0票数 0

我在nodeJS中有一个项目,我在系统上使用以及在移动。

我需要执行以下步骤- 1.添加打开/关闭手电筒的按钮。2.只有当手机和浏览器支持该功能时,才应显示该按钮。3.指示灯应默认为关闭

当我使用下面提到的代码时,它正在从我的系统启用我的WebCam闪存,并且它不能在我的手机上工作。

闪光灯开/关

代码语言:javascript
运行
复制
//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
}

有人能给出一个解决方案吗?

EN

回答 1

Stack Overflow用户

发布于 2020-06-24 23:57:18

对于Web RTC,在设备上使用此选项进行Torch支持检查

代码语言:javascript
运行
复制
var imageCapture = new ImageCapture(videoTrack);

var photoCapability = imageCapture.getPhotoCapabilities();

在浏览器中使用此选项进行Torch支持检查

代码语言:javascript
运行
复制
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");
      }

然后基于函数中的调用

代码语言:javascript
运行
复制
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);
    }
}

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61814177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档