Converting Image to Bytes and Vice-Versa
807569Jul 11 2006 — edited Jul 11 2006Hi 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!!!