Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Serialized Images

Started by vidjogamer Jan 30, 2009 at 11:03 AM 1 replies 2.9k views
Original Post
vidjogamer
vidjogamer
Im trying to send an image over a socket using ObjectOutputStream and ObjectInputStream to receive it. I can not seem to get this to work for the life of me. Am I doing something terribly wrong? If anyone could provide me with some sample source of how to do this, that would be wonderful.
__dev
__dev
Quote:
Original post by vidjogamer
Im trying to send an image over a socket using ObjectOutputStream and ObjectInputStream to receive it. I can not seem to get this to work for the life of me. Am I doing something terribly wrong? If anyone could provide me with some sample source of how to do this, that would be wonderful.


Here is good tutorial:

http://java.sun.com/developer/technicalArticles/ALT/sockets/
leif_dragon
leif_dragon
I had to serialize images in order to encode their data storage for a game project I am working on. This is the method I used...



.. This was the class that was serialized.

import java.io.IOException;import java.io.Serializable;import java.awt.*;import java.awt.image.BufferedImage;import javax.imageio.ImageIO;import javax.imageio.stream.ImageInputStream;import javax.imageio.stream.ImageOutputStream; public class ImageCell implements Serializable{   private BufferedImage image;	    public BufferedImage getImage(){return image;}   public void setImage(BufferedImage image){this.image = image;}   public void writeImageObject(java.io.ObjectOutputStream out) throws IOException   {      ImageIO.write(image, "png", ImageIO.createImageOutputStream(out));   }	    public void readImageObject(java.io.ObjectInputStream in)throws IOException, ClassNotFoundException   {      while(image == null){image = ImageIO.read(ImageIO.createImageInputStream(in));}   }}


... and another class used this method to save an array of those serializable imageCell objects to file.

public static void saveFile(String fileName){	try	{		// Write to disk with FileOutputStream		FileOutputStream fOut = new FileOutputStream(fileName);					// Write object with ObjectOutputStream		ObjectOutputStream objOut = new ObjectOutputStream(fOut);				for(int x = 0; x < count; x++)		{			imageCell[x].writeImageObject(objOut);		}				System.out.println("File Saved!");	}	catch (FileNotFoundException e)	{		System.out.println("Error : Cannot save file.");		e.printStackTrace();	}	catch (IOException e)	{				System.out.println("Error : Cannot write file.");		e.printStackTrace();	}}


Hope this helps :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.