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){
- 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) {
- 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>
|