compress-image.uvue 6.1 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2 3 4
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-padding-wrap">
        <view class="image-container">
          <image class="image" :src="beforeCompressPath" mode="aspectFit"></image>
          <image class="image" :src="afterCompressPath" mode="aspectFit"></image>
        </view>
        <view class="uni-title">
          <text class="uni-subtitle-text">压缩前图片信息</text>
        </view>
        <text>{{beforeCompressImageInfo}}</text>
        <view class="uni-title">
          <text class="uni-subtitle-text">压缩后图片信息</text>
        </view>
        <text>{{afterCompressImageInfo}}</text>
        <view class="uni-btn-v">
          <button type="primary" @click="chooseImage">从相册中选取待压缩的图片</button>
        </view>
        <view class="uni-btn-v">
          <button type="primary" @click="compressImage">压缩图片</button>
        </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
26
      </view>
27 28 29 30 31
      <input-data defaultValue="80" title="压缩质量,范围0~100,数值越小,质量越低,压缩率越高(仅对jpg有效)" type="number"
        @confirm="onQualityConfirm"></input-data>
      <input-data title="压缩后图片的宽度,单位px" type="string" @confirm="onCompressedWidthConfirm"></input-data>
      <input-data title="压缩后图片的高度,单位px" type="string" @confirm="onCompressedHeightConfirm"></input-data>
      <input-data defaultValue="0" title="旋转度数,范围0~360" type="number" @confirm="onRotateConfirm"></input-data>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
32 33 34 35 36 37 38
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
39 40 41
  // #ifdef APP-ANDROID
  import FileInputStream from 'java.io.FileInputStream';
  // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
42 43 44 45 46 47 48 49 50 51 52
  export default {
    data() {
      return {
        title: "compressImage",
        beforeCompressImageInfo: "",
        afterCompressImageInfo: "",
        beforeCompressPath: "",
        afterCompressPath: "",
        quality: 80,
        compressedWidth: null as number | null,
        compressedHeight: null as number | null,
53 54
        rotate: 0,
        // 自动化测试
55 56
        imageInfoForTest: null,
        imageSrcForTest: '/static/test-image/logo.png'
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
57 58 59 60
      }
    },
    methods: {
      compressImage() {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
61 62 63 64 65 66 67 68 69 70
        if (this.beforeCompressPath == "") {
          uni.showToast({
            title: "请先选择图片",
            icon: "error"
          });
          return;
        }
        uni.showLoading({
          title: "图片压缩中"
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
71 72 73 74 75 76
        uni.compressImage({
          src: this.beforeCompressPath,
          quality: this.quality,
          compressedWidth: this.compressedWidth,
          compressedHeight: this.compressedHeight,
          rotate: this.rotate,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
77 78 79
          success: (res) => {
            console.log("compressImage success", JSON.stringify(res));
            this.afterCompressPath = res.tempFilePath;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
80 81 82 83 84
            uni.showToast({
              title: "压缩成功",
              icon: null
            });
            uni.getImageInfo({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
85 86
              src: res.tempFilePath,
              success: (_res) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
87 88 89 90 91
                this.afterCompressImageInfo = `图片宽度: ${_res.width}\n图片高度: ${_res.height}\n`;
                // #ifdef APP-ANDROID
                const size = new FileInputStream(res.tempFilePath.substring("file://".length)).available() / 1024;
                this.afterCompressImageInfo = this.afterCompressImageInfo.concat(`图片大小: ${size}KB`);
                // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
92 93 94 95 96
              }
            });
          },
          fail: (err) => {
            uni.showModal({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
97 98 99 100 101 102 103
              title: "压缩图片失败",
              content: JSON.stringify(err),
              showCancel: false
            });
          },
          complete: (_) => {
            uni.hideLoading();
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
104
          }
105
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
106 107 108 109 110 111
      },
      chooseImage() {
        uni.chooseImage({
          count: 1,
          sizeType: ["original"],
          sourceType: ["album"],
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
112 113
          success: (res) => {
            this.beforeCompressPath = res.tempFilePaths[0];
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
114
            uni.getImageInfo({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
115 116
              src: res.tempFilePaths[0],
              success: (_res) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
117 118 119 120 121
                this.beforeCompressImageInfo = `图片宽度: ${_res.width}\n图片高度: ${_res.height}\n`;
                // #ifdef APP-ANDROID
                const size = new FileInputStream(res.tempFilePaths[0].substring("file://".length)).available() / 1024;
                this.beforeCompressImageInfo = this.beforeCompressImageInfo.concat(`图片大小: ${size}KB`);
                // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
122 123 124 125 126
              }
            });
          }
        });
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
127
      onQualityConfirm(value : number) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
128 129
        this.quality = value;
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
130 131
      onCompressedWidthConfirm(value : string) {
        this.compressedWidth = parseInt(value);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
132
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
133 134
      onCompressedHeightConfirm(value : string) {
        this.compressedHeight = parseInt(value);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
135
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
136
      onRotateConfirm(value : number) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
137
        this.rotate = value;
138 139 140
      },
      testCompressImage() {
        uni.compressImage({
141
          src: this.imageSrcForTest,
142 143 144 145 146 147 148
          quality: 50,
          compressedWidth: 100,
          compressedHeight: 100,
          success: (res) => {
            uni.getImageInfo({
              src: res.tempFilePath,
              success: (_res) => {
149
                let beforeCompressSize : number, afterComoressSize : number;
150
                // #ifdef APP-ANDROID
151 152
                beforeCompressSize = new FileInputStream(UTSAndroid.convert2AbsFullPath(this.imageSrcForTest)).available();
                afterComoressSize = new FileInputStream(res.tempFilePath.substring("file://".length)).available();
153 154 155 156
                // #endif
                this.imageInfoForTest = {
                  "width": _res.width,
                  "height": _res.height,
157
                  "isSizeReduce": afterComoressSize < beforeCompressSize
158 159 160 161 162 163 164 165
                };
              }
            });
          },
          fail: (_) => {
            this.imageInfoForTest = null;
          }
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179
      }
    }
  }
</script>

<style>
  .image {
    flex: 1;
  }

  .image-container {
    flex-direction: row;
  }
</style>