简体   繁体   中英

Get an Area containing the non-transparent pixels in a BufferedImage in Java

In Java SE 7, I create a BufferedImage object:

BufferedImage i = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = i.createGraphics();

And then I fill parts of the image with color, using several call to fillPolygon , drawImage , etc. (Note that some of my colors have an alpha less than 255.)

My question is, how can I construct an Area object which contains only the area which was painted to in the BufferedImage ? From reading the API docs, I believe it could be done by inspecting the Raster returned by the getAlphaRaster method, but I'm hoping there's an easier (and faster?) way of doing this.

..how can I construct an Area object which contains only the area which was painted to in the BufferedImage ?

The getOutline(..) method seen in this source could be used to distinguish between the opaque and translucent parts of the image. Having said that, the method only checks for an 'exact match' between target color and image color for that pixel. You'd need to adjust it to instead check for alpha <255.

The ShapeContainment class would be used to determine if a point falls into any of a number of Area instances.

Other code deailing with Java-2D and images can be seen in OneRing & ShapeCollision .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM