這裏的基本問題是,我繪製了一個圖像,它是grayScaled並且是該形狀的Rectangle邊界。我已經在那個矩形上繪製了形狀。現在我需要從圖像中刪除訪問區域。如何從形狀中減去圖像的面積
的代碼以獲得矩形邊界從形狀是:
public static Rectangle getBoundingBox(Shape shape,Graphics2D g) {
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int maxY = Integer.MIN_VALUE;
final Rectangle polygonBounds = shape.getBounds();
int ax = polygonBounds.x;
int ay = polygonBounds.y;
int bx = ax + polygonBounds.width;
int by = ay + polygonBounds.height;
minX = Math.min(ax, minX);
minY = Math.min(ay, minY);
maxX = Math.max(bx, maxX);
maxY = Math.max(by, maxY);
final Rectangle boundingBox = new Rectangle(minX, minY, 1, 1);
boundingBox.add(maxX, maxY);
return boundingBox;
}
以繪製矩形和所選擇的形狀的代碼是:
Rectangle rect = getBoundingBox((Shape)selectedArea,g);
int x = (int)rect.getX();
int y = (int)rect.getY();
int width = (int)rect.getWidth();
int height = (int)rect.getHeight();
if((x+width)>grayScaledImage.getWidth()){
width = grayScaledImage.getWidth()-x;
}
if(((y+height)>grayScaledImage.getHeight())){
height = grayScaledImage.getHeight()-y;
}
BufferedImage img = grayScaledImage.getSubimage(x,y,width,height);
}
}
}
g.drawImage(img,x,y,null);
和代碼到內部灰度圖像矩形是:
private BufferedImage toGray(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color c = new Color(image.getRGB(j, i));
int red = (int) (c.getRed() * 0.3);
int green = (int) (c.getGreen() * 0.59);
int blue = (int) (c.getBlue() * 0.11);
int sum = red + green + blue;
Color newColor = new Color(sum, sum, sum);
//Color newColor =Color.red;
image.setRGB(j, i, newColor.getRGB());
}
}
return image;
}
我已經嘗試了setClip()方法,但它有我的抗鋸齒問題。所以得到一些幫助會很好。需要
光灰度化部以從圖像中減去:
,圖像被。
所需要的圖像是:
多邊形可以是任何形狀的。
可以說你想要什麼樣的結果?最好製作一張你想要的圖像。 –
@BahramdunAdil我想要的是繪製灰度圖像和任何多邊形以上的顏色 –
您的意思是說,複製具有多邊形形狀的圖像部分並將其放入新圖像中?但最好發佈最後想要的圖像,意味着您希望結果圖像是什麼樣的結果,編輯問題併發布圖像。 –