是否可以通过网站控制手机上的相机灯?通过 Chrome 或 Firefox 说。我知道可以使用 Android 或 iOS 应用程序,那里有许多手电筒应用程序。而且我知道可以通过 getUserMedia 函数系列控制相机。如果没有,有谁知道它什么时候可用?
是否可以通过网站控制手机上的相机灯?
IT技术
javascript
google-chrome
mobile
getusermedia
2021-03-04 04:35:48
3个回答
这是一个网站的小“火炬应用程序”:
编辑 1:我也做了一个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>
编辑 2: 这仅适用于 Chrome(可能还有 Opera)。它在 iOS 上的Chrome 中不起作用,因为 Chrome 无法访问相机。我现在无法在 android 上测试它。我创建了一个带有输出的新 jsfiddle。如果您有一部 android 手机并且它不适合您,它可能会说明原因:https : //jsfiddle.net/jpa1vwed/
随意调试,评论和编辑。
您可以使用MediaStream图像捕捉API通过创建ImageCapture中从VideoStreamTrack和设置选项“fillLightMode”到“闪”或“火炬”。例子:
<video autoplay="true"></video>
<img />
<button onclick="takePhoto()">Take Photo</button>
<script type="text/javascript">
var imageCapture = null;
var deviceConfig = {
video: {
width: 480,
height: 640,
facingMode: "environment", /* may not work, see https://bugs.chromium.org/p/chromium/issues/detail?id=290161 */
deviceId: null
}
};
var imageCaptureConfig = {
fillLightMode: "torch", /* or "flash" */
focusMode: "continuous"
};
// get the available video input devices and choose the one that represents the backside camera
navigator.mediaDevices.enumerateDevices()
/* replacement for not working "facingMode: 'environment'": use filter to get the backside camera with the flash light */
.then(mediaDeviceInfos => mediaDeviceInfos.filter(mediaDeviceInfo => ((mediaDeviceInfo.kind === 'videoinput')/* && mediaDeviceInfo.label.includes("back")*/)))
.then(mediaDeviceInfos => {
console.log("mediaDeviceInfos[0].label: " + mediaDeviceInfos[0].label);
// get the device ID of the backside camera and use it for media stream initialization
deviceConfig.video.deviceId = mediaDeviceInfos[0].deviceId;
navigator.mediaDevices.getUserMedia(deviceConfig)
.then(_gotMedia)
.catch(err => console.error('getUserMedia() failed: ', err));
});
function takePhoto () {
imageCapture.takePhoto()
.then(blob => {
console.log('Photo taken: ' + blob.type + ', ' + blob.size + 'B');
// get URL for blob data and use as source for the image element
const image = document.querySelector('img');
image.src = URL.createObjectURL(blob);
})
.catch(err => console.error('takePhoto() failed: ', err));
}
function _gotMedia (mediastream) {
// use the media stream as source for the video element
const video = document.querySelector('video');
video.srcObject = mediastream;
// create an ImageCapture from the first video track
const track = mediastream.getVideoTracks()[0];
imageCapture = new ImageCapture(track);
// set the image capture options (e.g. flash light, autofocus, ...)
imageCapture.setOptions(imageCaptureConfig)
.catch(err => console.error('setOptions(' + JSON.stringify(imageCaptureConfig) + ') failed: ', err));
}
</script>
笔记:
- 在撰写本文时,API 仍在开发中,将来可能会发生变化。
- 要在 Chrome 中启用ImageCapture,必须将标志“chrome://flags/#enable-experimental-web-platform-features”设置为“true”
- 为使ImageCapture中的Firefox中的标志“dom.imagecapture.enabled”在“about:config中”已被设置为“真”。但是在撰写本文时不支持“setOptions”!
也可以看看:
- GitHub 上的媒体流图像捕获
- NPM moduleImageCapture polyfill