Hi, I want to save a BufferedImage to hard disk, as well as transmission across a network. Since it isn't serializable I want to shove it into a byte array using the ByteArrayOutputStream, and read it back as a ByteArrayInputStream. I tried simply using ImageIcon instead, but the file-size is very large when I serialize it.
Anyways, I was stuck and found some sample code a nice guy called Christen posted called "WrapBufferedImage". I wrote a test harness around it and tested it but can't get it to work correctly. I now believe I understand it, and could write it from scratch myself, but I cannot see the flaw in it so feel it would be a waste of time until I can get this to work.
The test harness loads a picture into a JFrame, and saves this image as a byte array from a BufferedImage. Then you click the LoadBack button and it is supposed load the saved file back into a BufferedImage and display it. Unfortunately it only displays about 5% of the image, and the rest looks corrupt. I did a System.out.print of the byte array immediately after it was created and everything seems fine, but when I do the same as soon as it's loaded back, 95% of the array are zeroes.
So I can presume that either the array isn't saving correctly (though as I say, its being created fine), or the data is on the hard disk ok, it just isn't loading it back fully.
I'll post the code, the main class here is the WrapBufferedImage, whereas the test harness is MyFrame. If anyone could just paste this into their editor and have a quick look it'd be great. This has been frying my head for a week now.
I wouldn't close your stream in your writeObject() method. I'd close it after you call it...
That, however, won't solve your problem. Make sure it's actually filling up your byte array when you read in the data from your file. The read method returns an integer indicating how many bytes it actually read. That should be equal to your length, right?
James Carman, President<br />Carman Consulting, Inc.
where the length should be 86409, but the return value from read(..) is 1020. This would definately seem to be the problem. Do you think its a problem in the save section? But I don't see how it would be reading only 1020 bytes if I'm specifically supplying 86409?, but at the same time, I can't see any problem in the save section either?