Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Converting Image to Bytes and Vice-Versa

807569Jul 11 2006 — edited Jul 11 2006
Hi All

I am actually trying to convert image to bytes,its happening. The code is below.

code to convert image to byte array
=========================


BufferedImage image = ImageIO.read(this.getClass().getResource("photo.jpg"));

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", bos);

buffer = bos.toByteArray();


===========================
I am reciving at other end as an OBJECT, Then i am converting it into a byte array, I want to convert it back to image i tried this code.But when i recive at other its not coming properly, The code is Below.


code to convert recived bytes back to image
===============================
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(photo);
byte[] photoByte = byteArrayOutputStream.toByteArray();

FileOutputStream fileOutputStream=new FileOutputStream("dany.jpg",true);
fileOutputStream.write(photoByte);



PLEASE LOOK INTO THIS AND HELP!!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 8 2006
Added on Jul 11 2006
6 comments
440 views