我已经获得了8BitARGB颜色空间中的图像字节数组,需要将此字节数组转换为java.awt.BufferedImage。 代码如下:
public void getImage(byte byteArray[]){
int height = 1920;
int width = 1080;
ARGB_to_ABGR(byteArray);
BufferedImage image1 = new BufferedImage(height, width,
BufferedImage.TYPE_4BYTE_ABGR);
image1.getWritableTile(0, 0).setDataElements(0, 0, height, width, byteArray);
java.io.File file = new java.io.File("amazing.png");
try {
ImageIO.write(image1, "jpg", file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/*
* Swap the Red byte and Blue byte
*/
public void ARGB_to_ABGR(byte byteArray[]){
int length = byteArray.length;
byte r = 0;
byte b = 0;
for(int i = 0; i < byteArray.length; i++){
if(length % 4 == 0){
//do nothing
}else if(length % 4 == 1){
r = byteArray[i];
}else if(length % 4 == 2){
//do nothing
}else if(length % 4 == 3){
b = byteArray[i];
byteArray[i] = r;
byteArray[i - 2] = b;
}
}
}
我认为原始字节数组没有任何问题。为了快速调试,仅根据图像效果,谁能判断哪个字节(Alpha、Red、Green、Blue)是错误的?感谢您提前提供的帮助。
最佳答案
在 ARGB_to_ABGR
的代码中,您可能想在现在 length % 4
的位置写入 i % 4
。就目前而言,我猜它什么也没做。
关于java - 将颜色空间为 8BitARGB 的字节数组转换为 BufferedImage.TYPE_4BYTE_ABGR 时哪个字节(Alpha、Red、Green、Blue)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34305354/