Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to send Image over Socket in Java?

Status
Not open for further replies.

Joncamp

MIS
Oct 30, 2006
15
0
0
US
I am trying to transmit an Image object through a Socket connection in Java. The only thing I may be missing is how to get an image size in bytes so I know how many bytes are being transferred over the Socket.

I have the ServerSocket and Client working transferring data, and I have a cut-paste image working from a client Applet, and I have a Java program that saves Images to disk as JPEG/PNG.

How do you get the byte size of an Image object in Java?... or do you know of source code to a program that transferes images through Sockets?

Thanks,
Jon
 
Do you want to transfer the image or the object?

Cheers,
Dian
 
I'm trying to tranfer the Image it's self. I already have a routine to handle BufferedImage objects, writing them to a file. I was hoping I could use something similar with writing to a Socket InputStream and OutputStream to transfer the Image data.

Jon
 
Well, if you can them to a file and read them back in again using the InputStream and OutputStream interfaces, using sockets should just be a matter of replacing your use of FileInputStream and FileOutputStream with the Sockets' InputStream and OutputStream at respective ends of the link.

Tim
 
Well, I don't know much about the Image object, but AFAIK, and object needs to be serializable to be sent through a socket.

I guess Image will have a reference to the file containing the image so that won't be send. I'd follow Tim's approach and send the image and not the object.

Cheers,
Dian
 
Yeah, I looked at the Image class too when I read Jon's post and noticed it wasn't serializable. I don't know exactly which technique Jon is using to write his image to a file but if the image can be squirted into a file using a FileOutputStream, then it can be squirted down a socket via that socket's output stream as returned from the getOutputStream() method.

Tim
 
To save an Image to disk, it has to be transformed into a BufferedImage object, then you save the BufferedImage object to a file stream.

So using a BufferedImage one should be able to send the image through an IP Socket? I just don't know how?


Jon
 
Assuming you're using ImageIO to save the BufferedImage, then something like this (I haven't compiled this or tested it, I just don't have the time, sorry).
Code:
//..BufferedImage in bImage;
//..Socket in sSocket;

  OutputStream os = null;
  try {
    os = sSocket.getOutputStream();
    ImageIO.write(bImage, "jpg", os);
  } catch (IOException ex){
    ex.printStackTrace();
  } finally {
    if ( os != null ){
      try { os.close(); } catch (IOException ex){}
    }
  }
  
//Housekeep your socket. If conversation done, close it.

This should give you a hint on how to handle the InputStream at the other end, too.

Tim
 
Looks interesting TimW, I have actually gotten a working app from another forum, but I'll try yours because yours allows one to send the image through the Socket stream in any format, if it works?

Thanks much,
Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top