有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java中放大2倍?

我是Java新手,现在我正在尝试在一个特定的角上放大2倍的图像。目前我有这个方法需要改变On the right is the original Image, on the left is the result I need to have, the left corned is the one zoomed in 2x.为了得到我想要的结果,我应该更改什么?提前谢谢

public static BufferedImage zoomImage(BufferedImage image) {
    int height = image.getHeight();
    int width = image.getWidth();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixelRGB = image.getRGB(x, y);

            int newPixelColor = pixelRGB;
            image.setRGB(width, y, newPixelColor);
        }
    }

    return image;

}

共 (1) 个答案

  1. # 1 楼答案

    您希望将原始图像左上角四分之一的每个像素转换为新图像中的4个像素,直到填满缓冲区。但是,您会意识到从左上角开始意味着您将在读取中间附近的像素之前覆盖它们。你应该从中间开始;将颜色应用于右下角的2x2正方形。然后重复同样的过程,向上向左移动。最后一个操作是使左上角2x2正方形与左上角像素的颜色相同