function plusReady(){
// 弹出系统选择按钮框
mui("body").on("tap","#cs1",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:appendByCamera();break;
case 2:appendByGallery();break;
}
});
}
//摄像头
}
// 拍照添加文件
function appendByCamera(){
plus.camera.getCamera().captureImage(function(e){
// console.log(e);
plus.io.resolveLocalFileSystemURL(e, function(entry) {
var path = entry.toLocalURL();
document.getElementById('cs').src= path;
diaoyongg();
// ImgSrc.push(path);
//就是这里www.bcty365.com
}, function(e) {
mui.toast("读取拍照文件错误:" + e.message);
});
});
}
//从相册添加文件
function appendByGallery(){
plus.gallery.pick(function(path){
//ImgSrc= path;
document.getElementById('cs').src= path;
diaoyongg();
// ImgSrc.push(path);
});
}
//转成base64 并压缩
function dealImage(path, callback){
var img = new Image();
img.src = path;
img.onload = function(){
//默认按比例压缩
var w = this.width,
h = this.height;
var quality = 0.95; // 默认图片质量为0.7
//生成canvas
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
// 创建属性节点
canvas.setAttribute("width", w);
canvas.setAttribute("height", h);
ctx.drawImage(this, 0, 0, w, h);
// quality值越小,所绘制出的图像越模糊
var base64 = canvas.toDataURL('image/jpeg', quality);
// 回调函数返回base64的值
callback(base64);
};
};
//储存地址
var ImgSrc= new Array();
// 调用函数处理图片
function diaoyongg(){
var im=document.getElementById('cs').src;//这个是获取从相册拍照传过来的地址
dealImage(im,function(base){
//直接将获取到的base64的字符串,放到一个image标签中就可看到测试后的压缩之后的样式图了
//document.getElementById("cs").src = base;
console.log("压缩后:" + base.length / 1024 + " " + base);
var wt=plus.nativeUI.showWaiting();
$.ajax({
url:'http://pin.91dlg.com/api/index/upload',
data:{
image:base,
token:tok
},
dataType:'json',
type:'post',
success:function (data) {
console.log(data);
console.log();
ImgSrc.push(data.data.data);
//console.log(document.getElementById("cs1").src);
console.log(ImgSrc.length);
var s='';
for(var i=0;i<ImgSrc.length;i++){
s+="<img src='http://pin.91dlg.com/"+ImgSrc[i]+"' class='image-item' />"
}
$('#cs').html(s);
wt.close();//关闭等待提示按钮
},
error:function () {
console.log('error');
wt.close();//关闭等待提示按钮
}
})
})
}
// 弹出系统选择按钮框
mui("body").on("tap","#cs1",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:appendByCamera();break;
case 2:appendByGallery();break;
}
});
}
//摄像头
}
// 拍照添加文件
function appendByCamera(){
plus.camera.getCamera().captureImage(function(e){
// console.log(e);
plus.io.resolveLocalFileSystemURL(e, function(entry) {
var path = entry.toLocalURL();
document.getElementById('cs').src= path;
diaoyongg();
// ImgSrc.push(path);
//就是这里www.bcty365.com
}, function(e) {
mui.toast("读取拍照文件错误:" + e.message);
});
});
}
//从相册添加文件
function appendByGallery(){
plus.gallery.pick(function(path){
//ImgSrc= path;
document.getElementById('cs').src= path;
diaoyongg();
// ImgSrc.push(path);
});
}
//转成base64 并压缩
function dealImage(path, callback){
var img = new Image();
img.src = path;
img.onload = function(){
//默认按比例压缩
var w = this.width,
h = this.height;
var quality = 0.95; // 默认图片质量为0.7
//生成canvas
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
// 创建属性节点
canvas.setAttribute("width", w);
canvas.setAttribute("height", h);
ctx.drawImage(this, 0, 0, w, h);
// quality值越小,所绘制出的图像越模糊
var base64 = canvas.toDataURL('image/jpeg', quality);
// 回调函数返回base64的值
callback(base64);
};
};
//储存地址
var ImgSrc= new Array();
// 调用函数处理图片
function diaoyongg(){
var im=document.getElementById('cs').src;//这个是获取从相册拍照传过来的地址
dealImage(im,function(base){
//直接将获取到的base64的字符串,放到一个image标签中就可看到测试后的压缩之后的样式图了
//document.getElementById("cs").src = base;
console.log("压缩后:" + base.length / 1024 + " " + base);
var wt=plus.nativeUI.showWaiting();
$.ajax({
url:'http://pin.91dlg.com/api/index/upload',
data:{
image:base,
token:tok
},
dataType:'json',
type:'post',
success:function (data) {
console.log(data);
console.log();
ImgSrc.push(data.data.data);
//console.log(document.getElementById("cs1").src);
console.log(ImgSrc.length);
var s='';
for(var i=0;i<ImgSrc.length;i++){
s+="<img src='http://pin.91dlg.com/"+ImgSrc[i]+"' class='image-item' />"
}
$('#cs').html(s);
wt.close();//关闭等待提示按钮
},
error:function () {
console.log('error');
wt.close();//关闭等待提示按钮
}
})
})
}