java通过控制鼠标实现屏幕广播的方法
程序员文章站
2024-03-01 10:37:46
本文实例讲述了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());
}
}
* 屏幕广播类,调用了两个工具类:发送截屏信息的类和发送鼠标的信息类,利用了线程池。
*/
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) {
}
}
}
}
}
}
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; //断开连接的时候就停止无线循环
}
}
}
}
* 工具类:获取鼠标的信息并且发送给学生端
*/
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));
}
}
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(); //垃圾回收
}
});
}
}
* 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();
}
}
}
}
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程序设计有所帮助。