views:

52

answers:

2

Hi All,

I want to cut a particular shape of an image in java, for example an image which contains a man with white background, here I want to crop the man without the background. Don't want to make it as transparent image, want to cut with some coordinates. I think using the cropImageFilter we can only cut the rectangle region. Can anyone tel me how to do this?

Thanks is advance.

A: 

First, you need to create a java.awt.image.BufferedImage from a java.awt.Image. Here's some code to do that, from DZone Snippets.

/**
 * @author Anthony Eden
 */
public class BufferedImageBuilder {

    private static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;

    public BufferedImage bufferImage(Image image) {
        return bufferImage(image, DEFAULT_IMAGE_TYPE);
    }

    public BufferedImage bufferImage(Image image, int type) {
        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        Graphics2D g = bufferedImage.createGraphics();
        g.drawImage(image, null, null);
        waitForImage(bufferedImage);
        return bufferedImage;
    }

    private void waitForImage(BufferedImage bufferedImage) {
        final ImageLoadStatus imageLoadStatus = new ImageLoadStatus();
        bufferedImage.getHeight(new ImageObserver() {
            public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                if (infoflags == ALLBITS) {
                    imageLoadStatus.heightDone = true;
                    return true;
                }
                return false;
            }
        });
        bufferedImage.getWidth(new ImageObserver() {
            public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                if (infoflags == ALLBITS) {
                    imageLoadStatus.widthDone = true;
                    return true;
                }
                return false;
            }
        });
        while (!imageLoadStatus.widthDone && !imageLoadStatus.heightDone) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {

            }
        }
    }

    class ImageLoadStatus {

        public boolean widthDone = false;
        public boolean heightDone = false;
    }

}

Now that you have a BufferedImage, you can use that polygon of coordinates you have to turn the pixels that are not the man, transparent. Just use the methods provided in BufferedImage.

You can't literally cut a polygon from a BufferedImage. A BufferedImage has to be a rectangle. The best you can do is make the parts of the image you don't want transparent. Or, you can put the pixels you do want on another rectangular BufferedImage.

Gilbert Le Blanc
A: 

I'm not sure but class Graphics2D has amethod clip() that accepts a polygon and I think does what you need.

So create a BufferedImage from your image, and get Graphics2D object with createGraphics()

tulskiy