您的位置:首页 > 编程语言 > Java开发

JAVA实现远程录屏或广播屏幕

2015-06-25 19:01 417 查看
最近网络编程课到了结尾,老师让写一个大作业,运用的就是JAVA的网络支持写一个东西,有写网络聊天室,斗地主什么的,我就写了一个录屏昨天通过了验收,废话不多讲,直接上代码:

客户端:接收服务端发送的信息;

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package homework;

import java.awt.Frame;

import java.awt.Image;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.*;

import java.net.InetAddress;

import java.net.InetSocketAddress;

import java.net.Socket;

import java.net.SocketAddress;

import java.util.concurrent.TimeUnit;

import java.util.zip.ZipInputStream;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

/*

* @KnuthChao 20150625

* 用于接收服务器端发送的信息,没有写多线程和鼠标的录制,后期有空 *会加上去;

*/

public class ReceiveImages extends Thread{

public BorderInit frame ;

public Socket socket;

public String IP;

public static void main(String[] args){
new ReceiveImages(new BorderInit(),"127.0.0.1").start();
}
//接收图片
public ReceiveImages(BorderInit frame,String IP)
{
this.frame = frame;
this.IP=IP;
}
//实现run方法
public void run() {
while(frame.getFlag()){
try {
socket = new Socket(IP,12122);
DataInputStream ImgInput = new DataInputStream(socket.getInputStream());
ZipInputStream imgZip = new ZipInputStream(ImgInput);

imgZip.getNextEntry(); //到Zip文件流的开始处
Image img = ImageIO.read(imgZip);  //按照字节读取Zip图片流里面的图片
frame.jlbImg.setIcon(new ImageIcon(img));
System.out.println("连接第"+(System.currentTimeMillis()/1000)%24%60+"秒");
frame.validate();
TimeUnit.MILLISECONDS.sleep(1);// 接收图片间隔时间
imgZip.close();

} catch (IOException | InterruptedException e) {
System.out.println("连接断开");
}finally{
try {
socket.close();
} catch (IOException e) {}
}
}
}


}

//Client端窗口辅助类,专门用来显示从教师端收到的屏幕信息

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:"+"127.0.0.1");
this.setSize(1366, 768);
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();
}
});
}


}

服务端:发送数据

类名:SendScreen

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package homework;

import javax.swing.DefaultListModel;

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.z
4000
ip.ZipEntry;

import java.util.zip.ZipOutputStream;

import java.net.*;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

/**

*

* @author Knuth

*/

public class SendScreen extends javax.swing.JFrame {

public static int SERVERPORT=12122;
private ServerSocket serverSocket;
private Robot robot;
public  Dimension screen;
public Rectangle rect ;
private Socket socket;
DefaultListModel dlmsend = new DefaultListModel();

public SendScreen(int SERVERPORT) {
initComponents();
jList1.setModel(dlmsend);
try {
serverSocket = new ServerSocket(SERVERPORT);
//设置超时时间
serverSocket.setSoTimeout(864000);
robot = new Robot();
} catch (Exception e) {
e.printStackTrace();
}

screen = Toolkit.getDefaultToolkit().getScreenSize();  //获取主屏幕的大小

rect = new Rectangle(screen);                          //构造屏幕大小的矩形

}
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jScrollPane1.setViewportView(jList1);

jButton1.setText("开始广播");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("结束广播");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(24, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(40, 40, 40))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(62, 62, 62)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(61, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
dlmsend.addElement("教师端开始广播!");

Thread t = new  reThread();
t.start();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

try {
// TODO add your handling code here:
socket.close();
dlmsend.addElement("广播结束");
Thread t = new  reThread();
t.stop();
} catch (IOException ex) {
Logger.getLogger(SendScreen.class.getName()).log(Level.SEVERE, null, ex);
}
}

class reThread extends Thread {

public void run(){

while(true)
{
try{
socket = serverSocket.accept();
dlmsend.addElement("学生端口已经连接");
ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
zip.setLevel(0);     //设置压缩级别,java共8个还是11个压缩级别?

BufferedImage img = robot.createScreenCapture(rect);
zip.putNextEntry(new ZipEntry("test.jpg"));
ImageIO.write(img,"jpg",zip);
if(zip!=null)zip.close();
dlmsend.addElement("Client正在实时连接");
dlmsend.addElement("连接第"+(System.currentTimeMillis()/1000)%24%60+"秒");
} catch (IOException ioe) {
dlmsend.addElement("连接断开");

} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {e.printStackTrace();}
}
}
}
}
}

public static void main(String args[]) {

try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SendScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SendScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SendScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SendScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SendScreen(SERVERPORT).setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration


}

第一次在这里添加代码不太熟悉,以上就是这个功能的实现,用的NetBeans画的窗体,窗体的图片就不再上传了,画的简单程度谁用了谁知道,就是这样,拿走的记得说一下(●’◡’●);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: