简体   繁体   English

在java中为图像添加像素边框

[英]Adding a pixel border to an image in java

I am trying to create an image that adds a border to an existing image on Java by copying the pixels from their old locations to new coordinates. 我试图创建一个图像,通过将像素从旧位置复制到新坐标,为Java上的现有图像添加边框。 So far, this is what I have done: 到目前为止,这就是我所做的:

 public static NewPic border(NewPic p, int borderWidth, Pixel borderColor) {
    int w = 2 * borderWidth;
    int h = 2 * borderWidth;


    Pixel[][] src = p.getBitmap();
    Pixel[][] tgt = new Pixel[w][h];

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            if (x < borderWidth || x >= (w - borderWidth) ||
                    y < borderWidth)
                tgt[x][y] = borderColor;
            else
                tgt[x][y] = src[x - borderWidth][y - borderWidth]; 

        }
    }

return new NewPic(tgt);    

}

Not sure why this isn't passing my test case. 不知道为什么这不通过我的测试用例。 Can anybody provide me with any guidance? 任何人都可以向我提供任何指导吗?

Thanks! 谢谢!

Shouldn't w and h be the width and height of src plus twice the borderwidth? 不应该w和h是src的宽度和高度再加上borderwidth的两倍? You're creating tgt just big enough to hold the border color. 你创造的tgt足够大,可以保持边框颜色。

Hope that helps. 希望有所帮助。

您可以使用图形和绘制线条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM