VUE 移动端只获取当前拍摄照片,不允许相册获取 及 input标签capture属性详解

一, 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时都只能单文件

测试连接:http://h5apk.cn/input-capture.php

猜你喜欢

转载自www.cnblogs.com/tangwei89/p/12421274.html
今日推荐