bxdsam
bxdsam
  • 发布:2017-08-02 11:44
  • 更新:2019-01-25 16:46
  • 阅读:3908

完整demo 上传头像

分类:HTML5+

因为没有用canvas所以并不会有图片旋转的问题。

在实际工作中遇到了上传头像的需求,刚开始做的时候发现社区里面都是零散的代码,都是在说原理的没有一个完整的功能,然后就自己写了这个功能和大家分享一下,下载下来放hbuilder里面运行就能看到效果了,有需要的可以拿去用。

示例代码

打开该链接有效果图,可以看一下是不是想要的。

0 关注 分享

要回复文章请先登录注册

l***@qq.com

l***@qq.com

为什么选择图片后自动旋转了?
2019-01-25 16:46
2***@qq.com

2***@qq.com

回复 bxdsam :
惭愧,后来又让我改的乱七八早了
2018-03-21 15:12
bxdsam

bxdsam (作者)

回复 2***@qq.com :
棒棒棒...
2018-03-06 13:39
2***@qq.com

2***@qq.com

用到了,谢谢,完善了一下photo.js代码,可以选择多图与文件类型过滤,代码如下:

/*
* 照片选择类
* @constructor Photo
* @param {String} cfgs.title 修改原生标题
* @param {String} cfgs.cancel 修改取消按钮文字描述
* @param {Array} cfgs.buttons 修改按钮文字描述
* @param {Object} cfgs.options 作为回调函数第三个形参传入
* @param {Function(file,path,options)} cfgs.callback 获取图片成功以后的回调函数,file:文件,path:文件路径,options:初始传入的对象
* */
(function(win) {
//判断组件库是否第一次载入,如果是初始化组件库
win.Plugin = win.Plugin || {};

//Photo类构造函数
function Photo(cfgs) {
var configs = cfgs || {};
var buttons = [{
title: "拍照"
}, {
title: "从手机相册选择"
}];
//初始化数据
this.savePath = configs.savePath || '_doc/tempImage/';
this.title = configs.title || "修改用户头像";
this.cancel = configs.cancel || "取消";
this.buttons = configs.buttons || buttons; //不推荐修改
this.options = configs.options || {};
this.callback = configs.callback || function() {};
this.compress = configs.compress || false; //默认不压缩
this.compressCfg = configs.compressCfg || {}; //默认压缩配置
this.filter = configs.filter || 'image'; //相册选择时的过滤类型默认(image图片),其他 video(仅可选择视频文件) none(不过滤,可选择图片或视频文件)
this.multiple = configs.multiple || false; //true是否多选,默认false(单选)
}

//对象方法定义
Photo.prototype = {
init: function() {
//this.showSheet();
},
showSheet: function() { //点击选择
var that = this;
var actionSheetObj = {
title: that.title,
cancel: that.cancel,
buttons: that.buttons
};
plus.nativeUI.actionSheet(actionSheetObj, function(b) { /*actionSheet 按钮点击事件*/
switch(b.index) {
case 0:
break;
case 1:
that.appendByCamera(that); /*拍照*/
break;
case 2:
that.appendByGallery(that); /*打开相册*/
break;
default:
break;
}
})
},
//拍照
appendByCamera: function(that) {

plus.camera.getCamera().captureImage(function(file) {
plus.io.resolveLocalFileSystemURL(file, function(entry) {
var path = entry.toLocalURL() + "?version=" + new Date().getTime(); //文件路径
var srcPath = entry.toLocalURL();
var dstPath = that.savePath + entry.name;
var compressOptions = {};

entry.file(function(file) {
//console.log("文件名称" + file.name + "文件大小" + file.size);
});

//如果需要压缩
if(that.compress === true) {
//初始化压缩配置对象
for(var i in that.compressCfg) {
compressOptions[i] = that.compressCfg[i];
}
compressOptions["src"] = srcPath;
compressOptions["dst"] = dstPath;
//默认压缩到的目录有同名文件进行覆盖更新
compressOptions["overwrite"] = true;

plus.zip.compressImage(compressOptions, function(event) {
//console.log("拍照需要压缩");
//console.log("压缩成功,文件大小" + event.size + "宽:" + event.width + "高:" + event.height);
that.callback(dstPath, path, that.options);
}, function(error) {
//console.log("压缩图片失败");
});
} else { //如果不需要压缩
//console.log("拍照不需要压缩");
//执行回调函数
that.callback(file, path, that.options);
}
}, function(e) {
console.log("读取拍照文件错误:" + e.message);
});
});
},
//打开相册
appendByGallery: function(that) {

plus.gallery.pick(function(files) {
console.log('photo.js 110 files ' + files)
//临时图片数组
var tempFiles = [];
//原始路径
var originalPath = [];
//临时路径
var tempPath = [];
//resolveLocalFileSystemURL 是异步读取,所以要做个标记,执行定时任务,当标记为真时再读取originalPath与tempPath并回调
var flag = false;

if(typeof files == 'string') {
tempFiles.push(files)
} else {
tempFiles = files.files
};

console.log('photo.js 127 tempFiles: ' + JSON.stringify(tempFiles))

for(var i = 0; i < tempFiles.length; i++) {
//异步读取
plus.io.resolveLocalFileSystemURL(tempFiles[i], function(entry) {

var path = entry.toLocalURL() + "?version=" + new Date().getTime(); //文件路径
var srcPath = entry.toLocalURL();
var dstPath = that.savePath + entry.name;
var compressOptions = {};

var thisFile = tempFiles[i];
entry.file(function(thisFile) {
//console.log("文件名称" + file.name + "文件大小" + file.size);
});

//如果需要压缩
if(that.compress === true) {
//初始化压缩配置对象
for(var i in that.compressCfg) {
compressOptions[i] = that.compressCfg[i];
}
compressOptions["src"] = srcPath;
compressOptions["dst"] = dstPath;
//默认压缩到的目录有同名文件进行覆盖更新
compressOptions["overwrite"] = true;

plus.zip.compressImage(compressOptions, function(event) {
//console.log("相册需要压缩");
//console.log("压缩成功,文件大小" + event.size + "宽:" + event.width + "高:" + event.height + "--" + dstPath);
// that.callback(dstPath, path, that.options);
}, function(error) {
//console.log("压缩图片失败");
});
};
console.log('photo.js 161 dstPath: ' + JSON.stringify(dstPath))
console.log('photo.js 162 path: ' + JSON.stringify(path))
//把临时与原始路径放到相应数组中
tempPath.push(dstPath);
originalPath.push(path)

}, function(e) {
console.log("读取相册文件错误:" + e.message);
}); //end resolveLocalFileSystemURL

//如果是最后一个图片读完,标记为真
if(i == tempFiles.length - 1) {
flag = true;
}
}; //end for

//定时任务-实时获取标记为真才能回调
var timer = setInterval(function(){
if(flag) {
//如果 flag 为真,表示所有图片都读完,就不在执行定时任务
clearInterval(timer);
console.log('photo.js 182 tempPath: ' + JSON.stringify(tempPath))
console.log('photo.js 183 originalPath: ' + JSON.stringify(originalPath))
//如果是单选,取数组第一个
if(!that.multiple) {
tempPath = tempPath[0];
originalPath = originalPath[0]
};
that.callback(tempPath, originalPath, that.options)
}
},500)
}, function(e) {
// console.log("取消选择图片");
}, {
filter: that.filter,
multiple: that.multiple
}) //end pick
}

}

//将Photo类暴露在外部
win.Plugin.Photo = Photo;
})(window);

现在可以选择多张图片了,还可以过滤文件类型

使用:

selectPic.addEventListener('tap', function() {
//拍照或从相册选择图片
var Photo = new Plugin.Photo({
callback: function(file, path, options) {
console.log('463-file(图片的临时相对于本应用的路径):' + JSON.stringify(file))
console.log('464-path(图片的原始路径):' + JSON.stringify(path))
console.log('465-options(初始传入的options对象):' + JSON.stringify(options))
},
options: {haha: 'xixi'},
title: "选择照片",
compress: true,
compressCfg: {
width: '100%'
},
multiple: true
});
Photo.showSheet()

})
2018-01-05 14:41
s***@qq.com

s***@qq.com

回复 bxdsam :
嗯嗯,可以了,非常感谢!
2017-10-18 14:25
bxdsam

bxdsam (作者)

回复 s***@qq.com :
你要在hbuilder里面以app的方式运行然后在手机里面看哦。
2017-10-18 13:18
s***@qq.com

s***@qq.com

下载了你的示例代码,为啥点击“选择照片”的button没有反应?有完整代码吗
2017-10-17 18:11
雪之梦技术驿站

雪之梦技术驿站

mark
2017-08-02 22:50
wenju

wenju

感谢分享 mark~
2017-08-02 20:56