本文介绍了将数据类型TYPE_4BYTE_ABGR的字节数组转换为BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类型为TYPE_4BYTE_ABGR的字节数组,我知道它的宽度和高度,我想将其更改为BufferedImage,有什么想法吗?
I have a byte array with type TYPE_4BYTE_ABGR, and I know its width and height, I want to change it to BufferedImage, any ideas?
推荐答案
效率可能不高,但是 BufferedImage
可以通过以下方式转换为另一种类型:
Might not be very efficient, but a BufferedImage
can be converted to another type this way:
public static BufferedImage convertToType(BufferedImage image, int type) {
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), type);
Graphics2D graphics = newImage.createGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
return newImage;
}
关于要实现的方法,您必须知道宽度或图像的高度,将 byte []
转换为 BufferedImage
。
About the method you want to be implemented, you would have to know the width or height of the image to convert a byte[]
to a BufferedImage
.
编辑:
一种方法是将 byte []
转换为 int []
(数据类型 TYPE_INT_ARGB
)并使用 setRGB
:
One way is converting the byte[]
to int[]
(data type TYPE_INT_ARGB
) and using setRGB
:
int[] dst = new int[width * height];
for (int i = 0, j = 0; i < dst.length; i++) {
int a = src[j++] & 0xff;
int b = src[j++] & 0xff;
int g = src[j++] & 0xff;
int r = src[j++] & 0xff;
dst[i] = (a << 24) | (r << 16) | (g << 8) | b;
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, width, height, dst, 0, width);
这篇关于将数据类型TYPE_4BYTE_ABGR的字节数组转换为BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!