簡體   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