一, VUE移動端簡單實現只獲取當前拍攝照片demo
<template> <div> <img :src="urls" width="200px" height="200px"/> <input style="display: none;" type="file" id='uploadFile' accept="image/*" capture="user" v-on:change="readLocalFile()"> <button @click="imgClick()">拍照</button> </div> </template>
<script> export default { data() { return { urls:'', } }, methods:{ //圖片click imgClick(){ document.getElementById("uploadFile").click(); }, //點擊選中圖片 readLocalFile: function () { var that_ = this; var localFile = document.getElementById("uploadFile").files[0]; var reader = new FileReader(); var content; var current=this; reader.onload = function(event) { content = event.target.result; that_.urls = content; //base64圖片地址 } reader.onerror = function(event) { alert('error') } content = reader.readAsDataURL(localFile,"UTF-8"); var fileUrls=document.getElementById("uploadFile").value; console.log(fileUrls); //圖片本機路徑 } } } </script>
二,H5 input標簽capture屬性詳解
HTML5官方文檔解釋:capture屬性用於調用設備的攝像頭或麥克風。
當accept=”audio/*或video/*”時capture只有兩種值,一種是user,用於調用面向人臉的攝像頭(例如手機前置攝像頭),一種是environment,用於調用環境攝像頭(例如手機后置攝像頭)。
當accept=”audio”時,只要有capture就調用設備麥克風,忽略user和environment值。
至於網上提到的camera和filesystem,官方沒提。
官方文檔:www.w3.org/TR/2018/REC-html-media-capture-20180201/
iOS最遵守遵守HTML5規范,其次是X5內核,安卓的webview基本忽略了capture。
理想情況下應該按照如下開發webview:
1. 當accept=”image/*”時,capture=”user”調用前置照相機,capture=”其他值”,調用后置照相機
2. 當accept=”video/*”時,capture=”user”調用前置錄像機,capture=”其他值”,調用后置錄像機
3. 當accept=”image/*,video/*”,capture=”user”調用前置攝像頭,capture=”其他值”,調用后置攝像頭,默認照相,可切換錄像
4. 當accept=”audio/*”時,capture=”放空或者任意值”,調用錄音機
5. 當input沒有capture時,根據accppt類型給出文件夾選項以及攝像頭或者錄音機選項
6. input含有multiple時訪問文件夾可勾選多文件,調用系統攝像頭或者錄音機都只是單文件
7. 無multiple時都只能單文件