Java - 通过套接字发送指向 BufferedImage 的对象
我和一群朋友正在开发一个 Java 项目,我们需要一些有关通过套接字发送对象的帮助。
到目前为止,我们已经使用 ObjectOutputStream
和 ObjectInputStream
实现了通过套接字发送简单对象(整数、字符串等)。然而,我们今天遇到了一个巨大的问题(无论如何对我们来说都是巨大的^^)
我们有一个树结构,我们需要将其从一台PC发送到另一台PC。问题是,在该树的每个节点中,我们都有一个对 BufferedImage 的引用,并且它不可序列化。
今天研究了很多,发现可以使用ImageIO.write()
通过socket的OutputStream发送one BufferedImage,但是这样不行因为我们不需要单独发送 BufferedImage 本身,而是发送它所在的整个树。
我们需要的是一种方法(如果存在)来序列化每个 BufferedImage,必要时将其转换为另一个类,同时创建树,并使树的每个节点引用新的可序列化类,以便可以将树发送为整个对象...
我们真的不关心性能,因为我们发送的树没有那么大(顶部有 10-15 个节点)。预先感谢您的帮助,对于糟糕的英语表示歉意。哦,这是为了……好吧,一种家庭作业,以防你想记住这一点:-)
谢谢!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在每个节点上,您可以使用 writeObject() 和 readObject() 检查 http://java.lang. 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
essentially it will become
您可以在客户端将 BufferedImage 转换为字节数组,然后作为普通字节数组发送,然后在服务器端从该字节数组创建 BufferedImage。下面是将 BufferedImage 转换为字节数组的代码,反之亦然。
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.
如果要使用对象流,可以将缓冲图像包装在实现 Serialized 的类中,并且具有字节数组属性(图像数据为字节数组)。您必须修改代码,将 BufferedImage 引用更改为 SerializedImage(类名示例)。
如果您这样做,您的类将被序列化并传输。
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..
我对此进行了很多搜索,因为我需要通过套接字发送 bufferedImage 而不是文件,并发现您可以将 BufferedImage 转换为 ImageIcon,因此您可以使用 ObjectOutputStream 发送该对象,因为它是可序列化的,有其他方式但不适用于我的代码,希望这对某人有用。
i was searching out a lot about this, because i needed to send a bufferedImage and not a file over socket, and find out that you can convert a BufferedImage to ImageIcon, so you can send that object using ObjectOutputStream, because is serializable, there are others ways but not working in my code, hope this to be usefull for someone.
这是一个扩展 BufferedImage 的简单类,将其图像数据序列化为 PNG。
因此,在代码中的任何地方都类似于:
BufferedImage bi = xyz();
您可以转换为:
SerializedBufferedImage bi = new SerializedBufferedImage(xyz());
SerializedBufferedImage 仍然是 BufferedImage,因此您仍然可以像以前一样绘制它并与其交互。而且该构造函数不会重新创建新的栅格,因此据我所知,它不会对性能/内存产生影响。
Here is a simple class that extends
BufferedImage
that serializes its image data as a PNG.So everywhere in your code where that resembles:
BufferedImage bi = xyz();
You can convert to:
SerializableBufferedImage bi = new SerializableBufferedImage(xyz());
The SerializableBufferedImage is still a BufferedImage, so you can still paint it and interact with it exactly as before. And that constructor does not recreate new rasters, so to my knowledge it should not make an impact on performance/memory.