如何在不降低质量和目标宽度和高度的情况下调整 Ionic 3 中的图像大小?

How to resize image in Ionic 3 without quality reduce and target width and height?

我想缩小相机拍摄的图像的尺寸API,但质量降低并不好。最好的办法是降低分辨率,但我不想对所有图像都使用目标宽度和高度。

例如,我希望图像宽度为 1280,图像高度按比例自动变化,但在 API 中我应该使用精确的宽度和高度。

如何根据图像比例动态更改高度???

现在,我使用这个代码:

 this.camera.getPicture({
  quality: 60,
  destinationType: this.camera.DestinationType.FILE_URI,
  sourceType: sourceType,
  mediaType: this.camera.MediaType.PICTURE,
  targetWidth: 1280,
  targetHeight: 1280,
  encodingType: this.camera.EncodingType.JPEG,
  saveToPhotoAlbum: (source === PictureSource.CAMERA),
  allowEdit: true
})

我在相机中使用本机图像缩放器 API,它可以正常工作。当为宽度和高度分配值时,它会将较大的一个变为目标值并按原始比例调整另一个.. 这是我的代码:

 this.camera.getPicture({
  destinationType: this.camera.DestinationType.FILE_URI,
  sourceType: sourceType,
  mediaType: this.camera.MediaType.PICTURE,
  encodingType: this.camera.EncodingType.JPEG,
  saveToPhotoAlbum: (source === PictureSource.CAMERA),
  allowEdit: true
})
  .then(imageUri => {
    this.imageResizer.resize({
      uri: imageUri,
      quality: 60,
      width: 1280,
      height: 1280
    }).then(uri => handler(uri))
  })
  .catch(error => console.warn(error))

}

使用 Cordova 插件中的这些选项:

targetWidth:缩放图像的宽度(以像素为单位)。必须与 targetHeight 一起使用。纵横比保持不变。 (数量)

targetHeight:缩放图像的高度(以像素为单位)。必须与 targetWidth 一起使用。纵横比保持不变。 (数量)

像这样

var options = {
      quality: 100,
      sourceType: sourceType,
      saveToPhotoAlbum: false,
      correctOrientation: true,
      destinationType: this.camera.DestinationType.DATA_URL,
      targetWidth: 400,
      targetHeight: 400,
    };

// Get the data of an image
this.camera.getPicture(options).then((imageData) => {
//use the imageData as required.
}, (err) => {

}).catch((error) => {

  })