是否可以通過網站控制手機上的照相機指示燈?通過Chrome或Firefox說。我知道有可能使用Android或iOS應用程序,其中有很多手電筒應用程序。而且我知道可以通過getUserMedia系列功能控制相機。如果沒有,有人知道它何時可用嗎?是否可以通過網站控制手機上的照相機指示燈?
5
A
回答
6
這裏是一個小 「火炬的應用程序」 的網站:
編輯:我也做了一個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>
的代碼在很大程度上受到這個啓發repository,這webseries這blog-post
3
您可以通過從VideoStreamTrack創建ImageCapture中和設置選項「fillLightMode」到「閃」或「火炬」使用MediaStream Image Capture API。例如:
<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仍處於開發階段,並在將來可能會改變。
- 爲使ImageCapture中 Chrome的標誌「鉻://標誌/#使實驗性的Web平臺的特性」必須被設置爲「真」
- 爲使ImageCapture中在Firefox標誌「dom.imagecapture.enabled」 在 「about:config中」必須被設置爲「真」。 但是在撰寫本文時,不支持「setOptions」!
參見:
- Mediastream Image Capture在GitHub上
- NPM模塊ImageCapture polyfill
相關問題
- 1. 通過藍牙控制手機相機
- 2. 是否可以通過智能手機瀏覽器製作livecam?
- 3. 手機網站手機網站
- 4. 調出相機功能手機網站
- 5. 是否可以通過編程方式將iPhone的相機閃光燈變暗?
- 6. 是否可以控制手機的振動器?
- 7. 是否有可能通過用戶權限訪問手機網站中的手機號碼?
- 8. 是否可以更改相機膠捲中照片的相冊?
- 9. 手機上的Android主機網站
- 10. 網站在手機上顯示空白
- 11. Interrobang在手機網站上不顯示
- 12. iPad相機照片到網站 - 可以這樣做嗎?
- 13. 是否可以控制WP7芒果的相機對焦?
- 14. 是否可以通過受控計算機內的虛擬機遠程控制計算機?
- 15. 我們可以使用手機網站觸發手機通話事件嗎?
- 16. 我們可以用HTML5上傳手機相機中的照片嗎
- 17. 是否可以通過手機瀏覽器觸發Android服務?
- 18. 如何檢查電話是否可以通過手機發送?
- 19. 通過wholeBodyMotion控制機器人手臂
- 20. 通過藍牙控制Android手機
- 21. 通過Python控制機器人手臂
- 22. 是否可以通過網絡應用訪問手機的聯繫人?
- 23. 是否可以在Android手機上創建手機應答機程序?
- 24. 開發可通過手機應用程序訪問的網站
- 25. 保存其通過本地計算機上的照相機
- 26. 準確的指南針手機網站
- 27. 檢查以太網上的Windows計算機是否通過C++
- 28. 是否可以通過帶耳機的耳機播放音調?
- 29. 是否可以觸發智能手機上的共享菜單(通過HTML/JS)?
- 30. 是否可以在本地主機上安裝PHP Fox製作的網站?
你有沒有弄清楚你是否能夠做到這一點,如果是的話怎麼辦? –
nope: - /沒有 – TinyTheBrontosaurus