Java - Sending an object that points to a BufferedImage through a Socket

独自空忆成欢 提交于 2019-12-18 09:54:07

问题


Me and a group of friends are working on a project in Java, and we need some help regarding sending objects through sockets.

So far, we have achieved to send simple objects (ints, strings and whatnot) through sockets, using ObjectOutputStream and ObjectInputStream. However, we ran into a huge problem today (huge for us, anyway ^^)

We have a tree structure, that we need to send from one PC to another. The problem is that, within each node of that tree, we have a reference to a BufferedImage and it's not serializable.

We have been researching a lot today, and we found out that we can use ImageIO.write() to send one BufferedImage through the socket's OutputStream, however, it's no good to us since we don't need to send the BufferedImage by itself, but the whole tree were it is located.

What we need is a way (if it exists) to serialize each BufferedImage, converting it to another class if necessary, while making the tree, and having each node of the tree reference that new serializable class instead, so the tree can be sent as a whole object...

We really don't care about performance, since the trees we're sending aren't that big (10-15 nodes top). Thanks in advance for the help, sorry for the lousy English. Oh, and this is for a... well, a kind of homework, in case you want to keep that in mind :-)

Thanks!!


回答1:


on each node you can use writeObject() and readObject() check http://java.sun.com/developer/technicalArticles/Programming/serialization/ for more info

essentially it will become

public Node implements Serializable{

    transient BufferedImage buff;//transient make it so it won't be written with defaultWriteObject (which would error)

    private void writeObject(ObjectOutputStream out)throws IOException{
        out.defaultWriteObject();
        //write buff with imageIO to out
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
        in.defaultReadObject();
        //read buff with imageIO from in
    }
}



回答2:


You can convert BufferedImage to byte array on client side and send then as normal byte array and on the server side create BufferedImage from that byte array. Below is the code to convert BufferedImage to byte array and vice versa.

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;

public class ImageTest {

public static void main(String[] args) {

    try {

        byte[] imageInByte;
        BufferedImage originalImage = ImageIO.read(new File(
                "c:/darksouls.jpg"));

        // convert BufferedImage to byte array
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(originalImage, "jpg", baos);
        baos.flush();
        imageInByte = baos.toByteArray();
        baos.close();

        // convert byte array back to BufferedImage
        InputStream in = new ByteArrayInputStream(imageInByte);
        BufferedImage bImageFromConvert = ImageIO.read(in);

        ImageIO.write(bImageFromConvert, "jpg", new File(
                "c:/new-darksouls.jpg"));

    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}
}



回答3:


If you want to use Objects Streams, you can wrap buffered image inside a class that implements Serializable and has a attribute that is a array of bytes (image data as byte array). You'll have to modify your code, changing BufferedImage references to SerializableImage(class name example)..

If you do that, your class will be serialized and transferred..



来源:https://stackoverflow.com/questions/8031481/java-sending-an-object-that-points-to-a-bufferedimage-through-a-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!