是否可以通过网站控制手机上的照相机指示灯? [英] Is it possible to control the camera light on a phone via a website?
问题描述
是否可以通过网站控制手机上的照相机指示灯?通过Chrome或Firefox说。我知道有可能使用Android或iOS应用程序,其中有很多手电筒应用程序。而且我知道可以通过getUserMedia系列功能控制相机。如果没有,有人知道什么时候可以使用吗?
以下是一个网站的torch-app
编辑:我也制作了 jsfiddle
//测试浏览器supportconst SUPPORTS_MEDIA_DEVICES ='mediaDevices'in navigator; if( (设备)=> device.kind ===' videoinput'); if(cameras.length === 0){throw'在此设备上找不到相机';} const camera = cameras [cameras.length - 1]; //创建流并获取视频轨道navigator.mediaDevices .getUserMedia({视频:{deviceId:camera.deviceId,facingMode:['user','environment'],height:{ideal:1080},width:{ideal:1920}}})。then(stream => {const track = stream.getVideoTracks()[0]; //创建图像捕捉对象并获取相机功能const imageCapture = new ImageCapture(track)const photoCapabilities = imageCapture.getPhotoCapabilities()。then(()=> {//待办事项:检查相机是否有火炬//让light!const btn = document.querySelector('。switch'); btn.addEventListener('click',function(){track.applyConstraints({advanced:[{torch:true}]});});}); }); }); //只要轨道存在,灯就会亮起来}
< button class =switch>开启/关闭< / button>
$ b
代码深受这个存储库的启发,这是 webseries 和博客文章
Is it possible to control the camera light on a phone via a website? Say through Chrome or Firefox. I know it's possible using an Android or iOS app, which the many flashlight apps out there. And I know one can control the cameras via the getUserMedia family of functions. If not, does anyone know when it will become available?
Here is a little "torch-app" for a website:
Edit: I also made a jsfiddle
//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: {
deviceId: camera.deviceId,
facingMode: ['user', 'environment'],
height: {ideal: 1080},
width: {ideal: 1920}
}
}).then(stream => {
const track = stream.getVideoTracks()[0];
//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(){
track.applyConstraints({
advanced: [{torch: true}]
});
});
});
});
});
//The light will be on as long the track exists
}
<button class="switch">On / Off</button>
The code is heavily inspired by this repository, this webseries and this blog-post
这篇关于是否可以通过网站控制手机上的照相机指示灯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!