Java implementation of screen sharing function case analysis

  • 2020-04-01 03:39:12
  • OfStack

This article illustrates a Java approach to screen sharing. Share with you for your reference. Specific analysis is as follows:

Recently, I have been doing the course design of software software engineering, making a screen monitoring system for the laboratory, referring to various predecessors' codes, and then converting my own code after finally understanding, which is how beginners imitate.

When it comes to screen monitoring system, there are teacher end and student end. The teacher end is the Server end, and the student end is the Client end. A more interesting part of the system should be the screen broadcast and screen monitoring, the rest of what check in, lock screen, regular shutdown, is relatively simple.

Screen broadcast, in the function of the realization of the above, to put it bluntly, is the teacher side of the machine constantly intercept screen information, in the form of pictures sent to each student side of the computer, so that students can see the teacher's operation on the computer, this is the so-called screen broadcast.

The trouble with this is that when you capture a screen image, there is no mouse information. But there are two solutions:

When sending screenshot information, draw a mouse on the picture, so that there will be two mice in the student end, the student end can move their own computer mouse.

Send the teacher end of the mouse coordinates to the students end, the students end of the computer mouse according to the coordinate information real-time movement, here is involved in the control of the function, the students end can not move the mouse.

Screen monitoring is a bit trickier, but it's actually two things:
The teacher monitors the function of all the students' computer screens;
The teacher controls a student's computer;
Because it involves concurrency, each client has to send the screen information to the teacher in real time, which is a little troublesome, but it can be realized.

Here is a temporary implementation of the mouseless screen sharing function, relatively simple, to be improved, but can be used as a tool class in the later integration.

The first is the teacher-side Server:

package Test;
 
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;
 
/*
 * ly  2014-11-20
 * This class sends screenshots in real time to disappear, multi-threaded implementation, does not contain mouse information, and does not do each one right Client Do optimization
 */
public  class SendScreenImg extends Thread
{
    public static int SERVERPORT=8000;
    private ServerSocket serverSocket;
    private Robot robot;
    public  Dimension screen;
    public Rectangle rect ;
    private Socket socket;
    
    public static void main(String args[])
    {
        new SendScreenImg(SERVERPORT).start();
    }
    
    //Construction method & NBSP; Open socket connection & NBSP;         Robot robot    Gets screen size
    public SendScreenImg(int SERVERPORT)
    {
        try {
            serverSocket = new ServerSocket(SERVERPORT);
            serverSocket.setSoTimeout(864000000);
            robot = new Robot();
        } catch (Exception e) {
            e.printStackTrace();
        }
        screen = Toolkit.getDefaultToolkit().getScreenSize();  //Gets the size of the home screen
        rect = new Rectangle(screen);                          //Construct a rectangle of screen size
    }
    
    @Override
    public void run()
    {
        //Wait in real time to receive screenshot message
        while(true)
        {
            try{
                socket = serverSocket.accept();
                System.out.println(" The student port has been connected ");
                ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
                zip.setLevel(9);     //Set the compression level
                
                BufferedImage img = robot.createScreenCapture(rect);
                zip.putNextEntry(new ZipEntry("test.jpg"));
                ImageIO.write(img, "jpg", zip);
                if(zip!=null)zip.close();
                System.out.println("Client Connecting in real time ");
               
            } catch (IOException ioe) {
                System.out.println(" Connection is broken ");
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {e.printStackTrace();}
                }
            }
        }
    }
}

Then the student-side Client:

package Test;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
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;
/*
 * ly  2014-11-20
 * This class is used to receive screen information on the teacher side, not including the mouse, to be optimized
 */
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;
    }
    
    public void run() {
        while(frame.getFlag()){
            try {
                socket = new Socket(IP,8000);
                DataInputStream ImgInput = new DataInputStream(socket.getInputStream());
                ZipInputStream imgZip = new ZipInputStream(ImgInput);
                
                imgZip.getNextEntry();             //To the beginning of the Zip file stream
                Image img = ImageIO.read(imgZip);  //
reads the images in the Zip image stream by byte                 frame.jlbImg.setIcon(new ImageIcon(img));
                System.out.println(" Connect the first "+(System.currentTimeMillis()/1000)%24%60+" seconds ");
                frame.validate();
                TimeUnit.MILLISECONDS.sleep(50);//Time between receiving images
                imgZip.close();
                
            } catch (IOException | InterruptedException e) {
                System.out.println(" Connection is broken ");
            }finally{
                try {
                    socket.close();
                } catch (IOException e) {} 
            }      
        }  
    }
}
 
//Client side window helper class, dedicated to display the screen information received from the teacher side
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(" Remote monitoring --IP:"  + "-- The theme :" );
        this.setSize(400, 400);
        //this.setUndecorated(true);  // Full screen display, best commented out when testing
        //this.setAlwaysOnTop(true);  // The display window is always at the front
        this.add(jlbImg);
        this.setLocationRelativeTo(null);
        this.setExtendedState(Frame.MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
        this.validate();
    
        //Window closing event
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                flag=false;
                BorderInit.this.dispose();
                System.out.println(" Form closed ");
                System.gc();    //Garbage collection
            }
        });
    }
}

Here has never extracted such a small function in the finished product, far from the finished product has a lot to write, interested friends can improve on this basis.

I hope this article has been helpful to your Java programming.


Related articles: