compress-image.uvue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex:1">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view class="uni-padding-wrap">
  8. <view class="image-container">
  9. <image class="image" :src="beforeCompressPath" mode="aspectFit"></image>
  10. <image class="image" :src="afterCompressPath" mode="aspectFit"></image>
  11. </view>
  12. <view class="uni-title">
  13. <text class="uni-subtitle-text">压缩前图片信息</text>
  14. </view>
  15. <text>{{beforeCompressImageInfo}}</text>
  16. <view class="uni-title">
  17. <text class="uni-subtitle-text">压缩后图片信息</text>
  18. </view>
  19. <text>{{afterCompressImageInfo}}</text>
  20. <view class="uni-btn-v">
  21. <button type="primary" @click="chooseImage">从相册中选取待压缩的图片</button>
  22. </view>
  23. <view class="uni-btn-v">
  24. <button type="primary" @click="compressImage">压缩图片</button>
  25. </view>
  26. </view>
  27. <input-data defaultValue="80" title="压缩质量,范围0~100,数值越小,质量越低,压缩率越高(仅对jpg有效)" type="number"
  28. @confirm="onQualityConfirm"></input-data>
  29. <input-data title="压缩后图片的宽度,单位px" type="string" @confirm="onCompressedWidthConfirm"></input-data>
  30. <input-data title="压缩后图片的高度,单位px" type="string" @confirm="onCompressedHeightConfirm"></input-data>
  31. <input-data defaultValue="0" title="旋转度数,范围0~360" type="number" @confirm="onRotateConfirm"></input-data>
  32. </view>
  33. <!-- #ifdef APP -->
  34. </scroll-view>
  35. <!-- #endif -->
  36. </template>
  37. <script>
  38. // #ifdef APP-ANDROID
  39. import FileInputStream from 'java.io.FileInputStream';
  40. // #endif
  41. export default {
  42. data() {
  43. return {
  44. title: "compressImage",
  45. beforeCompressImageInfo: "",
  46. afterCompressImageInfo: "",
  47. beforeCompressPath: "",
  48. afterCompressPath: "",
  49. quality: 80,
  50. compressedWidth: null as number | null,
  51. compressedHeight: null as number | null,
  52. rotate: 0,
  53. // 自动化测试
  54. imageInfoForTest: null as UTSJSONObject | null,
  55. imageSrcForTest: '/static/test-image/logo.png'
  56. }
  57. },
  58. methods: {
  59. compressImage() {
  60. if (this.beforeCompressPath == "") {
  61. uni.showToast({
  62. title: "请先选择图片",
  63. icon: "error"
  64. });
  65. return;
  66. }
  67. uni.showLoading({
  68. title: "图片压缩中"
  69. });
  70. uni.compressImage({
  71. src: this.beforeCompressPath,
  72. quality: this.quality,
  73. compressedWidth: this.compressedWidth,
  74. compressedHeight: this.compressedHeight,
  75. rotate: this.rotate,
  76. success: (res) => {
  77. console.log("compressImage success", JSON.stringify(res));
  78. this.afterCompressPath = res.tempFilePath;
  79. uni.showToast({
  80. title: "压缩成功",
  81. icon: null
  82. });
  83. uni.getImageInfo({
  84. src: res.tempFilePath,
  85. success: (_res) => {
  86. this.afterCompressImageInfo = `图片宽度: ${_res.width}\n图片高度: ${_res.height}\n`;
  87. // #ifdef APP-ANDROID
  88. const size = new FileInputStream(res.tempFilePath.substring("file://".length)).available() / 1024;
  89. this.afterCompressImageInfo = this.afterCompressImageInfo.concat(`图片大小: ${size}KB`);
  90. // #endif
  91. // #ifdef APP-HARMONY
  92. const fsm = uni.getFileSystemManager()
  93. fsm.getFileInfo({
  94. filePath: res.tempFilePath,
  95. digestAlgorithm: null,
  96. success: (res) => {
  97. this.afterCompressImageInfo = this.afterCompressImageInfo.concat(`图片大小: ${res.size}KB`);
  98. }
  99. })
  100. // #endif
  101. }
  102. });
  103. },
  104. fail: (err) => {
  105. uni.showModal({
  106. title: "压缩图片失败",
  107. content: JSON.stringify(err),
  108. showCancel: false
  109. });
  110. },
  111. complete: (_) => {
  112. uni.hideLoading();
  113. }
  114. });
  115. },
  116. chooseImage() {
  117. uni.chooseImage({
  118. count: 1,
  119. sizeType: ["original"],
  120. sourceType: ["album"],
  121. success: (res) => {
  122. this.beforeCompressPath = res.tempFilePaths[0];
  123. uni.getImageInfo({
  124. src: res.tempFilePaths[0],
  125. success: (_res) => {
  126. this.beforeCompressImageInfo = `图片宽度: ${_res.width}\n图片高度: ${_res.height}\n`;
  127. // #ifdef APP-ANDROID
  128. const size = new FileInputStream(res.tempFilePaths[0].substring("file://".length)).available() / 1024;
  129. this.beforeCompressImageInfo = this.beforeCompressImageInfo.concat(`图片大小: ${size}KB`);
  130. // #endif
  131. // #ifdef APP-HARMONY
  132. const fsm = uni.getFileSystemManager()
  133. fsm.getFileInfo({
  134. filePath: res.tempFilePaths[0],
  135. digestAlgorithm: null,
  136. success: (res) => {
  137. this.beforeCompressImageInfo = this.beforeCompressImageInfo.concat(`图片大小: ${res.size}KB`);
  138. },
  139. fail: (err) => {
  140. console.log(err);
  141. }
  142. })
  143. // #endif
  144. }
  145. });
  146. }
  147. });
  148. },
  149. onQualityConfirm(value : number) {
  150. this.quality = value;
  151. },
  152. onCompressedWidthConfirm(value : string) {
  153. this.compressedWidth = parseInt(value);
  154. },
  155. onCompressedHeightConfirm(value : string) {
  156. this.compressedHeight = parseInt(value);
  157. },
  158. onRotateConfirm(value : number) {
  159. this.rotate = value;
  160. },
  161. testCompressImage() {
  162. uni.compressImage({
  163. src: this.imageSrcForTest,
  164. quality: 50,
  165. compressedWidth: 100,
  166. compressedHeight: 100,
  167. success: (res) => {
  168. uni.getImageInfo({
  169. src: res.tempFilePath,
  170. success: (_res) => {
  171. // #ifdef APP-HARMONY || APP-ANDROID
  172. const fsm = uni.getFileSystemManager()
  173. fsm.getFileInfo({
  174. filePath: this.imageSrcForTest,
  175. digestAlgorithm: null,
  176. success: (imageInfo) => {
  177. fsm.getFileInfo({
  178. filePath: res.tempFilePath,
  179. digestAlgorithm: null,
  180. success: (res) => {
  181. this.imageInfoForTest = {
  182. "width": _res.width,
  183. "height": _res.height,
  184. "isSizeReduce": res.size < imageInfo.size
  185. };
  186. }
  187. })
  188. }
  189. })
  190. // #endif
  191. }
  192. });
  193. },
  194. fail: (_) => {
  195. this.imageInfoForTest = null;
  196. }
  197. });
  198. }
  199. }
  200. }
  201. </script>
  202. <style>
  203. .image {
  204. flex: 1;
  205. }
  206. .image-container {
  207. flex-direction: row;
  208. }
  209. </style>