这段程序怎么改啊 改了好久

hongjun_sky 2014-04-11 03:00:29
public class Server extends JFrame{

//private MyCanvas canvas;

private ServerSocket ss;
private Socket socket=null;

private DataInputStream is;

private byte[]imageContent;
private FileOutputStream fos;

private static MyPanel canvas;
private static Server f;

private static ConcurrentLinkedDeque queue;
public static void main(String[] args) throws Exception {
f = new Server();
canvas = f.new MyPanel();
queue=new ConcurrentLinkedDeque();
f.add(canvas);
f.setSize(500, 500);
f.setVisible(true);
f.startThread();

}


class MyPanel extends JPanel
{

@Override
public void paint(Graphics g) {
super.paint(g);
g.clearRect(0, 0, getWidth(), getHeight());
BufferedImage image=(BufferedImage) queue.poll();
g.drawImage(image, 0, 0, null);
if(image!=null)
System.out.println("paint :"+image.toString());

}

}



public void startThread() throws Exception
{
try {
ss=new ServerSocket(9991);
socket=ss.accept();
if(socket!=null)
{
System.out.println(socket.getLocalAddress());
}
is=new DataInputStream(socket.getInputStream());
int hasRead=-1;
while(true)
{

int sum=0;
int len=is.readInt();
System.out.println("length:"+len);

imageContent=new byte[len];
while(sum<len)
{
hasRead=is.read(imageContent,sum,len-sum);
sum+=hasRead;
}
String fileName = "IMG_"
+ new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())
.toString() + ".jpg";
File imgFile = new File("D:\\"+fileName);
if(!imgFile.exists())
{
imgFile.createNewFile();

}
fos = new FileOutputStream(imgFile);
fos.write(imageContent);
BufferedImage image=ImageIO.read(imgFile);
queue.add(image);
canvas.repaint();
System.out.println(imgFile.getName());
System.out.println("sum "+sum);
//线程睡眠100ms,这样不会造成数据读的错误
Thread.sleep(100);

}

} catch (IOException e) {

System.out.println("__"+e.getMessage());
}
}

}



Android端不断传过来视频帧数据,PC端接收到之后显示,不断刷新接收到的图片,可是,只能显示一幅图片,其他的都显示不了。。。。怎么回事,好像同步的问题吧 望指点。
...全文
318 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuhouqingchen_2648 2014-05-27
  • 打赏
  • 举报
回复
mark一记。。。
代码间的舞者 2014-04-11
  • 打赏
  • 举报
回复
引用 6 楼 xiao0808305 的回复:
[quote=引用 3 楼 swjtueducn2 的回复:] 还有fos使用之前最好先flush()一下
你知道到底是为什么吗,谢谢了 [/quote] 没写完,所以堵塞了?
hongjun_sky 2014-04-11
  • 打赏
  • 举报
回复
引用 3 楼 swjtueducn2 的回复:
还有fos使用之前最好先flush()一下
你知道到底是为什么吗,谢谢了
hongjun_sky 2014-04-11
  • 打赏
  • 举报
回复
引用 3 楼 swjtueducn2 的回复:
还有fos使用之前最好先flush()一下
你好 感谢你的回答
swjtueducn2 2014-04-11
  • 打赏
  • 举报
回复
还有fos使用之前最好先flush()一下
swjtueducn2 2014-04-11
  • 打赏
  • 举报
回复
引用 1 楼 swjtueducn2 的回复:
粗略的看了一下,可能是因为你每次都是用同一个canvas,然后repaint()当然只能显示一个图片了。
忽略,迷糊ing
swjtueducn2 2014-04-11
  • 打赏
  • 举报
回复
粗略的看了一下,可能是因为你每次都是用同一个canvas,然后repaint()当然只能显示一个图片了。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧