我想在不同宽度和高度的各种位置上显示一幅图像。
我正在使用一种使用Sclar进行裁剪和调整大小的方法,但我有两个问题:
无效的裁剪边界: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);
}发布于 2016-03-08 01:15:59
使用
y = Math.abs((imageHeight - height) / 2);确保Y永远不会是消极的。对其他块中的x也执行同样的操作。
https://stackoverflow.com/questions/32415471
复制相似问题