Androidwebview实现拍照的方法-创新互联

Android webview实现拍照的方法

从策划到设计制作,每一步都追求做到细腻,制作可持续发展的企业网站。为客户提供成都网站设计、做网站、网站策划、网页设计、域名注册网站空间、网络营销、VI设计、 网站改版、漏洞修补等服务。为客户提供更好的一站式互联网解决方案,以客户的口碑塑造优易品牌,携手广大客户,共同发展进步。

1. html 


<div id="pnlVideo1"> 
              <input type="hidden" name="imgNric1" id="imgNric1" /> 
              <label id="nric" class="control-label labelfont" >Picture of Asset</label><br /><br /> 
              <button id="btnOpen1" class="btn btn-default" type="button">Open WebCam</button> 
              <select id="videoSource" > 
                 
              </select> 
              <div id="vdoOne" > 
                <video id="video"  width="300" autoplay></video> 
                <canvas id="canvasPreview"  width="300" height="224"></canvas> 
                <canvas id="canvasUpload"  width='300' height='224'></canvas> 
                <button id="snap" class="btn btn-default" type="button">Snap Photo</button> 
              </div> 
            </div> 
             
             
 
 
 
 
 
 
  <script type="text/javascript"> 
    $(document).ready(function () { 
 
 
 
 
    }); 
 
 
    //// Elements for taking the snapshot 
    var canvasPreview = document.getElementById('canvasPreview'); 
    var canvasUpload = document.getElementById('canvasUpload'); 
    var contextPreview = canvasPreview.getContext('2d'); 
    var contextUpload = canvasUpload.getContext('2d'); 
 
 
 
 
 
 
    //#################### Video Source ####################### 
    var videoElement = document.querySelector('video'); 
    var videoSelect = document.querySelector('select#videoSource'); 
 
 
    navigator.mediaDevices.enumerateDevices() 
      .then(gotDevices).then(getStream).catch(handleError); 
 
 
    videoSelect.onchange = getStream; 
 
 
    function gotDevices(deviceInfos) { 
      for (var i = 0; i !== deviceInfos.length; ++i) { 
        var deviceInfo = deviceInfos[i]; 
        var option = document.createElement('option'); 
        option.value = deviceInfo.deviceId; 
        if (deviceInfo.kind === 'videoinput') { 
          option.text = deviceInfo.label || 'camera ' + 
            (videoSelect.length + 1); 
          videoSelect.appendChild(option); 
        } else { 
          console.log('Found ome other kind of source/device: ', deviceInfo); 
        } 
      } 
    } 
 
 
    function getStream() { 
      if (window.stream) { 
        window.stream.getTracks().forEach(function (track) { 
          track.stop(); 
        }); 
      } 
 
 
      var constraints = { 
         
        video: { 
          optional: [{ 
            sourceId: videoSelect.value 
          }] 
        } 
      }; 
 
 
      navigator.mediaDevices.getUserMedia(constraints). 
        then(gotStream).catch(handleError); 
    } 
 
 
    function gotStream(stream) { 
      window.stream = stream; // make stream available to console 
      videoElement.srcObject = stream; 
    } 
 
 
    function handleError(error) { 
      console.log('Error: ', error); 
    } 
 
 
    //######################## End Video Source ################# 
 
 
 
 
    // Get access to the camera! 
    if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { 
      navigator.mediaDevices.getUserMedia({ video: true }).then(function (stream) { 
        videoElement.src = window.URL.createObjectURL(stream); 
        videoElement.play(); 
 
 
      }); 
    } 
    else { 
      document.getElementById("pnlVideo1").style.display = "none"; 
    } 
 
 
 
 
    
 
 
 
 
    //// Trigger photo take 
    document.getElementById("snap").addEventListener("click", function () { 
      contextPreview.drawImage(videoElement, 0, 0, 300, 224); 
      contextUpload.drawImage(videoElement, 0, 0, 300, 224); 
      document.getElementById("video").style.display = "none"; 
      document.getElementById("snap").style.display = "none"; 
      document.getElementById("canvasPreview").style.display = "block"; 
 
 
      var image = document.getElementById("canvasUpload").toDataURL("image/jpeg"); 
      image = image.replace('data:image/jpeg;base64,', ''); 
      $("#imgNric1").val(image); 
    }); 
 
 
    //// Trigger photo take 
 
 
 
 
    document.getElementById("btnOpen1").addEventListener("click", function () { 
      document.getElementById("vdoOne").style.display = "block"; 
      document.getElementById("video").style.display = "block"; 
      document.getElementById("snap").style.display = "block"; 
      document.getElementById("canvasPreview").style.display = "none"; 
    }); 
 
 
 
 
</script> 

网站标题:Androidwebview实现拍照的方法-创新互联
文章链接:https://www.cdcxhl.com/article36/ihpsg.html

成都网站建设公司_创新互联,为您提供网站内链网站建设网站维护外贸建站响应式网站企业建站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作