MUI---上传头像功能实现
这里使用MUI上传头像的功能是结合VUE来做的,所以:
changeFace:function(){
var IMAGE_UNSPECIFIED = "image/*"; //相册显示的文件类型
var PHOTOZOOM = 2; // 获取完图片返回key
var PHOTOLAT = 1; // 剪裁完毕后返回key
var main = plus.android.runtimeMainActivity(); //h5+获取应用主Activity实例对象
var Intent = plus.android.importClass("android.content.Intent");
//导入java类intent对象 以下importClass都是使用安卓原生类
var MediaStore = plus.android.importClass("android.provider.MediaStore");
var File = plus.android.importClass("java.io.File");
var Uri = plus.android.importClass("android.net.Uri");
var intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
var outPutPath = plus.io.convertLocalFileSystemURL("_www/head.jpg");
main.startActivityForResult(intent, PHOTOZOOM);
// uploadImgPath
main.onActivityResult = (requestCode,resultCode,data)=>{
// main.onActivityResult = function(requestCode, resultCode, data) {
if (PHOTOZOOM == requestCode) {
var file = new File(outPutPath);
// 输出目录uri
var outPutUri = Uri.fromFile(file);
if (data == undefined) {
return false;
}
plus.android.importClass(data);
var uri = data.getData();
var cropIntent = new Intent("com.android.camera.action.CROP"); //裁剪
cropIntent.setDataAndType(uri, IMAGE_UNSPECIFIED);
// 截图完毕后 输出目录
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);
cropIntent.putExtra("crop", "true");
//aspectX aspectY 是宽高的比例
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
cropIntent.putExtra("outputX", 70);
cropIntent.putExtra("outputY", 70);
cropIntent.putExtra("return-data", true);
main.startActivityForResult(cropIntent, PHOTOLAT);
} else if (requestCode == PHOTOLAT) {
if (data == undefined) {
return false;
}
var a = new File(outPutPath);
if (!a.exists()) {
console.log(outPutPath);
mui.toast('裁剪错误');
return false;
}
this.uploadImgPath = outPutPath;
console.log(outPutPath);
alert(outPutPath);
// img.src = outPutPath;
};
};
// user/profile/update/
var server = "";
server = config.userProfileUpdate;
var files = [];
var upload_img = (p)=>{
files = [];
var n = p.substr(p.lastIndexOf('/')+1);
files.push({name:"uploadkey"},{path:p});
// 开始上传
start_upload();
};
var start_upload = ()=>{
if(files.length <= 0){
plus.nativeUI.alert("没有添加上传文件");
return;
};
// 原生的转圈等待框
var wt = plus.nativeUI.showWaiting();
var task = plus.uploader.createUpload(server,{methods:"POST"},(t,status){
// 上传完成
alert(status);
if(status == 200){
// 资源
var responseText = t.responseText;
// 转化为json
var json = eval('('+responseText+')');
// 上传文件的信息
var files = json.files;
// 上传成功以后的保存路径
var img_url = files.uploadkey.url;
alert(img_url);
// ajax 写入数据库
// 关闭转圈等待框
wt.close();
}else{
console.log("上传失败:"+status);
};
});
task.addData("client","");
task.addData("uid",getUid()); // 每次都不同
for(var i=0;i<files.length;i++){
var f = files[i];
task.addFile(f.path,{key:f.name});
};
task.start();// 执行到这里才会开始上传
};
// 产生一个随机数
var getUid = (){
return Math.floor(Math.random()*100000000+100000000).toString();
};
// main.onActivityResult = function(requestCode, resultCode, data) {
// if (PHOTOZOOM == requestCode) {
// var file = new File(outPutPath);
// // 输出目录uri
// var outPutUri = Uri.fromFile(file);
// if (data == undefined) {
// return false;
// }
// plus.android.importClass(data);
// var uri = data.getData();
// var cropIntent = new Intent("com.android.camera.action.CROP"); //裁剪
// cropIntent.setDataAndType(uri, IMAGE_UNSPECIFIED);
// // 截图完毕后 输出目录
// cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);
// cropIntent.putExtra("crop", "true");
// //aspectX aspectY 是宽高的比例
// cropIntent.putExtra("aspectX", 1);
// cropIntent.putExtra("aspectY", 1);
// // outputX outputY 是裁剪图片宽高
// cropIntent.putExtra("outputX", 70);
// cropIntent.putExtra("outputY", 70);
// cropIntent.putExtra("return-data", true);
// main.startActivityForResult(cropIntent, PHOTOLAT);
// } else if (requestCode == PHOTOLAT) {
// if (data == undefined) {
// return false;
// }
// var a = new File(outPutPath);
// if (!a.exists()) {
// console.log(outPutPath);
// mui.toast('裁剪错误');
// return false;
// }
// console.log(outPutPath);
// alert(outPutPath);
// // img.src = outPutPath;
// }
// }
// this.showSelectFace = !this.showSelectFace;
// this.selectFace = true;
// this.mask = mui.createMask((res)=>{
// this.selectFace = false;
// });
// this.mask.show();
// if (mui.os.plus) {
// var a = [{
// title: "拍照"
// },{
// title: "从手机相册选择"
// }];
// plus.nativeUI.actionSheet({
// title: "修改用户头像",
// cancel: "取消",
// buttons: a
// }, function(b) { /*actionSheet 按钮点击事件*/
// switch (b.index) {
// case 0:
// break;
// case 1:
// getImage(); /*拍照*/
// break;
// case 2:
// galleryImg();/*打开相册*/
// break;
// default:
// break;
// }
// })
// };
// 拍照
function getImage() {
var c = plus.camera.getCamera();
c.captureImage(function(e) {
plus.io.resolveLocalFileSystemURL(e, function(entry) {
var s = entry.toLocalURL() + "?version=" + new Date().getTime();
uploadHead(s); /*上传图片*/
}, function(e) {
console.log("读取拍照文件错误:" + e.message);
});
}, function(s) {
console.log("error" + s);
}, {
filename: "_doc/head.png"
})
};
// 本地相册
function galleryImg() {
// 从相册中选择图片
console.log("从相册中选择图片");
plus.gallery.pick(function(path){
console.log(path);
},function ( e ) {
console.log( "取消选择图片" );
}, {filter:"image"} );
// 从相册中选择图片
// plus.gallery.pick(function(a) {
// plus.io.resolveLocalFileSystemURL(a, function(entry) {
// plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
// root.getFile("head.png", {}, function(file) {
// //文件已存在
// file.remove(function() {
// console.log("file remove success");
// entry.copyTo(root, 'head.png', function(e) {
// var e = e.fullPath + "?version=" + new Date().getTime();
// uploadHead(e); /*上传图片*/
// //变更大图预览的src
// //目前仅有一张图片,暂时如此处理,后续需要通过标准组件实现
// },
// function(e) {
// console.log('copy image fail:' + e.message);
// });
// }, function() {
// console.log("delete image fail:" + e.message);
// });
// }, function() {
// //文件不存在
// entry.copyTo(root, 'head.png', function(e) {
// var path = e.fullPath + "?version=" + new Date().getTime();
// uploadHead(path); /*上传图片*/
// },
// function(e) {
// console.log('copy image fail:' + e.message);
// });
// });
// }, function(e) {
// console.log("get _www folder fail");
// })
// }, function(e) {
// console.log("读取拍照文件错误:" + e.message);
// });
// }, function(a) {}, {
// filter: "image"
// })
};
//上传头像图片
function uploadHead(imgPath) {
console.log("imgPath = " + imgPath);
mainImage.src = imgPath;
mainImage.style.width = "60px";
mainImage.style.height = "60px";
var image = new Image();
image.src = imgPath;
image.onload = function() {
var imgData = getBase64Image(image);
pc = new PhotoClip("#clipArea",{
size: [300, 300],//裁剪框大小
outputSize:[0,0],//打开图片大小,[0,0]表示原图大小
ok:"#clipBtn",
img: imgPath,
// 图片开始加载的回调函数。this 指向当前 PhotoClip 的实例对象,并将正在加载的 file 对象作为参数传入。(如果是使用非 file 的方式加载图片,则该参数为图片的 url)
loadStart:function(){},
// 图片加载完成的回调函数。this 指向当前 PhotoClip 的实例对象,并将图片的 <img> 对象作为参数传入。
loadComplete:function(){},
// 裁剪完成的回调函数。this 指向当前 PhotoClip 的实例对象,会将裁剪出的图像数据DataURL作为参数传入。
done:function(dataURL){
// 在这里调用上传接口
// mui.ajax("图片上传接口",{
// data:{},
// dataType:'json',
// type:'post',
// timeout:10000,
// success:function(data){console.log('上传成功');},
// error:function(xhr, type, errorThrown){mui.toast("网络异常,请稍后重试!");}
// });
}
});
}
};
//将图片压缩转成base64
function getBase64Image(img) {
var canvas = document.createElement("canvas");
var width = img.width;
var height = img.height;
// calculate the width and height, constraining the proportions
if (width > height) {
if (width > 100) {
height = Math.round(height *= 100 / width);
width = 100;
}
} else {
if (height > 100) {
width = Math.round(width *= 100 / height);
height = 100;
}
}
canvas.width = width; /*设置新的图片的宽度*/
canvas.height = height; /*设置新的图片的长度*/
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height); /*绘图*/
var dataURL = canvas.toDataURL("image/png", 0.8);
return dataURL.replace("data:image/png;base64,", "");
};
},
MUI---上传头像功能实现的更多相关文章
- 完美实现类似QQ的自拍头像、上传头像功能!(Demo 源码)
现在很多下载客户端程序都需要设定自己头像的功能,而设定头像一般有两种方式:使用摄像头自拍头像,或者选择一个图片的某部分区域作为自己的头像. 一.相关技术 若要实现上述的自拍头像和上传头像的功能,会碰到 ...
- php实现手机拍照上传头像功能
现在手机拍照很火,那么如何使用手机拍照并上传头像呢?原因很简单,就是数据传递,首先手机传递照片信息,这个就不是post传递 也不是get函数传递, 这个另外一种数据格式传递,使用的是$GLOBALS ...
- php实现视频拍照上传头像功能实例代码
如果要在php中实现视频拍照我们需要借助于flash插件了,由flash拍出的确照片我们再通过php的$GLOBALS ['HTTP_RAW_POST_DATA']接受数据,然后保存成图片就可以了,下 ...
- (GoRails) 使用ActiveStorage给user添加上传头像功能。
对activestorage的简单使用: 头像库:uifaces.co. 可以使用大量设置好的头像图片. 1.安装avatar rails active_storage:install 2.user ...
- 【javascript】html5中使用canvas编写头像上传截取功能
[javascript]html5中使用canvas编写头像上传截取功能 本人对canvas很是喜欢,于是想仿照新浪微博头像上传功能(前端使用canvas) 本程序目前在谷歌浏览器和火狐浏览器测试可用 ...
- asp.net mvc上传头像加剪裁功能介绍
正好项目用到上传+剪裁功能,发上来便于以后使用. 我不能告诉你们其实是从博客园扒的前台代码,哈哈. 前端是jquery+fineuploader+jquery.Jcrop 后台是asp.net mvc ...
- asp.net mvc上传头像加剪裁功能
原文:asp.net mvc上传头像加剪裁功能 正好项目用到上传+剪裁功能,发上来便于以后使用. 我不能告诉你们其实是从博客园扒的前台代码,哈哈. 前端是jquery+fineuploader+jqu ...
- mvc上传头像加剪裁功能
asp.net mvc上传头像加剪裁功能 正好项目用到上传+剪裁功能,发上来便于以后使用. 我不能告诉你们其实是从博客园扒的前台代码,哈哈. 前端是jquery+fineuploader+jquery ...
- web实现QQ头像上传截取功能
由于最近一段时间比较忙,发现好久没写博客了,给大家分享下最近搞的logo上传截取功能.在实现这个功能之前找了一些jq的插件,最后选定了cropper在github中可以找到. 具体的思路是1:选择上传 ...
随机推荐
- 6 Django系列之关于models的sql语句日常用法总结
preface Django提供了强大的ORM,我们可以通过ORM快速的写出我们想要对数据做什么样操作的代码.下面就说说我在日常工作中的用法: 外键关联精确查询 应用场景:表A host字段关联到了表 ...
- linux环境中通过useradd命令,创建用户的时候指定用户的base-dir
需求说明: 今天一个同事,问了一个这样的问题,在linux环境中,创建用户的时候,默认的是在/home目录下创建一个与用户名相同的家目录, 如何能够将这个/home更换成一个其他的,比如/opt/ap ...
- linux 中搜索命令的对比
1.find find是最常用和最强大的查找命令.它能做到实时查找,精确查找,但速度慢. find的使用格式如下: #find [指定目录] [指定条件] [指定动作] 指定目录:是指所要搜索的目录和 ...
- selenium+phantomjs渲染网页
from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapa ...
- 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值
7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...
- swift--CATransform3D的简单介绍
今天来了解下CATransform3D的一些基本的知识.CATransform3D是一个用于处理3D形变的类,其可以改变控件的平移.缩放.旋转.斜交等,其坐标系统采用的是三维坐标系,即向右为x轴正方向 ...
- 【AI】图像识别-物体检测-百度AI-EasyDL-NodeJS
var https = require('https') var express = require('express'); var app = express(); var bodyParser = ...
- linux 停止对某个端口的监听
1.通过"netstat -anp" 来查看哪些端口被打开. 2.关掉对应的应用程序,则端口就自然关闭了,如:"kill -9 PID" (PID:进程号)
- python卸载或者安装时提示There is a problem with this Windows Installer package.A program required for this install to complete could not be run. Contact your support personnel or package vendor
1.卸载时报这个错,先进行下修复,再执行卸载: 2.安装时报这个错,安装的过程中,没有取得管理员的权限. Msi格式的文件,点右键后,也没有“以管理员身份运行”的菜单项,那怎么办呢?你可以点“开始”菜 ...
- php-fpm 配置进程池
什么是 php-fpm :php 是作为一个独立服务存在的,这个服务叫做 php-fpm什么是 php-fpm pool :也就是 php-fpm 的进程池,这个进程池中运行了多个子进程,用来并发处理 ...