程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java經由過程掌握鼠標完成屏幕播送的辦法

java經由過程掌握鼠標完成屏幕播送的辦法

編輯:關於JAVA

java經由過程掌握鼠標完成屏幕播送的辦法。本站提示廣大學習愛好者:(java經由過程掌握鼠標完成屏幕播送的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java經由過程掌握鼠標完成屏幕播送的辦法正文


本文實例講述了java經由過程掌握鼠標完成屏幕播送的辦法。分享給年夜家供年夜家參考。詳細剖析以下:

在後面一篇《java完成屏幕同享功效實例剖析》中提到截取屏幕時是沒鼠標,為了看到教員端界面上的鼠標,可以在截取屏幕的時刻,把鼠標繪制到每張截圖上去,然則因為截圖的時刻是一張張截取的,所以看到的鼠標不免會有點卡,之前寫了java鼠標操控小法式,可以經由過程這類方法來看到鼠標的演示。

完成的方法也挺簡略的,後面兩篇文章分離完成了鼠標掌握和不帶鼠標的屏幕分享功效,把這兩個聯合一下就ok了,上面簡略剖析下。

辦事端,將SendScreenImg和SendMouseMessage看做兩個對象類,分離監聽分歧的端口,他們兩個都完成了Thread類,我們用線程池ExecutorService類掌握他們。

應用了兩個端口,由於臨時還不曉得該若何吧鼠標信息和圖片的信息一路發送,也許可以把圖片轉換成字節數組的情勢,把鼠標的坐標放在數組後面,不外如許的話鼠標能夠也會不連接,由於傳送鼠標坐標的速度會比傳圖片的要快一些,嗯,有空再嘗嘗。

客戶端類比下面就是了。

上面是代碼:

辦事端:

主法式:
/*
 * 屏幕播送類,挪用了兩個對象類:發送截屏信息的類和發送鼠標的信息類,應用了線程池。
 */
public class BroderCast {
    public  static void main(String[] args)
    {
        new BroderCast();
        System.out.println("開端");
    }
    public BroderCast()
    {
        ExecutorService exector = Executors.newFixedThreadPool(2);
        exector.execute(new SendScreenImg());
        exector.execute(new SendMouseMessage());
    }
}
發送截圖代碼:
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
 
/*
 * 對象類:發送教員端截屏信息給先生端,沒有鼠標信息,應用了8002號端口
 *      可以在發送的圖片下面組件繪制鼠標的信息,從而完成在先生端界面上見到鼠標信息,暫未完成該功效
 *             
 */
public  class SendScreenImg extends Thread
{
    public int serverPort=8002;
    private ServerSocket serverSocket;
    private Robot robot;
    public  Dimension screen;
    public Rectangle rect ;
    private Socket socket;
    public static void main(String args[])
    {
        new SendScreenImg().start();
    }
    
    public void changeServerPort(int serverPort)
    {
        if(this.serverPort == serverPort) return ;
        try{
            this.serverSocket.close();    //有需要先封閉以後端口
            this.serverPort = serverPort;
            serverSocket = new ServerSocket(this.serverPort);
            serverSocket.setSoTimeout(8000000);
        }catch(Exception e){}
        
    }
    
    //結構辦法  開啟套接字銜接      機械人robot   獲得屏幕年夜小
    public SendScreenImg()
    {
        try {
            serverSocket = new ServerSocket(getServerPort());
            serverSocket.setSoTimeout(864000000);
            robot = new Robot();
        } catch (Exception e) {
            e.printStackTrace();
        }
        screen = Toolkit.getDefaultToolkit().getScreenSize();  //獲得主屏幕的年夜小
        rect = new Rectangle(screen);                          //結構響應年夜小的矩形
        
    }
    @Override
    public void run()
    {
        //及時期待吸收截屏新聞
        while(true){
            try {
                socket = serverSocket.accept();
                ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
                zip.setLevel(9);     //設置緊縮級別
                try{
                    BufferedImage img = robot.createScreenCapture(rect);
                    zip.putNextEntry(new ZipEntry("test.jpg"));
                    ImageIO.write(img, "jpg", zip);
                    if(zip!=null)zip.close();
                    System.out.println("先生端口曾經銜接");
                } catch (IOException ioe) {
                    System.out.println("被控端:disconnect");
                }
            } catch (IOException ioe) {
             System.out.println("銜接失足");
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
    }
}
發送鼠標信息:
/*
 * 對象類:獲得鼠標的信息而且發送給先生端
 */
public class SendMouseMessage extends Thread{
    private int OPERATE_PORT = 8001;
    private ServerSocket server;
    private Socket socket;
    private String operateStr;
    public static void main(String[] args)
    {
        new SendMouseMessage().start();
    }
    public SendMouseMessage(){
        try {
            server = new ServerSocket(OPERATE_PORT);
            //JOptionPane.showMessageDialog(null, "曾經開端監聽");
        } catch (IOException e1) {
            e1.printStackTrace();
        }      
    }
    //多線程  在無線的輪回中監聽客戶真個
    public void run()
    {
        while(true){
            Point point = MouseInfo.getPointerInfo().getLocation();  //
            operateStr ="Movemouse,"+point.x+","+point.y;
            try {
                socket = server.accept();
                socket.setSoTimeout(1000000);
                DataOutputStream output =new DataOutputStream(socket.getOutputStream());
                output.write(operateStr.getBytes());
                output.flush();   //刷行輸入流,而且使一切緩沖的輸入字節寫出
                output.close();   //封閉輸入流且釋放資本
    
                System.out.println("INFO:  "+operateStr);
            } catch (IOException e) {
                System.out.println("曾經停滯銜接");
                break;   //斷開銜接的時刻就停滯無線輪回
            }
        }
    }
}

客戶端:

主法式:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
import com.Tool.OperateMouse;
import com.Tool.ReceiveImages;
 
public class ReceiveBroderCast {
    public ExecutorService exector;
    public static String IP="202.216.60.9";
    
    public static void main(String[] args)
    {
        new ReceiveBroderCast(IP);
    }
    
    public ReceiveBroderCast(String IP) {
        exector = Executors.newFixedThreadPool(2);
        exector.execute(new ReceiveImages(IP));
        exector.execute(new OperateMouse(IP));
    }
}
吸收截圖代碼:
/*
 * ly  2014-11-20
 * 該類用於吸收教員真個屏幕信息,不包含鼠標
 * 應用socket()
 */
public  class ReceiveImages extends  Thread{
    public BorderInit frame ;
    public Socket socket;
    public String IP;
    
    public static void main(String[] args){
        new ReceiveImages("202.216.60.7").start();
    }
    public ReceiveImages(String IP)
    {
        frame=new BorderInit();
        this.IP=IP;
        
    }
    public void run() {
        while(frame.getFlag()){
            System.out.println("曾經銜接"+(System.currentTimeMillis()/1000)%24%60+"秒");
            try {
                socket = new Socket(IP,8002);
                DataInputStream ImgInput = new DataInputStream(socket.getInputStream());
                ZipInputStream imgZip = new ZipInputStream(ImgInput);
                Image img = null;
                try{
                    imgZip.getNextEntry();   //到Zip文件流的開端處
                    img = ImageIO.read(imgZip); //依照字節讀取Zip圖片流外面的圖片
                    frame.jlbImg.setIcon(new ImageIcon(img));
                    frame.validate();
                }catch (IOException e) {e.printStackTrace();}
                
                try{
                    imgZip.close();
                } catch (IOException e) {
                    System.out.println("銜接斷開");
                }
                try {
                    TimeUnit.MILLISECONDS.sleep(50);// 吸收圖片距離時光
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }      
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    socket.close();
                } catch (IOException e) {} 
            }      
        }  
    }
}
class BorderInit extends JFrame
{
    private static final long serialVersionUID = 1L;
    public JLabel jlbImg;
    private boolean flag;
    public boolean getFlag(){
        return this.flag;
    }
    public BorderInit()
    {
        this.flag=true;
        this.jlbImg = new JLabel();
        this.setTitle("長途監控--IP:"  + "--主題:" );
        this.setSize(400, 400);
        //this.setUndecorated(true);
        //this.setAlwaysOnTop(true);  //一直在最後面
        this.add(jlbImg);
        this.setLocationRelativeTo(null);
        this.setExtendedState(Frame.MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
        this.validate();
    
        //窗口封閉事宜
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                flag=false;
                BorderInit.this.dispose();
                System.out.println("窗體封閉");
                System.gc();    //渣滓收受接管
            }
        });
    }
}
吸收鼠標信息並掌握鼠標挪動:
import java.awt.AWTException;
import java.awt.Robot;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
 
import javax.swing.JOptionPane;
 
/*
 * 先生端   掌握鼠標和教員端分歧
 * 該類  擔任吸收鼠標的信息  而且用robot.mouseMove()函數掌握鼠標挪動 
 */
public class OperateMouse extends Thread{
    public static void main(String[] args)
    {
        new OperateMouse("202.116.60.7").start();
    }
    private Socket socket;
    public String IP;
    private int OPERATE_PORT = 8001;
    private Robot robot;
    
    public OperateMouse(String IP)
    {
        this.IP = IP;
    }
    public void run() {
        while(true){
            try {
                socket = new Socket(IP,OPERATE_PORT);
                robot = new Robot();
                //獲得鼠標挪動的信息
                DataInputStream dataIn = new DataInputStream(socket.getInputStream());       
                String info="";
                int r;
                while((r=dataIn.read()) != -1){
                    info +=""+(char)r;   //把字節數組中一切元素都變成字符型
                }
                dataIn.close();
                System.out.println("數據流斷開"+info);
                if(info!=null){
                        String s[] = info.trim().split(",");
                        if("Movemouse".equals(s[0].trim()));
                        {
                            if (s.length == 3) {
                                int x = Integer.parseInt(s[1].trim());
                                int y = Integer.parseInt(s[2].trim());
                                System.out.println("輸入鼠標的信息"+x+"  "+ y);
                                robot.mouseMove(x, y);
                            }
                        }
                    }
            } catch (IOException e) {
                System.out.println("已斷開銜接");
                break;
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    }
}

願望本文所述對年夜家的Java法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved