繁体   English   中英

使用Java进行屏幕共享

[英]screen sharing using java

我发现此屏幕使用Java共享代码。 这是接收屏幕的客户端。

客户端 :

    class ReceiveScreen extends Thread{
    private ObjectInputStream cObjectInputStream = null;
    private JPanel cPanel = null;
    private boolean continueLoop = true;
    InputStream oin = null;
    Image image1 = null;

    public ReceiveScreen(InputStream in,JPanel p){
        oin = in;
        cPanel = p;
        start();
    }

    public void run(){
        try{
            //Read screenshots of the client and then draw them
            while(continueLoop){
                byte[] bytes = new byte[1024*1024];
                int count = 0;
                do{
                    count+=oin.read(bytes,count,bytes.length-count);
                }while(!(count>4 && bytes[count-2]==(byte)-1 && bytes[count-1]==(byte)-39));

                image1 = ImageIO.read(new ByteArrayInputStream(bytes));
                image1 = image1.getScaledInstance(cPanel.getWidth(),cPanel.getHeight(),Image.SCALE_FAST);

                //Draw the received screenshots

                Graphics graphics = cPanel.getGraphics();
                graphics.drawImage(image1, 0, 0, cPanel.getWidth(), cPanel.getHeight(), cPanel);
            }

        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}

有人能解释一下条件时这是做什么的吗?

while(!(count> 4 && bytes [count-2] ==((byte)-1)&& bytes [count-1] ==((byte)-39)));

查看服务器端

  1. 它从套接字读取字节,直到至少有4个字节为止。

  2. 然后,它检查最后两个字节是否为幻数,表示图像的结尾。

  3. 然后,它从原始字节创建图像对象。

  4. 然后将图像对象绘制到屏幕上。

(并且它会不断重复此操作,直到continuteloop设置为false。

您应该学习DeMorgan的THEROM。 它允许条件被重写

while(!(count>4 && bytes[count-2]==(byte)-1 && bytes[count-1]==(byte)-39));

是相同的

while ( count < 4 || bytes[count-2] != (byte)-1 || bytes[count-1] != (byte)-39 );

这使情况更加清晰。

  • 必须读取四个字节
  • 倒数第二个字节必须为0xFF
  • 最后一个字节必须为0xD9

如果您查看JPEG图像规范格式,则会看到0xFFD9是“ JPEG标记”,表示“图像流的末尾”

因此,此循环有效地从套接字读取JPEG图像并将其显示,直到continuteloop标志设置为false为止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM