html跳转到相册

html-css010

html跳转到相册,第1张

HTML5页面如何在手机端浏览器调用相机、相册功能

开发微信端浏览器访问的HTML5的页面,页面中有一个<input id="input" type="file"/>标签,iOS直接就支持吊起相机拍照或是相册选择,

但android中不支持吊起相机拍照,只能吊起相册选择,网上的帖子说是因为android屏蔽了文件上传功能还是怎么的,没看明白。

此篇博文记录如何解决这一问题,使得android也可以直接吊起相机拍照。

在查资料的之后,总结了一下用input调用相机和相册功能的方法,之前没有深入了解过,现在整理一下:

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

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>HTML5页面如何在手机端浏览器调用相机、相册功能</title>

</head>

<body>

<div>

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

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

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

</div>

</body>

</html>

 

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

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

 如果不加上capture,则只会显示相应的,例如上述三种依次是:拍照或图库,录像或图库,录像或拍照或图库,加上capture之后不会调用图库。

 其中还有一个属性multiple,支持多选,当支持多选时,multiple优先级高于capture,

所以只用写成:<input type="file" accept="image/*" multiple>就可以,可以在手机上测试一下。

 

需要加载cordova.js

方法:

document.addEventListener("deviceready", onDeviceReady, false)

function onDeviceReady() {

pictureSource = navigator.camera.PictureSourceType

destinationType = navigator.camera.DestinationType

}

//相册

function fromCamera()

{

var source = pictureSource.PHOTOLIBRARY

navigator.camera.getPicture(function (imageData) {

setimg(imageData)

}, function (message) {

if (source == pictureSource.CAMERA)

alert('加载照相机出错!' + message)

else

alert('加载相册出错!' + message)

}, {

quality: 50,

destinationType: destinationType.FILE_URI,

sourceType: source

})

}

//拍照

function EditImgPz()

{

navigator.camera.getPicture(function (imageData) {

setimg(imageData)

}, function (message) {

alert(message)

}, {

quality: 50,

destinationType: navigator.camera.DestinationType.FILE_URI,

sourceType: Camera.PictureSourceType.CAMERA,

allowEdit: true,

encodingType: Camera.EncodingType.JPEG,

popoverOptions: CameraPopoverOptions,

saveToPhotoAlbum: true

})

}

html相册就是把一组图片放到一个固定位置轮番滚动查看,一般是类似于qq相册的功能。

代码如下:

<script language="javascript">

//定义img图库

var img = ["","images/1.gif","images/2.gif","images/3.gif","images/4.gif"]//最前面留一个空白位置

//轮番显示

function show(op){

if(Number(op)){

// 取得对应的图片

document.getElementById("img1").src=img[op]

}

}

</script>

//设置图片显示样式

<style type="text/css">

li{

float:left

height:15px

background-color:#FF66FF

border:1px #00FFFF dotted

line-height:15px

}

a{

color:#00FFFF

text-decoration:none

}

</style>

以下是html代码,设置一个img控件,长和宽都是50个像素

<img src="images/1.gif" id="img1" width="50" height="50" />

<ul style="list-style-type:nonewidth:200px">

<li><a href="#" onclick="show(1)">1</a></li><!--点击显示第一张图-->

<li><a href="#" onclick="show(2)">2</a></li><!--点击显示第二张图-->

<li><a href="#" onclick="show(3)">3</a></li><!--点击显示第三张图-->

<li><a href="#" onclick="show(4)">4</a></li><!--点击显示第四张图-->

</ul>