簡體   English   中英

縮放比例並調整大小

[英]Scalr resize and crop to size

我想在寬度和高度不同的各種位置顯示圖像。

我正在使用Sclar進行裁剪和調整大小的方法,但是有兩個問題:

  1. 在某些情況下,結果看起來不太好。 我認為這是因為代碼中的圖像已首先縮放。
  2. 在其他情況下,我會例外。 例如:

無效的作物邊界:x [32],y [-1],寬度[64]和高度[64]必須全部> = 0

將裁切和圖像調整為目標寬度和高度的最佳方法是什么?

這是我目前的方法:

  public static BufferedImage resizeAndCropToCenter(BufferedImage image, int width, int height) {
    image = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH,
        width * 2, height * 2, Scalr.OP_ANTIALIAS);

    int x, y;

    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    if (imageWidth > imageHeight) {
      x = width / 2;
      y = (imageHeight - height) / 2;
    } else {
      x = (imageWidth - width) / 2;
      y = height / 2;
    }


    return Scalr.crop(image, x, y, width, height);
  }
  1. 在resize方法中,無論尺寸是多少,您始終在執行FIT_TO_WIDTH。 也許您應該根據圖像和所需的尺寸是縱向還是橫向格式進行不同的操作。 您打算在這里實現什么?

  2. 代替

     y = (imageHeight - height) / 2; 

采用

y = Math.abs((imageHeight - height) / 2);

確保y永遠不會為負。 對else塊中的x執行相同的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM