BufferedImage重复边框

时间:2012-03-01 18:23:38

标签: java bufferedimage

我试图复制边框像素n次。我已经设法为顶部边界的一部分做了这件事,但我正在努力与其余部分。有没有更简单的方法来做到这一点,因为它似乎过于复杂,我想要实现的目标。

public BlurredImage(int n) {

    try {
        castle = ImageIO.read(this.getClass().getResource("test.png"));
    } catch (IOException e) {
        System.out.println("cannot read image");
    }

    int w = (2*n)+castle.getWidth();
    int h = (2*n)+castle.getHeight();

    int origW =castle.getWidth();
    int origH = castle.getHeight();
    System.out.println(w);
    System.out.println(h);
    BufferedImage enlargedImage = new BufferedImage(w, h, castle.getType());

    //Map existing image
    for (int y=0; y < origH; y++){
        for (int x=0; x < origW; x++){
            enlargedImage.setRGB(x+n, y+n, castle.getRGB(x, y));
        }
    }

    //Top border
    for (int y=0; y < n; y++){
        for (int x=0; x < origW; x++){
            enlargedImage.setRGB(x+n, y, castle.getRGB(x, 0));
        }
    }

    //Bottom border
    for (int y=0; y > y+n; y++){
        for (int x=0; x < origW; x++){
            enlargedImage.setRGB(x+n, y, castle.getRGB(x, 0));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

底部边框的问题是y值的for循环和.setRGB。您的.setRGB似乎是顶部边框的复制/粘贴,并且正在获取/设置与顶部边框代码相同的像素。我稍微更改了你的代码:y for循环现在迭代你想要的边框副本数量,setRGB / getRGB引用图像中的正确位置

//Bottom border
for (int y = 0; y < n; y++){
    for (int x = 0; x < origW; x++){
        enlargedImage.setRGB(x + n, h - 1 - y, castle.getRGB(x, origH - 1));
    }
}