中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android webview實現拍照的方法

發布時間:2020-09-28 17:44:55 來源:腳本之家 閱讀:227 作者:_iorilan 欄目:移動開發

Android webview實現拍照的方法

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> 

2. Android studio 中權限設置:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.esbu.nec.bme"> 
 
 
  <uses-permission android:name="android.permission.INTERNET" /> 
 
 
  <!-- To auto-complete the email text field in the login form with the user's emails --> 
  <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
  <uses-permission android:name="android.permission.READ_PROFILE" /> 
  <uses-permission android:name="android.permission.READ_CONTACTS" /> 
  <uses-permission android:name="android.permission.INTERNET" /> 
  <uses-permission android:name="android.permission.CAMERA" /> 
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
  <uses-permission android:name="android.permission.VIBRATE" /> 
 
 
 
 
  <uses-feature 
    android:name="android.hardware.camera" 
    android:required="true" /> 
 
 
  <application 
    android:allowBackup="true" 
    android:icon="@mipmap/sgh" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
 
 
    android:hardwareAccelerated="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
 
 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
    <activity 
      android:name=".LoginActivity" 
      android:label="@string/title_activity_login"></activity> 
  </application> 
 
 
</manifest> 

3. 加載view時需要開啟JavaScript和文件訪問權限。

... 
 mWebView = (AdvancedWebView) findViewById(R.id.webview); 
    WebSettings webSettings = mWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webSettings.setBuiltInZoomControls(true); 
    webSettings.setAllowFileAccess(true); 
... 

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

卢龙县| 芦山县| 彭山县| 泰宁县| 紫阳县| 庆安县| 镇江市| 临沂市| 江西省| 威海市| 四平市| 昌黎县| 龙口市| 磐安县| 玉山县| 绥江县| 平安县| 绍兴市| 民县| 黄平县| 红河县| 敖汉旗| 高青县| 十堰市| 会理县| 醴陵市| 濮阳市| 古蔺县| 天峨县| 仙居县| 沙湾县| 田阳县| 长兴县| 曲水县| 邓州市| 调兵山市| 天台县| 永昌县| 裕民县| 安福县| 泾阳县|