[英]Adding a pixel border to an image in java
我試圖創建一個圖像,通過將像素從舊位置復制到新坐標,為Java上的現有圖像添加邊框。 到目前為止,這就是我所做的:
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);
}
不知道為什么這不通過我的測試用例。 任何人都可以向我提供任何指導嗎?
謝謝!
不應該w和h是src的寬度和高度再加上borderwidth的兩倍? 你創造的tgt足夠大,可以保持邊框顏色。
希望有所幫助。
您可以使用圖形和繪制線條。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.