【发布时间】:2015-12-16 17:44:39
【问题描述】:
我已经得到了 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)是错误的?提前感谢您的帮助。
【问题讨论】:
标签: java image bufferedimage