java通過控制鼠標(biāo)實(shí)現(xiàn)屏幕廣播的方法
本文實(shí)例講述了java通過控制鼠標(biāo)實(shí)現(xiàn)屏幕廣播的方法。分享給大家供大家參考。具體分析如下:
在前面一篇《java實(shí)現(xiàn)屏幕共享功能實(shí)例分析》中提到截取屏幕時(shí)是沒鼠標(biāo),為了看到教師端界面上的鼠標(biāo),可以在截取屏幕的時(shí)候,把鼠標(biāo)繪制到每一張截圖上去,但是由于截圖的時(shí)候是一張張截取的,所以看到的鼠標(biāo)難免會(huì)有點(diǎn)卡,之前寫了java鼠標(biāo)操控小程序,可以通過這種方式來看到鼠標(biāo)的演示。
實(shí)現(xiàn)的方式也挺簡單的,前面兩篇文章分別實(shí)現(xiàn)了鼠標(biāo)控制和不帶鼠標(biāo)的屏幕分享功能,把這兩個(gè)結(jié)合一下就ok了,下面簡單分析下。
服務(wù)端,將SendScreenImg和SendMouseMessage看作兩個(gè)工具類,分別監(jiān)聽不同的端口,他們兩個(gè)都實(shí)現(xiàn)了Thread類,我們用線程池ExecutorService類控制他們。
使用了兩個(gè)端口,因?yàn)闀簳r(shí)還不知道該如何吧鼠標(biāo)信息和圖片的信息一起發(fā)送,或許可以把圖片轉(zhuǎn)換成字節(jié)數(shù)組的形式,把鼠標(biāo)的坐標(biāo)放在數(shù)組前面,不過這樣的話鼠標(biāo)可能也會(huì)不連貫,因?yàn)閭魉褪髽?biāo)坐標(biāo)的速度會(huì)比傳圖片的要快一些,嗯,有空再試試。
客戶端類比上面就是了。
下面是代碼:
服務(wù)端:
主程序:
* 屏幕廣播類,調(diào)用了兩個(gè)工具類:發(fā)送截屏信息的類和發(fā)送鼠標(biāo)的信息類,利用了線程池。
*/
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());
}
}
發(fā)送截圖代碼:
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;
/*
* 工具類:發(fā)送教師端截屏信息給學(xué)生端,沒有鼠標(biāo)信息,使用了8002號(hào)端口
* 可以在發(fā)送的圖片上面組件繪制鼠標(biāo)的信息,從而實(shí)現(xiàn)在學(xué)生端界面上見到鼠標(biāo)信息,暫未實(shí)現(xiàn)該功能
*
*/
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(); //有必要先關(guān)閉當(dāng)前端口
this.serverPort = serverPort;
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(8000000);
}catch(Exception e){}
}
//構(gòu)造方法 開啟套接字連接 機(jī)器人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); //構(gòu)造相應(yīng)大小的矩形
}
@Override
public void run()
{
//實(shí)時(shí)等待接收截屏消息
while(true){
try {
socket = serverSocket.accept();
ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
zip.setLevel(9); //設(shè)置壓縮級(jí)別
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("學(xué)生端口已經(jīng)連接");
} catch (IOException ioe) {
System.out.println("被控端:disconnect");
}
} catch (IOException ioe) {
System.out.println("連接出錯(cuò)");
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
}
發(fā)送鼠標(biāo)信息:
* 工具類:獲取鼠標(biāo)的信息并且發(fā)送給學(xué)生端
*/
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, "已經(jīng)開始監(jiān)聽");
} catch (IOException e1) {
e1.printStackTrace();
}
}
//多線程 在無線的循環(huán)中監(jiān)聽客戶端的
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(); //刷行輸出流,并且使所有緩沖的輸出字節(jié)寫出
output.close(); //關(guān)閉輸出流且釋放資源
System.out.println("INFO: "+operateStr);
} catch (IOException e) {
System.out.println("已經(jīng)停止連接");
break; //斷開連接的時(shí)候就停止無線循環(huán)
}
}
}
}
客戶端:
主程序:
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
* 該類用于接收教師端的屏幕信息,不包括鼠標(biāo)
* 使用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("已經(jīng)連接"+(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); //按照字節(jié)讀取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);// 接收?qǐng)D片間隔時(shí)間
} 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("遠(yuǎn)程監(jiān)控--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();
//窗口關(guān)閉事件
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
flag=false;
BorderInit.this.dispose();
System.out.println("窗體關(guān)閉");
System.gc(); //垃圾回收
}
});
}
}
接收鼠標(biāo)信息并控制鼠標(biāo)移動(dòng):
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;
/*
* 學(xué)生端 控制鼠標(biāo)和教師端一致
* 該類 負(fù)責(zé)接收鼠標(biāo)的信息 并且用robot.mouseMove()函數(shù)控制鼠標(biāo)移動(dòng)
*/
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();
//獲取鼠標(biāo)移動(dòng)的信息
DataInputStream dataIn = new DataInputStream(socket.getInputStream());
String info="";
int r;
while((r=dataIn.read()) != -1){
info +=""+(char)r; //把字節(jié)數(shù)組中所有元素都變?yōu)樽址?br /> }
dataIn.close();
System.out.println("數(shù)據(jù)流斷開"+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("輸出鼠標(biāo)的信息"+x+" "+ y);
robot.mouseMove(x, y);
}
}
}
} catch (IOException e) {
System.out.println("已斷開連接");
break;
} catch (AWTException e) {
e.printStackTrace();
}
}
}
}
希望本文所述對(duì)大家的Java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring?MVC基于注解的使用之JSON數(shù)據(jù)處理的方法
這篇文章主要介紹了Spring?MVC基于注解的使用JSON數(shù)據(jù)處理,json是一種輕量級(jí)的數(shù)據(jù)交換格式,是一種理想的數(shù)據(jù)交互語言,它易于閱讀和編寫,同時(shí)也易于機(jī)器解析和生成,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05IDEA Error:java: 無效的源發(fā)行版: 17錯(cuò)誤
本文主要介紹了IDEA Error:java: 無效的源發(fā)行版: 17錯(cuò)誤,這個(gè)錯(cuò)誤是因?yàn)槟腎DEA編譯器不支持Java 17版本,您需要更新您的IDEA編譯器或者將您的Java版本降級(jí)到IDEA支持的版本,本文就來詳細(xì)的介紹一下2023-08-08Java?Lambda表達(dá)式常用的函數(shù)式接口
這篇文章主要介紹了Java?Lambda表達(dá)式常用的函數(shù)式接口,文章基于Java?Lambda表達(dá)式展開對(duì)常用的函數(shù)式接口的介紹,具有一的的參考價(jià)值需要的小伙伴可以參考一下2022-04-04Java正則表達(dá)式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
什么是正則表達(dá)式,正則表達(dá)式的作用是什么?這篇文章主要為大家詳細(xì)介紹了Java正則表達(dá)式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Java實(shí)現(xiàn)常見的排序算法代碼實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)常見的排序算法代碼實(shí)例,按照思路實(shí)現(xiàn)了以下幾個(gè)排序算法(冒泡排序、直接插入排序、直接選擇排序、快速排序),方便日后用到,特此記錄一下,需要的朋友可以參考下2023-11-11java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解及實(shí)例代碼
這篇文章主要介紹了java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02spring boot定時(shí)器實(shí)現(xiàn)定時(shí)同步數(shù)據(jù)的操作步驟
在Java中,@Scheduled注解是用于指定定時(shí)任務(wù)的執(zhí)行規(guī)則的,這篇文章給大家介紹spring boot定時(shí)器實(shí)現(xiàn)定時(shí)同步數(shù)據(jù)的操作步驟,感興趣的朋友一起看看吧2023-12-12