幅と高さが異なるあらゆる種類の配置で画像を表示したいと思います。
私はSclarでトリミングとサイズ変更の方法を使用していますが、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);
}