원격 데스크탑을 구현하는 Java 인스턴스 코드
제어 측면은 마우스 이벤트를 서버쪽으로 전달합니다.
서버에서 마우스 이벤트를 수신 한 후 클라이언트로 전송합니다.
클라이언트가 마우스 이벤트를 받으면 로봇 클래스를 통해 완료 할 수 있으며 스크린 샷을 사용하여 이미지를 서버로 보내고 서버가 제어 터미널로 보냅니다.
내가 단순화 한 후
// 먼저 로봇 클래스 import java.awt.awtexception; import java.awt.robot; import java.awt.event.inputevent.inputevent.inputevent.inputevent.inputevent.inputevent.inputevent.inputevent.inpute.event.event.inputevent.inputevent.inputeception의 간단한 사용을 소개합니다./** * 사용 로봇 * @author dumb * */public class robottest {public static void main (argss) awtecture {Robot r = 새로운 Robot (); R.MousEmove (300, 500); // 마우스 이동 R.MousePress (inputevent.button1_mask); // 마우스 r.mouserElease (inputevent.button1_mask); // 마우스 릴리스 r.keypress ((int) 'a'); // makeboard press (int) 'a'는 a를 키보드의 해당 키로 변환하는 것을 의미합니다. // makeboard release}} // 화면 크롤링 크롤링 import java.awt.awtexception; import java.awt.rectangle; import java.awt.robot; import java.awt.image.bufferedimage; import javax.swing.imageicon; import javax.swing.jframe; jlabel.jlabel; javax.swing. javax.swing.windowConstants;/*** 크롤링 로컬 데스크탑 이미지* @Author Dumb*/public class screentest {public static void main (string [] args)은 Awtexception, InterruptedException {Robot = new Robot (); jframe jframe = new jframe (); jframe.setsize (1200, 700); Jlabel 레이블 = New Jlabel (); jframe.add (레이블); // 4 개의 매개 변수 XY 너비 높이 jframe.setVisible (true); jframe.setDefaultCloseOperation (WindowConstants.exit_on_close); // Dead Loop Dynamic Intercept while (true) {BufferedImage image = robot.createscreencapture (New Rectangle (0,0,1366,768)); // Seave Screen Label.seticon (new imageicon (image)); Thread.sleep (50); }}} // 리모컨의 원리에 대한 설명
// 서버 종료 및 클라이언트 종료로 나누었습니다.
// 원래 서버 측은 전달로만 사용되지만 전달을 데모로 작성하지는 않습니다.
// 즉, 클라이언트 측은 서버 측 E를 제어합니다.
/**
* 여기서 사용하는 것은 클라이언트 측에서 마우스 이벤트를 보내는 것입니다. 즉, 서버 측에서 전송 된 화면을 수신 한 후 제어 측면
* 그런 다음 로봇을 사용하여 처리하십시오
* 전송 방법은 소켓+IO로 처리 할 수 있습니다
* 화면 캡처 및 이미지 압축 로봇의 화면 캡처 기능과 JDK 자체 이미지 인코더를 채택하여 바이트 배열로 변환합니다.
* 서버로 전송 한 후 로봇은 IO+소켓을 통해 객체 객체를 직접 가져 와서 inputevent로 캐스팅 할 수 있습니다 (KeyEvent와 Mouseevent는 모두 상속됩니다).
* 이벤트 유형을 판단하면 별도로 처리 할 수 있습니다. 여기서 서버에는 두 개의 스레드가 필요하며, 하나는 클라이언트에게 스크리닝하고 보내는 것이고, 다른 스레드는 클라이언트의 말을 듣는 것입니다.
* 통과 된 이벤트
*/
// 다음은 특정 구현 코드입니다
// 서버 메인 프로세스 import java.awt.awxception; import java.awt.event; import java.awt.robot; import java.awt.event.inputevent; import java.awt.event.keyevent; import java.awt.event.mouseevent; import java.io.data ehoutception; java.io.objectinputStream; import java.net.serversocket; import java.net.socket;/*** server* @author dumb*/public class server {public static void main (string [] args)은 ioexception {serversocket server = new serversocket (80); System.out.println ( "서버는 정상적으로 시작되었습니다"); Socket Socket = Server.accrect (); // 요청 수신을 기다리고 메소드 System.out.println을 차단합니다 ( "클라이언트 연결이 있습니다"); dataOutputStream dos = 새로운 dataOutputStream (socket.getOutputStream ()); // 클라이언트 및 서버 측면에서 링크 된 출력 스트림 전송 ImageThread ImageThread = New ImageThread (DOS); 새 스레드 (imageThread) .start (); 새 스레드 (new Eventthread (new ObjectInputStream (socket.getInputStream ())). start (); }}/*** 수신 된 마우스 이벤트 또는 키보드 이벤트를 처리하는 데 사용*/클래스 이벤트 스레드 구현 실행 가능 {private objectInputStream OIS; 개인 로봇 로봇; 공개 이벤트 스레드 (ObjectInputStream OIS) {this.ois = OIS; } @override public void run () {try {robot = new robot (); while (true) {inputevent event = (inputevent) ois.readobject (); // 클라이언트가 전달한 객체 객체가 객체 ActionEvent (event); // handle ement}} catch (awtexception e) {e.printstacktrace (); } catch (classNotFoundException e) {e.printstacktrace (); } catch (ioexception e) {e.printstacktrace (); } 마침내 {try {ois.close (); } catch (ioexception e) {e.printstacktrace (); }}} / *** 이벤트 처리, 이벤트 유형을 판단하고 로봇 클래스* @param 이벤트* / public void actionEvent (inputevent event) {system.out.println (이벤트); if (keyEvent의 이벤트 인스턴스) {keyEvent e = (keyEvent) 이벤트; int type = e.getId (); // 이벤트 유형을 가져옵니다 if (type == event.key_press) {robot.keypress (e.getKeyCode ()); } else if (type == event.key_release) {robot.keyRelease (e.getKeyCode ()); }} else if (mouseevent의 이벤트 인스턴스) {mouseevent e = (mouseevent) 이벤트; int type = e.getId (); if (type == event.mouse_move) {robot.mouseMove (e.getx (), e.gety ()); } else if (type == event.mouse_down) {robot.mousepress (getMouseKey (type)); } else if (type == event.mouse_up) {robot.mouserElease (getMouseKey (type)); } else if (type == event.mouse_drag) {robot.mouseMove (e.getx ()); // 마우스 드래그}}}/*** 마우스의 실제 이벤트를 반환합니다. 마우스 시간은 직접 처리 할 수 없으며 * @return */public int getmousekey (int button) {if (button == mouseevent.button1) {// 왼쪽 마우스 버튼 inputevent.button1_mask; } else if (button == mouseevent.button2) {// 오른쪽 마우스 버튼 inputevent.button2_mask를 반환합니다. } else if (button == mouseevent.button3) {// scroller return inputevent.button3_mask; } else {return 0; }}} // 스크린 캡처 및 발신자, 여기서 소켓의 아웃 스트림 가져 오기 java.awt.awtexception; import java.awt.dimension; import java.awt.rectangle; import java.awt.robot; import java.awt.toolkit; import java.awt.image.buperedimage; java.io.bytearRayoutputStream; import java.io.dataOutputStream; import java.io.ioException; import com.sun.image.codec.jpeg.*;/*** @author dumb*/public class imageEthread runbable {dataOutputStream dos = null; // 데이터 출력 스트림 public ImageThread (dataOutputStream dos) {this.dos = dos; } @override public void run () {try {robot robot = new Robot (); // 전체 화면 dimension dimension = toolkit.getDefaultToolKit (). getScreensize (); /* int width = (int) dimensional.getWidth (); int height = (int) dimensional.getWidth (); 사각형 Rec = 새로운 사각형 (0,0, 너비, 높이); */ 사각형 Rec = 새로운 사각형 (치수); BufferedImage 이미지; 바이트 ImageBytes []; while (true) {image = robot.createscreencapture (rec); ImageBytes = getImageBytes (이미지); dos.writeint (imageBytes.length); dos.write (ImageBytes); dos.flush (); Thread.sleep (50); // 스레드 수면}} catch (awtexception e) {e.printstacktrace (); } catch (ImageFormateXception e) {e.printstacktrace (); } catch (ioexception e) {e.printstacktrace (); } catch (InterruptedException e) {e.printstacktrace (); } 마침내 {try {if (dos! = null) dos.close (); } catch (ioexception e) {e.printstacktrace (); }}} / *** 압축 이미지* @param 사진 압축 해야하는 @param 사진* @return compressed byte array* @throws ioexception* @throws imageformatexception* / public image (bufferedimage image) imageformatexception, ioexception (bytearrayout). // 컴프레서를 압축하고 먼저 바이트 출력 스트림에 저장된 jpegimageEncoder jpegd = jpegcodec.createjpegencoder (baos); // compress iamge jpegd.encode (image); // 바이트 배열로 변환 return baos.tobytearRay (); }}------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- java.io.objectoutputStream; import java.net.socket; import java.net.unknownhostexception; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.windowConstants;/*** 클라이언트* @Author Dumb**/public class client {public static void main (String args [])는 미지의 호스트 덱스크, ioexception {소켓 s = 새 소켓 ( "127.0.0.1", 80); datainputStream dis = new DatainputStream (s.getInputStream ()); ObjectOutputStream OOS = 새로운 ObjectOutputStream (s.getOutputStream ()); ClientWindow CW = New ClientWindow (OOS); 바이트 [] imageBytes; while (true) {imageBytes = new Byte [dis.readint ()]; // 먼저 전달 된 배열 길이를 dis.Readly (ImageBytes) 가져옵니다. // BYTE CW.RepainImage (ImageBytes)의 모든 데이터를 저장합니다. }}}/*** 클라이언트 양식* @Author Dumb*/클라이언트 Window는 jframe {private objectOutputStream OOS; 개인 jlabel 레이블; // 배경 이미지를 다시 쓰기 공개 void repainImage (byte [] imageBytes) {label.seticon (new ImageIcon (imageBytes)); this.repaint (); } public clientWindow (ObjectOutputStream oos) {this.oos = oos; this.settitle ( "원격 제어 프로그램"); label = new Jlabel (); JPANEL P = NEW JPANEL (); p.add (레이블); jscrollpane scroll = new jscrollpane (p); // p 패널에 스크롤 바를 추가 this.add (scroll); this.setsize (1024,768); this.SetDefaultCloseOperation (WindowConstants.exit_on_close); this.set -Visible (true); this.addkeylistener (new keylistener () {@override public void keytyped (keyevent e) {// todo auto-auto-keenrated method stub} @override public void keyreleased (keyevent e) {sendevent (e);} @override public void keypressed (sendevent (e)); label.addmouselistener (new mouselistener () {@override public void mouserEleled (mouseevent e) {sendevent (e);} @override public void mousepressed (mouseevent e) {sendevent (e);} @override public void mouseclicked (mouseevent e) @e) @ MouseEntered (Mouseevent e) {// todo 자동 생성 메소드 Stub} @override public void mouseexited (mouseevent e) {// todo 자동 생성 메소드 스터브}); } public void sendEvent (inputevent event) {try {oos.writeobject (이벤트); } catch (ioexception e) {e.printstacktrace (); }}}원격 데스크탑의 Java 구현을위한 위의 예제 코드는 내가 공유 한 모든 컨텐츠입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.