Java - 通过Socket发送指向BufferedImage的对象 [英] Java - Sending an object that points to a BufferedImage through a Socket

查看:466
本文介绍了Java - 通过Socket发送指向BufferedImage的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一群朋友正在研究Java项目,我们需要一些关于通过套接字发送对象的帮助。

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

到目前为止,我们已经实现了使用 ObjectOutputStream ObjectInputStream 通过套接字发送简单对象(整数,字符串和诸如此类)。然而,我们今天遇到了一个巨大的问题(对我们来说很大,无论如何^^)

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 ^^)

我们有一个树形结构,我们需要从一台PC发送到另一台PC。问题是,在该树的每个节点内,我们都有一个BufferedImage的引用,而且它不可序列化。

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.

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

我们需要的是一种方法(如果存在的话)序列化每个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...

我们真的不关心性能,因为我们的树'重新发送不是那么大(10-15个节点顶部)。在此先感谢您的帮助,抱歉糟糕的英语。哦,这是一个...好吧,一种功课,如果你想记住这一点: - )

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 :-)

谢谢!!

推荐答案

在每个节点上可以使用writeObject()和readObject()检查 http://java.sun.com/developer/technicalArticles/Programming/serialization/ 了解更多信息

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

基本上它将成为

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

这篇关于Java - 通过Socket发送指向BufferedImage的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆