Java:使用AffineTransform缩放图像时出现线条

发布于 2024-08-26 11:53:51 字数 2084 浏览 9 评论 0原文

我在图像缩放方面遇到问题。当我使用以下代码缩放图像时,它最终会在图像的底部或右侧出现一条线。

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

enter image description here

The scaled image

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-09-02 11:53:51

您能否给我们其余的代码 - 如何操作 bufferedThumb,因为如果您只是将其保存到文件中应该没问题。

ImageIO.write(bufferedThumb, "PNG", new File("img.png"));

你用什么java版本?

编辑:

您可以尝试像这样明确地构造最终图像:

BufferedImage bufferedThumb = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_ARGB);
operation.filter(img, bufferedThumb);

以确保使用什么颜色模式。

我认为您的问题可能与此错误有关:
https://bugs.java.com/bugdatabase/view_bug?bug_id=6725106

另一件事是可能使用不同的插值类型,例如:

AffineTransformOp.TYPE_BILINEAR

有关更多信息,请查看:
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.

ImageIO.write(bufferedThumb, "PNG", new File("img.png"));

What java version do you use?

EDITED:

What you can try is to construct the final image explicitly like this:

BufferedImage bufferedThumb = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_ARGB);
operation.filter(img, bufferedThumb);

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:

AffineTransformOp.TYPE_BILINEAR

For more information have a look at:
http://www.dpreview.com/learn/?/key=interpolation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文