首页 文章

如何在webview中为Android启用前置摄像头

提问于
浏览
7

如何在Webview上启用前置摄像头?我在AndroidManifest.xml中启用了这些功能

<uses-feature android:name="android.hardware.camera" android:required="true" />
 <uses-feature android:name="android.hardware.camera.front" android:required="true" />

相机不会用于拍照或录制,只是打开前置摄像头 .

当我使用手机浏览器访问网站时,手机摄像头工作一次允许提示信息 . 如何使用webview?

在html文件中有一个显示网络摄像头的Canvas和Video标签它不会记录或拍摄它只是显示摄像机视图 .

Here is the html code

<canvas id="inCanvas"  width="500" height="500" style="display:none"></canvas>
 <video id="inputVideo" width="100" height="100" autoplay loop ></video>

它适用于网络摄像头,但不适用于android中的webview .

2 回答

  • 0

    我不太明白,但我可能会有以下两种中的一种,你想要什么 .

    1)访问摄像头,只在屏幕上显示视频(不捕获图像):

    html:

    <canvas id='canvas' width='100' height='100'></canvas>
    

    js:

    var onFailSoHard = function(e)
        {
                console.log('failed',e);
        }
    
        window.URL = window.URL || window.webkitURL ;
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
    
        var video = document.querySelector('video');
    
        if(navigator.getUserMedia)
        {
            navigator.getUserMedia({video: true},function(stream) {
            video.src = window.URL.createObjectURL(stream);
            },onFailSoHard);
        }
    
    
            var canvas = document.getElementById('canvas'); 
        var ctx = canvas.getContext('2d');
    setInterval(function(){ 
    ctx.drawImage(video,0,0);
         }, 100);
    
            }
    

    2)从相机捕获图像:

    heredoc .

    navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
        destinationType: Camera.DestinationType.DATA_URL
    });
    
    function onSuccess(imageData) {
        var image = document.getElementById('myImage');
        image.src = "data:image/jpeg;base64," + imageData;
    }
    
    function onFail(message) {
        alert('Failed because: ' + message);
    }
    
  • 4

    我会使用类似下面的东西作为访问手机摄像头的脚本 .

    <script>
      var errorCallback = function(e) {
        console.log('Rejected!', e);
      };
    
      // Not showing vendor prefixes.
      navigator.getUserMedia({video: true, audio: true}, function(localMediaStream) {
        var video = document.querySelector('video');
        video.src = window.URL.createObjectURL(localMediaStream);
    
        // Note: onloadedmetadata doesn't fire in Chrome when using it with getUserMedia.
        // See crbug.com/110938.
        video.onloadedmetadata = function(e) {
          // Ready to go. Do some stuff.
        };
      }, errorCallback);
    </script>
    

    the following教程来帮助我 . 希望它在正确的轨道上设置yuo :)

相关问题