我自定义了个类class Message implements Serializable {
Image im;
Message(Image im){
this.im=im;
}
}
也实现了Serializable 接口;但是这个类里面有个Image对象,不能序列化,怎么解决? 问题补充:
Image off_screen_buf;
if (off_screen_buf == null) {
off_screen_buf = this.createImage(1024, 768);
msg = new Message( off_screen_buf);
这样做不行的,具体点的!
out.writeObject(msg);的时候会抛异常:
java.io.NotSerializableException: sun.awt.image.OffScreenImage
(我只是想做一个网络绘画板,画板用的双缓冲,所以想直接把off_screen_buf传给其他人,但是在网络上传输的时候遇到了现在这个问题,off_screen_buf不能序列化,应该怎么解决,谢谢!)
Image im;
Message(Image im){
this.im=im;
}
}
也实现了Serializable 接口;但是这个类里面有个Image对象,不能序列化,怎么解决? 问题补充:
Image off_screen_buf;
if (off_screen_buf == null) {
off_screen_buf = this.createImage(1024, 768);
msg = new Message( off_screen_buf);
这样做不行的,具体点的!
out.writeObject(msg);的时候会抛异常:
java.io.NotSerializableException: sun.awt.image.OffScreenImage
(我只是想做一个网络绘画板,画板用的双缓冲,所以想直接把off_screen_buf传给其他人,但是在网络上传输的时候遇到了现在这个问题,off_screen_buf不能序列化,应该怎么解决,谢谢!)
解决方案 »
- 有问题请教大家,类似红警或者星际游戏那种的。
- 什么是java文档?帮助文档?
- 请教一个流操作中back read的问题
- 我想给JTextArea加一个滚动条,为什么不能显示??? 在retrieve()部分!
- java 连接oracle8i 数据库问题。急~~~~~~~~~~~~~~
- IO流的一个问题,关于汉字的输入和输出
- 我是一个新手
- 简单问题:JDOM读取xml文件时不识别encoding='GBK',有什么方法可以让它支持GBK?
- 很初级的问题,但是把我给难到了,前辈们帮忙啊!!!
- for循环下的内部类如何访问for语句的循环变量
- SWT编程问题 得分好贴
- 不定长分组并进行组内组合的疑难问题(java or C)
然后自己定义2个方法:
private void writeObject(ObjectOutputStream stream) throws IOException;
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException;
Image应该有获得byte数组的方法,活动具体数组,用基础数据在网络中传输。
1、是实现扩展序列化接口Externalizable,这样一来就不能使用默认方式序列化,只能在writeExternal()明确序列化那些需要的部分。
也就是说你需要自己写两个方法: readExternal 和 writeExternal。2、通过transient关键字,transient限定的属性的值将不会被串行化,这些属性会被设置成其类型的java默认值。反串行化并不是重新构造这个对象。
也就是说实现Serializable接口,不串行化的字段设为transient。
就在Message类中,添加两个方法 readObject 和 writeObject; 对Image进行特殊处理
方法一:我肯定都是要序列化的,但问题还是老问题IMAGE不能序列化。
方法二:对IMAGE怎么特殊处理?具体一点,谢谢!
......//其他处理
对Image的处理
int[] pixels = image != null? new int[w * h] : null;
if (image != null) {
try {
PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
pg.grabPixels();
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
throw new IOException("failed to load image contents");
}
}
catch (InterruptedException e) {
throw new IOException("image load interrupted");
}
}
s.writeObject(pixels);
}
我还是不知道怎么做。transient关键字加上好象就不序列化了,那反序列化的时候也就没有IMAGE对象了。怎么读得到呢?
实现扩展序列化接口Externalizable
自己写两个方法: readExternal 和 writeExternal
在这二个方法里特殊处理IMAGE对象
是这样吗?
......//其他处理
对Image的处理
int[] pixels = image != null? new int[w * h] : null;
if (image != null) {
try {
PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
pg.grabPixels();
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
throw new IOException("failed to load image contents");
}
}
catch (InterruptedException e) {
throw new IOException("image load interrupted");
}
}
s.writeObject(pixels); //这不就是Image的处理吗?
}
答:很简单啊.那有那么复杂的事啊.我是在本地抓屏后,将图像通过网络传给对方时写的程序中用的,方法是用:ImageIcon类啊.它可是实现了Serializable 接口的啊.
即:
方法如下:我自定义了个类class Message implements Serializable {
ImageIcon im;
Message(Image im){
this.im=new ImageIcon(im);
} 若要从ImageIcon对象imobj中取出Imaege,则:
image im=imobj.getImage();就行了.
你可以看一下 ImageIcon 的实现,其中的 Image对象就是定义为transient,参考一下它的实现,你就明白了。PS:14楼的使用系统的序列化可能代价是很大的,序列化是递归的。也许要序列化之前东西并不大,但是序列化之后就级数增长了。
Exception in thread "Thread-1" java.lang.OutOfMemoryError: Java heap space
msg = new Message(off_screen_buf);
pc.clientPaint.sendmsg(msg);
下面是clientPaint类里面的方法:
public void sendmsg(Message msg) {
try {
out.writeObject(msg);
} catch (IOException ex) {
System.out.print(ex.toString);
}
}
序列话并不是你想的那么简单,一个ImageIcon就是一个int数组......
它的Observer等其他属性呢,其他属性又会如何实现呢?看看源码,或者有空的话看看 Java Pitfalls 啊,也许你就会理解
后加上:out.flush();否则会:OutOfMemory的!
另一个就是当我把从网络上接收过来的IMAGE存到自己的画板缓冲即off_screen_buf后,当前用鼠标就不能画了。
msg = (Message) in.readObject();
ImageIcon cache = msg.getContent();
pc.mc.draw(cache.getImage());
另一类:
void draw(Image cache) {
off_screen_buf =cache;
Graphics g = getGraphics();
paint(g);
}
加上out.flush();依然会溢出
画得少不会,画得多的时候就会溢出了。
在鼠标松开的时候发送的 public void mouseReleased(MouseEvent e):
msg = new Message(off_screen_buf); //<---这个每一次都会内部开一个同样大小的数组,你即使自己对Image序列化(不通过ImageIcon),代码中每一次开一个同样大小的数组,同样内存溢出的.因此:看来最好的办法是:直接针这一个(仅此一个的)off_screen_buf,直接传送它的整个二进制数据
pc.clientPaint.sendmsg(msg);
ImageIcon怎么转成BUFFERIMAGE?上一问题:怎么直接传他的二进制呢?
ImageIcon怎么转成BufferedImage?
或者Image怎么转成BufferedImage?
Frame frame = null;//创建一个观察者
try{frame = new Frame();
frame.addNotify();
MediaTracker mt = new MediaTracker(frame);
mt.addImage(image,0);
mt.waitForAll();
int w = image.getWidth(frame);
int h = image.getHeight(frame);
BufferedImage buf = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics gc = buf.createGraphics();
gc.drawImage(image,0,0,frame);
return buf;
} catch (Exception e){
e.printStackTrace();
return null;
} finally{
if(frame != null){
frame.dispose();
}
}
}
反之:BufferedImage bi=(BufferedImage)ImageIcon的对象.getImage();