2016-06-16 9 views
5

क्या किसी वेबसाइट पर फोन पर कैमरा लाइट को नियंत्रित करना संभव है? क्रोम या फ़ायरफ़ॉक्स के माध्यम से कहें। मुझे पता है कि एंड्रॉइड या आईओएस ऐप का उपयोग करना संभव है, जो वहां कई फ्लैशलाइट ऐप्स हैं। और मुझे पता है कि कोई getUserMedia परिवार के कार्यों के माध्यम से कैमरों को नियंत्रित कर सकता है। यदि नहीं, तो क्या किसी को पता चलेगा कि यह कब उपलब्ध होगा?क्या किसी वेबसाइट पर कैमरे के प्रकाश को किसी वेबसाइट पर नियंत्रित करना संभव है?

+0

क्या तुमने कभी बाहर आप यह कर सकते हैं कि क्या आंकड़ा था, और यदि हां तो कैसे? –

+0

नहीं: -/नहीं है – TinyTheBrontosaurus

उत्तर

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> 

नोट:

  • इस एपीआई लिखने के रूप में विकास के तहत अब भी है और भविष्य में बदल सकता है।
  • झंडा क्रोम में ImageCapture को सक्षम करने के लिए "chrome: // झंडे/# सक्षम-प्रयोगात्मक-वेब-मंच-सुविधाओं""सही"
  • पर सेट होने के ImageCapture को सक्षम करने के लिए है फ़ायरफ़ॉक्स में ध्वज "dom.imagecapture.enabled"में "about: config" को "सत्य" पर सेट करना होगा। लेकिन "सेटऑप्शन" इस लेखन के रूप में समर्थित नहीं है!

यह भी देखें:

संबंधित मुद्दे