【发布时间】:2015-01-31 23:12:12
【问题描述】:
我想要实现的是获取image 的掩码,将其转换为尺寸为(image.getWidth(), image.getHeight()) 的Array,然后获取所有像素并查看它们是否具有0 的alpha 值.
如果他们这样做了,那么:
- 为我目前正在检查的
x, y坐标添加值1。
否则:
- 为我目前正在检查的
x, y坐标添加值0。
到目前为止,我知道如何编程。如果您有兴趣,这是我正在使用的代码:
private int[] createMask(BufferedImage image) {
final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
final boolean hasAlphaChannel = image.getAlphaRaster() != null;
int[][] result = new int[height][width];
if (hasAlphaChannel) {
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += 4) {
int alpha = pixels[pixel];
if(alpha != 0) {
result[row][col] = 1;
} else {
result[row][col] = 0;
}
if (col == width) {
col = 0;
row++;
}
}
}
return result;
}
在我得到这个面具之后,我试图使用这个Array 通过这个代码或者它的一个替代品来绘制一个Polygon(从http://docs.oracle.com/javase/tutorial/2d/geometry/arbitrary.html 获得):
int x1Points[] = {0, 100, 0, 100};
int y1Points[] = {0, 50, 50, 0};
GeneralPath polygon =
new GeneralPath(GeneralPath.WIND_EVEN_ODD,
x1Points.length);
polygon.moveTo(x1Points[0], y1Points[0]);
for (int index = 1; index < x1Points.length; index++) {
polygon.lineTo(x1Points[index], y1Points[index]);
};
polygon.closePath();
g2.draw(polygon);
但是,我需要创建一个方法,它可以为我提供由 Point 对象组成的 Array 中的所有坐标,以围绕图像创建一个“蒙版”。
public Point[] getCords(int[] mask) {
ArrayList<Point> points = new ArrayList<Point>(); //you can change this to whatever you want to use
//get coords to surround the mask
// >> involving `0` (for transparent) and `1` (non transparent)
// >> these are contained in the `mask` Array...
return points.toArray(new Points[0]);
所以,总结一下:
- 我需要从包含值
1和0的int[]数组中获取一个勾勒出图像可见像素的多边形,前者用于非透明像素,后者用于透明像素,分别。
【问题讨论】:
-
您的问题有点误导,因为您两次陈述“您要实现的目标”,而这两个实例说明了不同的目标(一个是您开始工作,另一个是实际问题)。
-
@Suseika 抱歉,这实际上是不正确的措辞。我只是在陈述我已经拥有的东西。
-
没关系,只需编辑您的问题,以便将来有类似问题的人能够欣赏您的问题。