HTML5调用手机摄像机、相册功能 方法

论坛 期权论坛 脚本     
匿名技术用户   2020-12-22 14:42   21   0

最近用MUI框架做webapp项目,在有PLUS环境的基础上能直接调用手机底层的API来使用拍照或从相册选择上传功能!

在查资料的时候,想起了另一种用input调用摄像和相册功能的方法,之前没有深入了解过,现在整理一下:

不需要特殊环境,使用input标签 type值为file,可以调用系统默认的照相机、相册、摄像机、录音功能。先上代码:

<input type="file" accept="image/*" capture="camera">

<input type="file" accept="video/*" capture="camcorder">

<input type="file" accept="audio/*" capture="microphone">

accept表示打开的系统文件目录

capture表示的是系统所捕获的默认设备,camera:照相机;camcorder:摄像机;microphone:录音;

其中还有一个属性multiple,支持多选,当支持多选时,multiple优先级高于capture,所以只用写成:<input type="file" accept="image/*" multiple>就可以,可以在手机上测试一下。那么选中的图片怎样获取并显示呢?

html:(css)

<form id="form1" runat="server">
<input type='file' id="imgInp" />
<div>
<img id="blah" src="#" alt="显示您上传的商品图片" />
</div>
</form>

js:

function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});

样式自己调整,这样就能显示刚拍下的照片或者从相册中选中的图片了。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP