MUI调用手机相机拍照+压缩+上传

<div id="image" style="height: 130px;background: #fff;padding-top: 10px;display: flex;justify-content: space-around;">
	<div class="pie" onclick="captureImage(1)" id="img1">
		<img src="img/photograph@3x.png" />
		<p>拍照</p>
	</div>
	<div class="pie" onclick="captureImage(2)" id="img2">
		<img src="img/photograph@3x.png" />
		<p>拍照</p>
	</div>
	<div class="pie" onclick="captureImage(3)" id="img3">
		<img src="img/photograph@3x.png" />
		<p>拍照</p>
		<!--<img src="img/connection_icon@3x.png"/><i>x</i>-->
	</div>
</div>

 

//调用手机拍照功能
	// 扩展API加载完毕后调用onPlusReady回调函数
document.addEventListener( "plusready", onPlusReady, false );

	// 扩展API加载完毕,现在可以正常调用扩展API 
function onPlusReady() {
	console.log("plusready");
}

var TC = 0;
	// 拍照
function captureImage(t){
	TC = t;
	var cmr = plus.camera.getCamera();
	var res = cmr.supportedImageResolutions[0];
	var fmt = cmr.supportedImageFormats[0];
//	console.log("Resolution: "+res+", Format: "+fmt);
	cmr.captureImage( function( path ){
			var files = [];
			files.push({name:"uploadkey",path:path});
			compressImage(files);
			
		},
		function( error ) {
//			alert( "Capture image failed: " + error.message );
		},
		{resolution:res,format:fmt}
	);
}
// 压缩图片
function compressImage(f){
	plus.nativeUI.showWaiting();
	plus.zip.compressImage({
		src:f[0].path,
		dst:"_doc/cm.jpg",
		quality:20,
		overwrite:true,
		width:'50%',
		clip:{
			top:"25%",
			left:"25%",
			width:"50%",
			height:"50%"
		}
	},
	function(i){
		plus.nativeUI.closeWaiting();
		upload(i.target,f[0].name);
	},function(e){
		plus.nativeUI.closeWaiting();
	});
}
var server=sysurl + "home/visit/upfile";


// 上传文件

function upload(files,name){
	
	if(files.length<=0){
		plus.nativeUI.alert("没有添加上传文件!");
		return;
	}
	console.log("开始上传:")
	var wt=plus.nativeUI.showWaiting();
	var task=plus.uploader.createUpload(server,
		{method:"POST"},
		function(t,status){ //上传完成
			if(status==200){
				var txt = JSON.parse(t.responseText);
				if(TC == 1){ //图片显示
					var im = '<img src="'+txt.url+'"  /><i>x</i>';
					$('#img1').addClass('active');
					$('#img1').html(im);
				}else if(TC == 2){
					var im = '<img src="'+txt.url+'"  /><i>x</i>';
					$('#img2').addClass('active');
					$('#img2').html(im);
				}else if(TC == 3){
					var im = '<img src="'+txt.url+'"  /><i>x</i>';
					$('#img3').addClass('active');
					$('#img3').html(im);
				}else{
					var im = '<img src="'+txt.url+'"  /><i>x</i>';
					$('#img1').addClass('active');
					$('#img1').html(im);
				}
				wt.close();
			}else{
				console.log("上传失败:"+status);
				wt.close();
			}
		}
	);
	task.addFile(files,{key:name});
	task.start();
}
// 拍照添加文件
function appendByCamera() {
	plus.camera.getCamera().captureImage(function(p) {
		appendFile(p);
	});
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值