开发者

apply filter on selected Area of BufferedImage

i want apply some Filters on BufferedImage but don't apply these filters on whole

of bufferedImage , i need apply filt开发者_Go百科er on Rectangle , ellipse , freehand selection of

BufferedImage.anybody have idea ?

thanks


See Graphics.setClip(Shape shape):

Graphics g = image.getGraphics();
g.setClip(shape);

You can then apply the filter on the whole graphics (image) but it will only be applied to the clipping area.


The code below will produce this image:

apply filter on selected Area of BufferedImage

public static void main(String[] args) throws Exception {

    BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) image.getGraphics();

    // set "user defined" clip
    g.setClip(new Polygon(
            new int[] { 50, 100, 50 }, 
            new int[] { 50, 50, 100 },
            3));
    g.fillRect(0, 0, 400, 400);

    // set an ellipse
    g.setClip(new Ellipse2D.Double(100, 100, 200, 200));
    g.fillRect(0, 0, 400, 400);

    // set an rectangle
    g.setClip(new Rectangle(300, 300, 50, 50));
    g.fillRect(0, 0, 400, 400);

    g.dispose();
    ImageIO.write(image, "png", new File("test.png"));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜