用Java缩放图像会产生黑空间
我编写了以下函数来缩小图像。但是,当缩放工作时,生成的图像始终是方形图像,并且在图像的底部或右侧有一个黑色空间。我做错了什么用Java缩放图像会产生黑空间,java,image,scaling,bufferedimage,Java,Image,Scaling,Bufferedimage,我编写了以下函数来缩小图像。但是,当缩放工作时,生成的图像始终是方形图像,并且在图像的底部或右侧有一个黑色空间。我做错了什么 private BufferedImage scaleImageTo(BufferedImage image, int width, int height) throws Exception { // Fetch the width and height of the source image, ... int srcWidth = image.getWi
private BufferedImage scaleImageTo(BufferedImage image, int width, int height) throws Exception {
// Fetch the width and height of the source image, ...
int srcWidth = image.getWidth();
int srcHeight = image.getHeight();
// ... verify that it is larger than the target image ...
if (srcWidth < width && srcHeight < height) {
throw new Exception();
}
// ... and setup the target image with the same dimensions.
BufferedImage scaledImage;
if (image.getType() == BufferedImage.TYPE_CUSTOM) {
scaledImage = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
} else {
scaledImage = new BufferedImage(width, height, image.getType());
}
// Calculate the scale parameter.
double scale = 1;
if (srcWidth - width >= srcHeight - height) {
scale = ((double) width) / srcWidth;
} else {
scale = ((double) height) / srcHeight;
}
// Setup the scaling transformation ...
AffineTransform at = new AffineTransform();
at.scale(scale, scale);
// ... and the transformation interpolation type.
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
// Generate the scaled image ...
scaledImage = scaleOp.filter(image, scaledImage);
// ... and return it.
return scaledImage;
}
对于x方向和y方向,始终使用相同的比例因子 尽管您可以通过指定两个这样的比例因子来解决这个问题
double scaleX = (double) width / srcWidth;
double scaleY = (double) height / srcHeight;
AffineTransform at = new AffineTransform();
at.scale(scaleX, scaleY);
我想知道你为什么这样做。仅创建图像的缩放版本通常相当容易…:
private static BufferedImage scaleImageTo(
BufferedImage image, int width, int height)
{
BufferedImage scaledImage =
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = scaledImage.createGraphics();
g.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return scaledImage;
}
是的,我对x和y方向总是使用相同的比例因子,因为否则我会在不保持纵横比的情况下缩放到给定的宽度和高度。如果我总是用相同的因子拉伸,我会保持它。我给出的宽度和高度作为参数应该只限制x和y方向的大小。它有点像一个边界框。但是,我看不到您介绍的缩放机制的好处。这仅仅是另一种方法,还是有一些好处呢?我主要认为它更简单,可能更有效,但到目前为止,这只是一个假设。但是,当你说widt/hight应该尽可能地限制大小时,你可能应该解释一下你的确切意思,把它添加到原始问题中。错误是我使用了边界框的宽度和高度。如果我在实际缩放图像之前计算缩放图像的大小,并使用这些尺寸,它将按预期工作。然而,由于你给我指出了正确的方向,而且你的回答显然是正确的,我接受这一点。