1. 程式人生 > >mui 拍照和相簿 功能

mui 拍照和相簿 功能


<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link href="../../../css/mui.min.css" rel="stylesheet" />
		<script src="../../../js/mui.min.js"></script>
		<script src="../../../js/jquery.js"></script>
		<style>
			.imageup{ position: absolute; margin:auto;left:0px;top:0;right:0;bottom:0; width:100px; height: 36px; line-height:36px; color: #000; border-radius: 5px; border:1px #ddd solid; text-align:center;font-size:20px;;}
		</style>
	</head>
	<body>
		<a href="javascript:void(0);" class="imageup">上傳圖片</a>
		<script>
			function plusReady(){
				// 彈出系統選擇按鈕框
				mui("body").on("tap",".imageup",function(){
					page.imgUp();
				})
				
			}
			 
			var page=null;
			page={
				imgUp:function(){
					var m=this;
					plus.nativeUI.actionSheet({cancel:"取消",buttons:[
						{title:"拍照"},
						{title:"從相簿中選擇"}
					]}, function(e){//1 是拍照  2 從相簿中選擇
						switch(e.index){
							case 1:clickCamera();break;
							case 2:clickGallery();break;
						}
					});
				}
				//攝像頭
			}
			
			
	 //傳送照片
	
	function clickGallery() {
		plus.gallery.pick(function(path) {
			plus.zip.compressImage({
				src: path,
				dst: "_doc/chat/gallery/" + path,
				quality: 20,
				overwrite: true
			}, function(e) {
				var task = plus.uploader.createUpload(server + "upload/chat", {
					method: "post"
				}, function(t, sta) {
					console.log(JSON.stringify(t))
					if(sta == 200) {
						var msg = t.responseText;
						var oImg = JSON.parse(msg);
						var imgUrl = oImg.urls;
						var re = new RegExp("\\\\", "g");
						imgUrl = imgUrl.replace(re, "/");
						uploadMsg(2, imgUrl);
					}
				});
				task.addFile(e.target, {});
				task.start();
			}, function(err) {
				console.error("壓縮失敗:" + err.message);
			});

		}, function(err) {});
	};
	
	
	// 拍照
	
	function clickCamera() {
		var cmr = plus.camera.getCamera();
		var res = cmr.supportedImageResolutions[0];
		var fmt = cmr.supportedImageFormats[0];
		cmr.captureImage(function(path) {
			//plus.io.resolveLocalFileSystemURL(path, function(entry) {
			plus.io.resolveLocalFileSystemURL(path, function(entry) {
				var localUrl = entry.toLocalURL();
				plus.zip.compressImage({
					src: localUrl,
					dst: "_doc/chat/camera/" + localUrl,
					quality: 20,
					overwrite: true
				}, function(e) {
					var task = plus.uploader.createUpload(server + "upload/chat", {
						method: "post"
					}, function(t, sta) {
						if(sta == 200) {
							var msg = t.responseText;
							var oImg = JSON.parse(msg);
							var imgUrl = oImg.urls;
							var re = new RegExp("\\\\", "g");
							imgUrl = imgUrl.replace(re, "/");
							console.log(imgUrl);
							uploadMsg(2, imgUrl);
						}
					});
					task.addFile(e.target, {});
					task.start();
				}, function(err) {
					console.log("壓縮失敗:  " + err.message);
				});
			});
		}, function(err) {
			console.error("拍照失敗:" + err.message);
		}, {
			index: 1
		});
	};


			
			
			if(window.plus){
				plusReady();
			}else{
				document.addEventListener("plusready",plusReady,false);
			}
			
		</script>
	</body> 
</html>