返回信息流public class Server extends JFrame{
//private MyCanvas canvas;
private ServerSocket ss;
private Socket socket=null;
private DataInputStream is;
private byte[]imageContent;
private FileOutputStream fos;
boolean repaint;
private static MyPanel panel;
private static Server f;
int i=0;
private static ConcurrentLinkedDeque<Image> queue;
public static void main(String[] args) throws Exception {
f = new Server();
panel = f.new MyPanel();
queue=new ConcurrentLinkedDeque<>();
f.add(panel);
f.setSize(500, 500);
f.setVisible(true);
f.startThread();
}
class MyPanel extends JPanel
{
int i=0;
@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);
panel.repaint();
//打印文件的相关信息
System.out.println(imgFile.getName());
System.out.println("sum "+sum);
//线程睡眠100ms,这样不会造成数据读的错误
Thread.sleep(100);
}
} catch (IOException e) {
System.out.println("__"+e.getMessage());
}
}
}
为什么会不现实呢,只显示第一幅图像,屏幕一直只显示第一幅,接下来的都不显示。求解答。。。
这是一条镜像帖。来源:北邮人论坛 / java / #29228同步于 2014/4/11
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
[问题]Android摄像头预览视频帧传输到pc上显示的问题
xiaohongjun
2014/4/11镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复