2012-03-01 81 views
0

我想重复边框像素n次。我设法为顶部边界的一部分做到了这一点,但我正在与其他部门斗争。有没有更简单的方法来做到这一点,因为它似乎过于复杂,我试图实现。BufferedImage重复的边框

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

你是不是放大图像,顺便说一句正确的位置 - 只是将其移动'n'个像素...你能澄清你想达到的目标吗? “复制边框像素”是什么意思?一个我们可以真正运行的适当的[SCCEE](http://sscce.org),而不是部分代码会让我们更容易帮助。 – DNA 2012-03-01 19:11:17

回答

0

底部边框的问题是y值和.setRGB的for循环。您的.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)); 
    } 
}