// 页面:拍照和从相册选择
// 开发者:糖羽仙
//说明:针对h5+中的拍照和从相册选择上传进行默认压缩处理,图片使用默认直接压缩
//--------plus相机拍摄
let camera = (success, error, zipOptions, cameraOptions) => {
zipOptions = zipOptions ? zipOptions : {};
cameraOptions = cameraOptions ? cameraOptions : {};
plus.camera.getCamera().captureImage(
function(path) {
zipOptions.src = "file://" + plus.io.convertLocalFileSystemURL(path);
if (zipOptions.type !== false) {
imgZip(
zipOptions,
function(event) {
success(event.target);
},
function(err) {
error(err);
}
);
} else {
success(zipOptions.src);
}
},
function(err) {
error(err);
},
cameraOptions
);
};
//--------plus相册上传
let gallery = (success, error, zipOptions, galleryOptions) => {
zipOptions = zipOptions ? zipOptions : {};
galleryOptions = galleryOptions ? galleryOptions : {};
galleryOptions.filter = galleryOptions.filter
? galleryOptions.filter
: "image";
let imgList = [];
galleryOptions.system = galleryOptions.system ? galleryOptions.system : false;
galleryOptions.multiple = galleryOptions.multiple
? galleryOptions.multiple
: false;
plus.gallery.pick(
function(event) {
if (galleryOptions.multiple !== false) {
imgList = event.files;
} else {
imgList.push(event);
}
if (zipOptions.type !== false && galleryOptions.filter === "image") {
recursionImgZip(
imgList,
zipOptions,
function(event) {
if (galleryOptions.multiple !== false) {
success(event);
} else {
success(event[0]);
}
},
function(err) {
error(err);
}
);
} else {
if (galleryOptions.multiple !== false) {
success(imgList);
} else {
success(imgList[0]);
}
}
},
function(err) {
error(err);
},
galleryOptions
);
};
//---------plus图片批量压缩递归
let recursionImgZip = (imgList, zipOptions, success, error, imgZipList, i) => {
i = i ? i : 0;
imgZipList = imgZipList ? imgZipList : [];
zipOptions.src = imgList[i];
if (i <= imgList.length - 1) {
imgZip(
zipOptions,
function(event) {
console.log("压缩后大小是" + event.size / 1024 + "kb"); //--文件大小
imgZipList.push(event.target);
recursionImgZip(imgList, zipOptions, success, error, imgZipList, i + 1);
},
function(err) {
error(err);
}
);
} else {
success(imgZipList);
}
};
//---------plus图片压缩
let imgZip = (zipOptions, success, error) => {
zipOptions.size = zipOptions.size ? zipOptions.size : 1025;
zipOptions.quality = zipOptions.quality ? zipOptions.quality : 1;
plus.io.resolveLocalFileSystemURL(
zipOptions.src,
function(entry) {
//--调用IO文件文件管理系统
entry.file(function(file) {
console.log("文件大小是" + file.size / 1024 + "kb"); //--文件大小
if (zipOptions.size < file.size / 1024) {
plus.zip.compressImage(
zipOptions,
function(event) {
success(event);
},
function(err) {
error(err);
}
);
} else {
success({
target: zipOptions.src,
size: file.size
});
}
});
},
function(e) {
error(e);
}
);
};
export { camera, gallery, test };
[vue与h5+]使用h5+进行拍照或图片上传压缩功能合并
内容版权声明:除非注明,否则皆为本站原创文章。