由于您imageByte的来源是未知的,它是很难说什么地方出了错。但是,如果你正在创建byteSource,则可能是下面的代码会帮助你,因为从Javadoc文档ImageIO.read()
返回一个BufferedImage,作为使用 一个ImageReader解码所提供File的结果从当前登记的 中自动选择。该文件包装在ImageInputStream中。如果没有 注册的ImageReader声称能够读取结果 流,则返回null。
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* Created by ankur on 13/7/15.
* The Following program will read an image file.
* convert it into byte array, and then reuse the
* converted byte array, and convert it back to new BufferedImage
*
*/
public class ImageToBuf {
public static void main(String... strings) throws IOException {
byte[] imageInByte;
//read the image
BufferedImage originalImage = ImageIO.read(new File("/home/ankur/Pictures/BlpRb.png"));
//convert BufferedImage to byte array
ByteArrayOutputStream byteOutS = new ByteArrayOutputStream();
ImageIO.write(originalImage, "png", byteOutS);
byteOutS.flush();
imageInByte = byteOutS.toByteArray();
byteOutS.close();
//convert byte array back to BufferedImage
InputStream readedImage = new ByteArrayInputStream(imageInByte);
BufferedImage bfImage = ImageIO.read(readedImage);
System.out.println(bfImage);
}
}
输出(在我amchine):
[email protected]: type = 13 IndexColorModel: #pixelBits = 8 numComponents = 3 color space = [email protected] transparency = 1 transIndex = -1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 4959 height = 3505 #numDataElements 1 dataOff[0] = 0
的Javadoc说:*如果没有注册的ImageReader声称能够读取得到的流,则返回null *所以,你的字节。数组不包含Java可以解码的图像。这些字节从哪里来? –