微信小程序拍照并提取base64数据

2018-12-20 14:20:50  卢浮宫  版权声明:本文为站长原创文章,转载请写明出处


一、前言

    在微信小程序中有一个chooseImage方法可以调用系统相机或上传照片,值得注意的是这里生成的src路径是一个Https的URL。

由于不知道这个URL是都为长期有效,所以这边转换成base64编码存放到服务器上了。

PS: 如果这个url是长期有效的,那么我们直接记录这个URL即可,当然这也是最佳选择。


二、这里贴一下当时的核心代码部分


    var thi = this;
wx.chooseImage({
count: 1,
sizeType: ["compressed"],
sourceType: ["camera"],
success: function(res) {
setTimeout(function() {
thi.data.imgSrcArr.push(res.tempFilePaths);
// thi.data.imgSrcArr[event.currentTarget.dataset.btnindex] = res.tempFilePaths;
thi.setData({
imgSrcArr: thi.data.imgSrcArr
});
}, 1000)
//保存数据到服务器
wx.request({
url: res.tempFilePaths.toString(),
method: "GET",
responseType: "arraybuffer",
success: function (res) {
var base64 = "data:image/png;base64," + wx.arrayBufferToBase64(res.data);
thi.saveToServer(base64);
}
});
},
fail: function(error) {
console.error("调用本地相册文件时出错")
wx.showToast({
title: error,
})
},
complete: function() {

}
})





更多精彩请关注guangmuhua.com


最新评论: