123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- function chooseImage(options) {
- const self = options.self;
- uni.chooseImage({
- count: 1,
- sizeType: ['compressed'],
- sourceType: ['album', 'camera'],
- success: (res) => {
- console.log('图片选择成功', new Date().getSeconds());
- let fileSize = res.tempFiles[0].size / 1024;
- console.log('图fileSize', fileSize);
- const endSize = options.endSize || 80;
- console.log('endSize', endSize);
-
- if (fileSize > endSize) {
- photoCompress(
- {
- self: self,
- path: res.tempFiles[0].path,
- quality: 8,
- endSize: endSize
- },
- options.success,
- options.fail
- );
- } else {
- photoHandle(
- {
- self: self,
- endSize: options.endSize,
- path: res.tempFiles[0].path
- },
- options.success,
- options.fail
- );
- }
- },
- fail: (err) => {
- // console.log(err)
- if (options.fail) options.fail(err);
- }
- });
- } // 图片压缩
-
- /*
- self 页面this
- path 图片地址,uni.getImageInfo要求path是图片的本地路径
- quality 图片质量 1-10区间,因为下方会乘以0.1
- callback 回调返回 (path, base64)
- */
-
- function photoCompress(options, callback, failCallback) {
- const self = options.self;
- const path = options.path;
- const quality = options.quality;
- const endSize = options.endSize;
- console.log('photoCompress压缩开始', options);
- uni.getImageInfo({
- src: path,
- success: (Infores) => {
- const initW = Infores.width,
- initH = Infores.height;
-
- if (initW > initH) {
- var width = initW > 800 ? 800 : initW;
- var base = initW / width;
- var height = parseInt(initH / base);
- } else {
- var height = initH > 800 ? 800 : initH;
- var base = initH / height;
- var width = parseInt(initW / base);
- }
-
- self.setData({
- canvas_quality: quality,
- canvasW: width,
- canvasH: height
- });
- console.log('getImageInfo--', Infores, width, height, base, quality);
- let ctx = uni.createCanvasContext('myCanvas');
- ctx.drawImage(Infores.path, 0, 0, width, height);
- ctx.draw(true, () => {
- setTimeout(() => {
- uni.canvasToTempFilePath({
- quality: quality * 0.1,
- fileType: 'jpg',
- //quality仅对jpg有效
- canvasId: 'myCanvas',
- width: width,
- height: height,
- destWidth: width,
- destHeight: height,
- success: (res) => {
- console.log('canvasToTempFilePath--', res);
- photoHandle(
- {
- self: self,
- endSize: endSize,
- path: res.tempFilePath
- },
- callback,
- failCallback
- );
- ctx.clearRect(0, 0, width, height);
- },
- fail: (err) => {
- console.log('canvasToTempFilePath--fail', err);
- if (failCallback) failCallback(err);
- }
- });
- }, 300);
- });
- },
- fail: (err) => {
- if (failCallback) failCallback(err);
- }
- });
- } // 生成base64
-
- function photoHandle(options, callback, failCallback) {
- const self = options.self;
- const path = options.path;
- ``;
- const endSize = options.endSize;
- console.log('photoHandle', options);
- let base64Img = 'data:image/jpg;base64,' + uni.getFileSystemManager().readFileSync(path, 'base64');
-
- if (showSize(base64Img) / 1 > endSize) {
- console.log('压缩后', showSize(base64Img));
- self.data.canvas_quality = self.data.canvas_quality - 1;
-
- if (self.data.canvas_quality <= 4) {
- //后面备注该处处理原因
- console.log('跳出压缩', showSize(base64Img));
- callback &&
- callback({
- path: path,
- base64: base64Img
- });
- return;
- }
-
- photoCompress(
- {
- self: self,
- path: path,
- quality: self.data.canvas_quality,
- endSize: endSize
- },
- callback,
- failCallback
- );
- } else {
- console.log('压缩结束', showSize(base64Img));
- callback &&
- callback({
- path: path,
- base64: base64Img
- });
- }
- } // 获取base64图片大小,返回kb数字
-
- function showSize(base64url) {
- //把头部去掉
- var str = base64url.replace('data:image/png;base64,', ''); // 找到等号,把等号也去掉
-
- var equalIndex = str.indexOf('=');
-
- if (str.indexOf('=') > 0) {
- str = str.substring(0, equalIndex);
- } // 原来的字符流大小,单位为字节
-
- var strLength = str.length; // 计算后得到的文件流大小,单位为字节
-
- var fileLength = parseInt(strLength - (strLength / 8) * 2); // 由字节转换为kb
-
- var size = '';
- size = (fileLength / 1024).toFixed(2);
- var sizeStr = size + ''; //转成字符串
-
- var index = sizeStr.indexOf('.'); //获取小数点处的索引
-
- var dou = sizeStr.substr(index + 1, 2); //获取小数点后两位的值
-
- if (dou == '00') {
- //判断后两位是否为00,如果是则删除00
- return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2);
- }
-
- return size;
- }
-
- module.exports = {
- chooseImage,
- photoCompress,
- photoHandle
- };
|