Apply Filter in Selected Area BufferedImage

I want to apply some filters in BufferedImage, but not apply these filters in general

bufferedImage, I need to apply a filter on a rectangle, ellipse, right hand selection

Does BufferedImage.anybody have an idea?

thank

+3
source share
1 answer

See Graphics.setClip ( Shape shape):

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

Then you can apply a filter on the entire graph (image), but it will be applied only to the clipping area.


Below is the code below:

Examples of clips

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"));
}
+4
source

Source: https://habr.com/ru/post/1782985/


All Articles