简体   繁体   English

Java - 通过Socket发送指向BufferedImage的对象

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

Me and a group of friends are working on a project in Java, and we need some help regarding sending objects through sockets. 我和一群朋友正在研究Java项目,我们需要一些关于通过套接字发送对象的帮助。

So far, we have achieved to send simple objects (ints, strings and whatnot) through sockets, using ObjectOutputStream and ObjectInputStream . 到目前为止,我们已经实现了使用ObjectOutputStreamObjectInputStream通过套接字发送简单对象(整数,字符串和诸如此类)。 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. 我们有一个树形结构,我们需要从一台PC发送到另一台PC。 The problem is that, within each node of that tree, we have a reference to a BufferedImage and it's not serializable. 问题是,在该树的每个节点内,我们都有对BufferedImage的引用,并且它不可序列化。

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. 我们今天已经研究了很多,我们发现我们可以使用ImageIO.write()通过套接字的OutputStream发送一个 BufferedImage,但是,这对我们没有好处,因为我们不需要自己发送BufferedImage,但整个树都在它的位置。

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... 我们需要的是一种方法(如果它存在)序列化每个BufferedImage,在必要时将它转换为另一个类,同时创建树,并让树的每个节点引用新的可序列化类,因此树可以作为整个对象......

We really don't care about performance, since the trees we're sending aren't that big (10-15 nodes top). 我们真的不关心性能,因为我们发送的树不是那么大(顶部10-15个节点)。 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!! 谢谢!!

on each node you can use writeObject() and readObject() check http://java.sun.com/developer/technicalArticles/Programming/serialization/ for more info 在每个节点上你可以使用writeObject()和readObject()检查http://java.sun.com/developer/technicalArticles/Programming/serialization/获取更多信息

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
    }
}

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. 您可以在客户端将BufferedImage转换为字节数组,然后将其作为普通字节数组发送,并在服务器端从该字节数组创建BufferedImage。 Below is the code to convert BufferedImage to byte array and vice versa. 下面是将BufferedImage转换为字节数组的代码,反之亦然。

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());
    }
}
}

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). 如果要使用Objects Streams,可以将缓冲的图像包装在实现Serializable的类中,并且具有一个字节数组的属性(图像数据作为字节数组)。 You'll have to modify your code, changing BufferedImage references to SerializableImage(class name example).. 您将不得不修改您的代码,将BufferedImage引用更改为SerializableImage(类名示例)。

If you do that, your class will be serialized and transferred.. 如果你这样做,你的班级将被序列化并转移..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM