Java:使用AffineTransform缩放图像时出现线条
我在图像缩放方面遇到问题。当我使用以下代码缩放图像时,它最终会在图像的底部或右侧出现一条线。
double scale = 1;
if (scaleHeight >= scaleWidth) {
scale = scaleWidth;
} else {
scale = scaleHeight;
}
AffineTransform af = new AffineTransform();
af.scale(scale, scale);
AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage bufferedThumb = operation.filter(img, null);
原始图像
缩放后的图像
有谁知道为什么会出现该行?
谢谢!
编辑:
添加了完整的方法代码:
public static final int SPINNER_MAX_WIDTH = 105;
public static final int SPINNER_MAX_HEIGHT = 70;
public void scaleImage(BufferedImage img, int maxWidth, int maxHeight, String fileName) {
double scaleWidth = 1;
double scaleHeight = 1;
if (maxHeight != NOT_SET) {
if (img.getHeight() > maxHeight) {
scaleHeight = (double) maxHeight / (double) img.getHeight();
}
}
if (maxWidth != NOT_SET) {
if (img.getWidth() > maxWidth) {
scaleWidth = (double) maxWidth / (double) img.getWidth();
}
}
double scale = 1;
if (scaleHeight >= scaleWidth) {
scale = scaleWidth;
} else {
scale = scaleHeight;
}
AffineTransform af = new AffineTransform();
af.scale(scale, scale);
AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage bufferedThumb = operation.filter(img, null);
if (bufferedThumb != null) {
File imageFile = new File(fileName);
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
try {
ImageIO.write(bufferedThumb, fileType, imageFile);
} catch (IOException e) {
logger.error("Failed to save scaled image: " + fileName + "\n" + e.getMessage());
}
}
}
方法调用中的 maxWidth 和 maxHeight 参数设置为 SPINNER_MAX_* 常量。
谢谢!
I'm having a problem with image scaling. When I use the following code to scale an image it ends up with a line either at the bottom or on the right side of the image.
double scale = 1;
if (scaleHeight >= scaleWidth) {
scale = scaleWidth;
} else {
scale = scaleHeight;
}
AffineTransform af = new AffineTransform();
af.scale(scale, scale);
AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage bufferedThumb = operation.filter(img, null);
The original image
The scaled image
Does anyone know why the line appears?
Thanks!
EDIT:
Added the complete method code:
public static final int SPINNER_MAX_WIDTH = 105;
public static final int SPINNER_MAX_HEIGHT = 70;
public void scaleImage(BufferedImage img, int maxWidth, int maxHeight, String fileName) {
double scaleWidth = 1;
double scaleHeight = 1;
if (maxHeight != NOT_SET) {
if (img.getHeight() > maxHeight) {
scaleHeight = (double) maxHeight / (double) img.getHeight();
}
}
if (maxWidth != NOT_SET) {
if (img.getWidth() > maxWidth) {
scaleWidth = (double) maxWidth / (double) img.getWidth();
}
}
double scale = 1;
if (scaleHeight >= scaleWidth) {
scale = scaleWidth;
} else {
scale = scaleHeight;
}
AffineTransform af = new AffineTransform();
af.scale(scale, scale);
AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage bufferedThumb = operation.filter(img, null);
if (bufferedThumb != null) {
File imageFile = new File(fileName);
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
try {
ImageIO.write(bufferedThumb, fileType, imageFile);
} catch (IOException e) {
logger.error("Failed to save scaled image: " + fileName + "\n" + e.getMessage());
}
}
}
The maxWidth and maxHeight parameters in the method call is set to the SPINNER_MAX_* constants.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否给我们其余的代码 - 如何操作
bufferedThumb
,因为如果您只是将其保存到文件中应该没问题。你用什么java版本?
编辑:
您可以尝试像这样明确地构造最终图像:
以确保使用什么颜色模式。
我认为您的问题可能与此错误有关:
https://bugs.java.com/bugdatabase/view_bug?bug_id=6725106
另一件事是可能使用不同的插值类型,例如:
有关更多信息,请查看:
http://www.dpreview.com/learn/?/key=interpolation
Can you give us the rest of the code - how you manipulate the
bufferedThumb
, because if you just save it to to a file it should be fine.What java version do you use?
EDITED:
What you can try is to construct the final image explicitly like this:
to make sure what color mode is being used.
I think your issue may be related to this bug:
https://bugs.java.com/bugdatabase/view_bug?bug_id=6725106
Another thing is to maybe use a differnt interpolation type like:
For more information have a look at:
http://www.dpreview.com/learn/?/key=interpolation