⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 自己写的
💻 JAVA
字号:

import java.net.ServerSocket;
import java.net.Socket;
import java.io.ObjectOutputStream;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Robot;
import java.awt.Dimension;

public class Server implements Runnable{
	
	private static Socket client;
	private static ObjectOutputStream output;
	private int[][]  oldPix;
	private int[] index;
	private BufferedImage image;
	private Robot r;
	
	public static void main(String[] args){
		try{
			ServerSocket server = new ServerSocket(1986);
			System.out.println("服务器已经启动");
			while(true){
				//停滞,等待
				client = server.accept();
				System.out.println("监测到新连接");
				new Thread(new Server()).start();
				//new Thread(new InputProcessor(client)).start();
			}
		}
		catch(Exception e){
			main(args);
		}
	}

	public int[][] getimage(Rectangle rec,int w,int h){
		image = r.createScreenCapture(rec);
		int[][] str = new int[w][h];
		for(int i=0;i<1024;i++)
			for(int j=0;j<768;j++)
				str[i][j] = image.getRGB(i,j);
		return str;
	}
	//为了保证传输速度
	public int[] compare(int[][] newPix,int[][] oldPix){
		// the first time
		int startx = Integer.MAX_VALUE;
		int starty = Integer.MAX_VALUE;
		int endx = 0;
		int endy = 0;
		if(oldPix == null){
			int leng = 1024*768+4;
			int[] str = new int[leng];
			str[0] = 0;
			str[1] = 0;
			str[2] = 1023;
			str[3] = 767;
			int count=4;
			for(int y=0;y<768;y++)
				for(int x=0;x<1024;x++){
					str[count]=newPix[x][y];
					count++;
				}
			return str;
		}
		else{
			for(int x=0;x<1024;x++){
				for(int y=0;y<768;y++){
					if(newPix[x][y] != oldPix[x][y]){
						if(y<starty)
							starty = y;
						if(y>endy)
							endy = y;
						if(x<startx)
							startx = x;
						if(x>endx)
							endx = x;
					}
				}
			}
			int leng = (endx-startx+1)*(endy-starty+1)+4;
			int[] str = new int[leng];
			str[0] = startx;
			str[1] = starty;
			str[2] = endx;
			str[3] = endy;
			if((endx<=startx)||(endy<=starty))
				return null;
			int count=4;
			for(int y=starty;y<endy+1;y++)
				for(int x=startx;x<endx+1;x++){
					str[count]=newPix[x][y];
					count++;
				}
			return str;
		}
	}

	public void run(){
		try{
			output = new ObjectOutputStream(client.getOutputStream());
			r = new Robot();
			Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
			int w = (int) d.getWidth();
			int h = (int) d.getHeight();
			Rectangle rec = new Rectangle(0, 0, w,h);
			while(true){
				int[][] newPix = this.getimage(rec,w,h);
				index = this.compare(newPix, oldPix);
				oldPix = newPix;
				output.writeObject(index);
				output.flush();
				output.reset();
				for(int i=0;i<10;i++)
					System.gc();
				Thread.sleep(500);
			}
		}
		catch(Exception e){
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -