E***@foxmail.com
E***@foxmail.com
  • 发布:2020-10-31 02:40
  • 更新:2020-10-31 02:40
  • 阅读:691

【报Bug】在uni-app中使用5+ api压缩图片,得到的图片却远比原图大

分类:HTML5+

产品分类: HTML5+

HBuilderX版本号: 2.9.5

手机系统: Android

手机系统版本号: Android 7.1.1

手机厂商: Sony

手机机型: Xperia Z5

打包方式: 离线

示例代码:

核心调用代码如下:

  /**  
   * @see http://www.html5plus.org/doc/zh_cn/zip.html#plus.zip.compressImage  
   * @param args  
   */  
  static compressImage(args: {  
    imgPath: string,  // absolute path or relative path  
    targetPath: string,  // absolute path or relative path  
    fileSize: number,  
    retryCount?: number  
  }): Promise<string> {  
    console.log('compress file args:', args)  
    let compressCoefficient: number // 压缩系数  
    if (args.fileSize <= 1024 * 2000) compressCoefficient = 0.5  
    else compressCoefficient = (1024 * 1000) / args.fileSize  
    compressCoefficient = Math.round(compressCoefficient * 100)  
    console.log('compressCoefficient', compressCoefficient)  

    return new Promise((resolve,reject) => {  
      plus.zip.compressImage({  
        src: args.imgPath,  
        dst: args.targetPath,  
        overwrite: true,  
        quality: compressCoefficient,  
      },  
      (event: any) => {  
        const target = event.target // 压缩转换后的图片url路径,以"file://"开头  
        const size = event.size  
        console.log('压缩后文件的大小:', size)  
        if (!args.retryCount) args.retryCount = 0  
        // 三次没有压缩到想要的大小,放弃  
        if (args.retryCount < 3 && size > 1024 * 1000) {  
          args.retryCount ++  
          return resolve(this.compressImage({  
            imgPath: target, targetPath: args.targetPath,  
            fileSize: size, retryCount: args.retryCount  
          }))  
        }  
        resolve(target)  
      },  
      (e: any) => {  
        console.error(e)  
        reject(new CompressImageFailed())  
      }  
      )  
    })  
  }

操作步骤:

执行上述代码

预期结果:

正确压缩(至少别压缩后比原图大几倍,不然就本末倒置了)

实际结果:

压缩后的大小远大于压缩前的大小

bug描述:

原图 100k 使用70的压缩比例 或者 50的压缩比例得到的 值为 900k

原图为1m(1127547)的图片 压缩后 为 1127541 (压缩了6个byte = =!)

2020-10-31 02:40 负责人:无 分享
已邀请:

该问题目前已经被锁定, 无法添加新回复