简体   繁体   English

获取包含Java中BufferedImage中的非透明像素的区域

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

In Java SE 7, I create a BufferedImage object: 在Java SE 7中,我创建了一个BufferedImage对象:

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.) 然后我用颜色填充图像的一部分,使用几个调用fillPolygondrawImage等(注意我的一些颜色的alpha小于255.)

My question is, how can I construct an Area object which contains only the area which was painted to in the BufferedImage ? 我的问题是,如何构造一个仅包含BufferedImage绘制的区域的Area对象? 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. 通过阅读API文档,我相信它可以通过检查getAlphaRaster方法返回的Raster来完成,但我希望有一种更简单(也更快)的方法。

..how can I construct an Area object which contains only the area which was painted to in the BufferedImage ? ..我可以构造一个Area对象,它只包含在BufferedImage绘制的区域吗?

The getOutline(..) method seen in this source could be used to distinguish between the opaque and translucent parts of the image. 在此源中看到的getOutline(..)方法可用于区分图像的不透明和半透明部分。 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. 你需要调整它来检查alpha <255。

The ShapeContainment class would be used to determine if a point falls into any of a number of Area instances. ShapeContainment类将用于确定某个点是否属于多个Area实例中的任何一个。

Other code deailing with Java-2D and images can be seen in OneRing & ShapeCollision . OneRingShapeCollision可以看到其他使用Java-2D和图像的ShapeCollision

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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