[英]Checking if an image is blank in Java
I'm currently working on a dynamic Animation Loader for Characters in a game and for that I'm in need of detecting if the current frame is completely blank in order to stop loading more sprites.我目前正在为游戏中的角色开发动态动画加载器,为此我需要检测当前帧是否完全空白以停止加载更多精灵。 This is what I'm currently using to find out if the current image is blank:这是我目前用来确定当前图像是否为空白的方法:
public static boolean isBlankImage(BufferedImage b) {
byte[] pixels1 = getPixels(b);
byte[] pixels2 = getPixels(getBlankImage(b.getWidth(), b.getHeight()));
return Arrays.equals(pixels1, pixels2);
}
private static BufferedImage getBlankImage(int width, int height) {
return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
private static byte[] getPixels(BufferedImage b) {
byte[] pixels = ((DataBufferByte) b.getRaster().getDataBuffer()).getData();
return pixels;
}
However, as soon as I run it, I get this annoying error:但是,一旦我运行它,我就会收到这个烦人的错误:
Exception in thread "Thread-0" java.lang.ClassCastException:
java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte
I've tried switching the casting type but all I get in return is:我试过切换铸造类型,但我得到的回报是:
Exception in thread "Thread-0" java.lang.ClassCastException:
java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
I've searched all over the place for an answer to no avail, so here's my question: Is there a better functional way to check if an image is fully transparent ?我到处搜索无济于事的答案,所以这是我的问题:是否有更好的功能方法来检查图像是否完全透明?
Any help will be greatly appreciated.任何帮助将不胜感激。
The blank image's DataBuffer
is indeed an instance of DataBufferInt
while your original image has a buffer of type DataBufferByte
.空白图像的DataBuffer
的确是一个实例DataBufferInt
,而你的原始图像有型的缓冲DataBufferByte
。 You should create your empty image based on the type of the image to compare with:您应该根据要与之比较的图像类型创建空图像:
private static BufferedImage getBlankImage(int width, int height, int type) {
return new BufferedImage(width, height, type);
}
and call it this way:并这样称呼它:
getBlankImage(b.getWidth(), b.getHeight(), b.getType())
Notice that in terms of performance and memory usage it may be better to create the empty image only once (or once for each image type which may occur).请注意,就性能和内存使用而言,最好只创建一次空图像(或为可能出现的每种图像类型创建一次)。 Probably the image type and size are constant and written wherever the actual images are created.可能图像类型和大小是恒定的,并在创建实际图像的任何地方写入。
Now you have a proper empty image and may test its equality as in Is there a simple way to compare BufferedImage instances?现在你有一个合适的空图像,可以测试它的相等性,如是否有一种简单的方法来比较 BufferedImage 实例? : :
public static boolean compareImages(BufferedImage imgA, BufferedImage imgB) {
int width = imgA.getWidth();
int height = imgA.getHeight();
// Loop over every pixel.
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Compare the pixels for equality.
if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) {
return false;
}
}
}
return true;
}
The method must be return a byte array, you are trying convert a DataBuffer in a DataBufferByte.该方法必须返回一个字节数组,您正在尝试将 DataBuffer 转换为 DataBufferByte。 I have changed the name getPixels to getByteArray.我已将名称 getPixels 更改为 getByteArray。 Since it is not the same.既然不一样。 Try this:尝试这个:
private static byte[] getByteArray(BufferedImage img) {
byte[] imageInByte = null;
String format = "jpeg"; //Needs a image TYPE
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, format, baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imageInByte;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.