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

旧巷老猫 提交于 2019-12-22 00:00:35

问题


I want to reduce the size of images taken by camera API but quality reduce is not good. The best thing is to reduce resolution but I don't want to use target width and height for all images.

For example, I want image width be 1280 and image height change automatically by its ratio, but in API I should use exact width and height.

How can I change height dynamic by image ratio???

For now, I use this code:

 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
})

回答1:


Use these options from the Cordova plugin:

targetWidth: Width in pixels to scale image. Must be used with targetHeight. Aspect ratio is maintained. (Number)

targetHeight: Height in pixels to scale image. Must be used with targetWidth. Aspect ratio is maintained. (Number)

Like this

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) => {

  })



回答2:


I use image resizer native API in camera and it works. when value assign for width and height it turn greater one to the target value and adjust another one by original ratio.. here is my code:

 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))

}



来源:https://stackoverflow.com/questions/46150790/how-to-resize-image-in-ionic-3-without-quality-reduce-and-target-width-and-heigh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!