You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

photoCompress.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. function chooseImage(options) {
  2. const self = options.self;
  3. uni.chooseImage({
  4. count: 1,
  5. sizeType: ['compressed'],
  6. sourceType: ['album', 'camera'],
  7. success: (res) => {
  8. console.log('图片选择成功', new Date().getSeconds());
  9. let fileSize = res.tempFiles[0].size / 1024;
  10. console.log('图fileSize', fileSize);
  11. const endSize = options.endSize || 80;
  12. console.log('endSize', endSize);
  13. if (fileSize > endSize) {
  14. photoCompress(
  15. {
  16. self: self,
  17. path: res.tempFiles[0].path,
  18. quality: 8,
  19. endSize: endSize
  20. },
  21. options.success,
  22. options.fail
  23. );
  24. } else {
  25. photoHandle(
  26. {
  27. self: self,
  28. endSize: options.endSize,
  29. path: res.tempFiles[0].path
  30. },
  31. options.success,
  32. options.fail
  33. );
  34. }
  35. },
  36. fail: (err) => {
  37. // console.log(err)
  38. if (options.fail) options.fail(err);
  39. }
  40. });
  41. } // 图片压缩
  42. /*
  43. self 页面this
  44. path 图片地址,uni.getImageInfo要求path是图片的本地路径
  45. quality 图片质量 1-10区间,因为下方会乘以0.1
  46. callback 回调返回 (path, base64)
  47. */
  48. function photoCompress(options, callback, failCallback) {
  49. const self = options.self;
  50. const path = options.path;
  51. const quality = options.quality;
  52. const endSize = options.endSize;
  53. console.log('photoCompress压缩开始', options);
  54. uni.getImageInfo({
  55. src: path,
  56. success: (Infores) => {
  57. const initW = Infores.width,
  58. initH = Infores.height;
  59. if (initW > initH) {
  60. var width = initW > 800 ? 800 : initW;
  61. var base = initW / width;
  62. var height = parseInt(initH / base);
  63. } else {
  64. var height = initH > 800 ? 800 : initH;
  65. var base = initH / height;
  66. var width = parseInt(initW / base);
  67. }
  68. self.setData({
  69. canvas_quality: quality,
  70. canvasW: width,
  71. canvasH: height
  72. });
  73. console.log('getImageInfo--', Infores, width, height, base, quality);
  74. let ctx = uni.createCanvasContext('myCanvas');
  75. ctx.drawImage(Infores.path, 0, 0, width, height);
  76. ctx.draw(true, () => {
  77. setTimeout(() => {
  78. uni.canvasToTempFilePath({
  79. quality: quality * 0.1,
  80. fileType: 'jpg',
  81. //quality仅对jpg有效
  82. canvasId: 'myCanvas',
  83. width: width,
  84. height: height,
  85. destWidth: width,
  86. destHeight: height,
  87. success: (res) => {
  88. console.log('canvasToTempFilePath--', res);
  89. photoHandle(
  90. {
  91. self: self,
  92. endSize: endSize,
  93. path: res.tempFilePath
  94. },
  95. callback,
  96. failCallback
  97. );
  98. ctx.clearRect(0, 0, width, height);
  99. },
  100. fail: (err) => {
  101. console.log('canvasToTempFilePath--fail', err);
  102. if (failCallback) failCallback(err);
  103. }
  104. });
  105. }, 300);
  106. });
  107. },
  108. fail: (err) => {
  109. if (failCallback) failCallback(err);
  110. }
  111. });
  112. } // 生成base64
  113. function photoHandle(options, callback, failCallback) {
  114. const self = options.self;
  115. const path = options.path;
  116. ``;
  117. const endSize = options.endSize;
  118. console.log('photoHandle', options);
  119. let base64Img = 'data:image/jpg;base64,' + uni.getFileSystemManager().readFileSync(path, 'base64');
  120. if (showSize(base64Img) / 1 > endSize) {
  121. console.log('压缩后', showSize(base64Img));
  122. self.data.canvas_quality = self.data.canvas_quality - 1;
  123. if (self.data.canvas_quality <= 4) {
  124. //后面备注该处处理原因
  125. console.log('跳出压缩', showSize(base64Img));
  126. callback &&
  127. callback({
  128. path: path,
  129. base64: base64Img
  130. });
  131. return;
  132. }
  133. photoCompress(
  134. {
  135. self: self,
  136. path: path,
  137. quality: self.data.canvas_quality,
  138. endSize: endSize
  139. },
  140. callback,
  141. failCallback
  142. );
  143. } else {
  144. console.log('压缩结束', showSize(base64Img));
  145. callback &&
  146. callback({
  147. path: path,
  148. base64: base64Img
  149. });
  150. }
  151. } // 获取base64图片大小,返回kb数字
  152. function showSize(base64url) {
  153. //把头部去掉
  154. var str = base64url.replace('data:image/png;base64,', ''); // 找到等号,把等号也去掉
  155. var equalIndex = str.indexOf('=');
  156. if (str.indexOf('=') > 0) {
  157. str = str.substring(0, equalIndex);
  158. } // 原来的字符流大小,单位为字节
  159. var strLength = str.length; // 计算后得到的文件流大小,单位为字节
  160. var fileLength = parseInt(strLength - (strLength / 8) * 2); // 由字节转换为kb
  161. var size = '';
  162. size = (fileLength / 1024).toFixed(2);
  163. var sizeStr = size + ''; //转成字符串
  164. var index = sizeStr.indexOf('.'); //获取小数点处的索引
  165. var dou = sizeStr.substr(index + 1, 2); //获取小数点后两位的值
  166. if (dou == '00') {
  167. //判断后两位是否为00,如果是则删除00
  168. return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2);
  169. }
  170. return size;
  171. }
  172. module.exports = {
  173. chooseImage,
  174. photoCompress,
  175. photoHandle
  176. };