我想将字节数组转换为Image文件。这是我的代码部分
BufferedImage image = user_space(getImage(image_path(path,name,"jpg")));
System.out.println("Image:"+image);
//Image : BufferedImage@12d263f: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@12a0f6c transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 466 height = 336 #numData Elements 3 dataOff[0] = 2
decode = decode_text(get_byte_data(image));
try {
System.out.println("length :"+decode.length);
//length :- 73
BufferedImage bImageFromConvert = ImageIO.read(new ByteArrayInputStream(decode));
System.out.println(bImageFromConvert);
ImageIO.write(bImageFromConvert, "jpg", new File(
"D:/eeraj/new-darksouls.jpg"));
}
private byte[] get_byte_data(BufferedImage image)
{
WritableRaster raster = image.getRaster();
DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();
return buffer.getData();
}
private byte[] decode_text(byte[] image)
{
int length = 0;
int offset = 24;
//loop through 32 bytes of data to determine text length
for(int i=24; i<32; ++i) //i=24 will also work, as only the 4th byte contains real data
{
length = (length << 1) | (image[i] & 1);
}
byte[] result = new byte[length];
//loop through each byte of text
for(int b=0; b<result.length; ++b )
{
//loop through each bit within a byte of text
for(int i=0; i<8; ++i, ++offset)
{
//assign bit: [(new byte value) << 1] OR [(text byte) AND 1]
result[b] = (byte)((result[b] << 1) | (image[offset] & 1));
}
}
return result;
}
我收到错误
java.lang.IllegalArgumentException: im == null!
at javax.imageio.ImageIO.write(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
ImageIO.read方法返回null。当我尝试编写图像时,这会导致异常。
我在google上搜索找到此问题的解决方案,但除了修改图像标题信息之外,我没有发现任何具体内容。
请告诉我,我该如何解决这个问题?