phoneGap3.4 添加拍照插间

  1. cmd进入phoneGap创建的工程目录
  2. 进入plugins文件夹cd plugins  
  3. 开始下载插件cordova plugin add org.apache.cordova.camera
    等待安装结束

     调用相机的例子

 

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport"
	content="width=device-width, minimum-scale=1, maximum-scale=1">
<script type="text/javascript" src="js/cordova.js"></script>
<script type="text/javascript" charset="utf-8">
	var pictureSource; //设定图片来源  
	var destinationType; //选择返回数据的格式  

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

	// Cordova准备好了可以使用了  
	function onDeviceReady() {
		pictureSource = navigator.camera.PictureSourceType;
		destinationType = navigator.camera.DestinationType;
	}

	function onPhotoDataSuccess(imageData) {
		// base64 encoded image data  
		var smallImage = document.getElementById('smallImage');

		smallImage.style.display = 'block';
		//在使用base64编码的时候需要使用这样的前缀  
		smallImage.src = "data:image/jpeg;base64," + imageData;
	}

	// Called when a photo is successfully retrieved  
	//  
	function onPhotoURISuccess(imageURI) {
		// image file URI   
		var largeImage = document.getElementById('largeImage');
		largeImage.style.display = 'block';
		//使用image file URI 直接赋值就可以了  
		largeImage.src = imageURI;
	}

	// 第一个按钮调用函数  
	function capturePhoto() {
		navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
			quality : 50,
			destinationType : destinationType.DATA_URL
		});
	}

	//第二个按钮调用的函数  
	function capturePhotoEdit() {
		navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
			quality : 20,
			allowEdit : true,
			destinationType : destinationType.DATA_URL
		});
	}

	//第三/四个按钮调用的函数  
	function getPhoto(source) {
		// Retrieve image file location from specified source  
		navigator.camera.getPicture(onPhotoURISuccess, onFail, {
			quality : 50,
			destinationType : destinationType.FILE_URI,
			sourceType : source
		});
	}

	function onFail(message) {
		alert('Failed because: ' + message);
	}
</script>
</head>

<body>
	<button οnclick="capturePhoto();">Capture Photo</button>
	<br>
	<button οnclick="capturePhotoEdit();">Capture Editable Photo</button>
	<br>
	<button οnclick="getPhoto(pictureSource.PHOTOLIBRARY);">From
		Photo Library</button>
	<br>
	<button οnclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From
		Photo Album</button>
	<br>
	<img style="display:none;width:60px;height:60px;" id="smallImage"
		src="" />
	<img style="display:none;" id="largeImage" src="" />
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

love13135816

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值