程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java完成長途掌握技巧完全源代碼分享

Java完成長途掌握技巧完全源代碼分享

編輯:關於JAVA

Java完成長途掌握技巧完全源代碼分享。本站提示廣大學習愛好者:(Java完成長途掌握技巧完全源代碼分享)文章只能為提供參考,不一定能成為您想要的結果。以下是Java完成長途掌握技巧完全源代碼分享正文


Java完成長途掌握技巧

java自帶的java.net.和java.awt.robot. 的混雜可以用於完成經由過程收集對另外一台盤算機的長途掌握,個中包含掌握長途盤算機鼠標的舉措和鍵盤的輸出,和及時取得長途盤算機屏幕的圖象。本文將用簡練的說話和由淺入深的邏輯,教年夜家若何控制這個技巧。
起首先看一下後果圖:
長途端盤算機界面:

 

掌握端盤算機界面:

 

掌握端輸出:

 

長途端輸出:

一下開端具體引見長途掌握的技巧思緒。
起首兩台盤算機經由過程java.net的Socket來停止銜接。

一端先翻開一個ServerSocket,然後別的一端用socket停止銜接。

辦事器端

應當設置一個ServerSocket,而且初始化須要用到的輸出輸入流:

 public static void OpenServer() throws IOException, ClassNotFoundException{
 System.out.println("ServerStart.....");
 ServerSocket server = new ServerSocket(7777);
 socket = server.accept();
 System.out.println("銜接上...\n"+socket);
 OIS = new ObjectInputStream(socket.getInputStream());
 OOS=new ObjectOutputStream(socket.getOutputStream());
 }

客戶機端
應當用socket去銜接辦事器,而且初始化輸出輸入流:

public static void StartConnection(String IP,int port) throws UnknownHostException, IOException, AWTException{
 socket = new Socket("192.168.0.106",7777);
 if(socket.isConnected()){
  System.out.println("socket connected..."+socket);
 }
 OOS = new ObjectOutputStream(socket.getOutputStream());
 OIS = new ObjectInputStream(socket.getInputStream());


 }

如許兩台盤算機就鏈接在一路而且可以經由過程流(InputStream和OutputStream)來交流數據了

接上去年夜家可以想想,要完成長途掌握的兩台盤算機須要交流甚麼信息呢?起首被掌握端須要赓續向掌握端供給截取的屏幕圖象(這個我們將會用java.awt.robot來完成),然後鼠標和鍵盤依據掌握端傳來的事宜(inputEvent)來做出雷同的操作(用robot來完成)。然後掌握端固然起首要吸收被掌握端傳來的圖象而且反應到一個面板上(pane),然後監聽本機上鍵盤鼠標的舉措再傳給被掌握真個主機(我們經由過程在面板pane上設置一個監聽器listener來完成)

這裡碰到的一個成績就是用於傳送的圖片不管是用image照樣用bufferedImage都是弗成串行化的。所以不克不及用I/OStream停止傳送,所認為懂得決這個成績,我們須要把圖象數據封裝在一個類外面並implements Serializable接口
圖象類以下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;

public class Message implements Serializable {
 private static final long serialVersionUID = 1L;
 private String fileName;  // 文件稱號

 private long fileLength;  // 文件長度
 private byte[] fileContent;  // 文件內容

 public Message(){

 }
 public Message(String filePath) throws IOException{
  File file = new File(filePath);
  this.fileLength=file.length();
  this.fileName=file.getName();

  FileInputStream FIS = new FileInputStream(filePath);
  byte[] bytes = new byte[(int)fileLength];
  FIS.read(bytes,0,(int)fileLength);
  this.fileContent=bytes;

 }


 public String getFileName()
 { return fileName;}

 public void setFileName(String fileName)

 { this.fileName = fileName;}
 public long getFileLength()
 { return fileLength;
 }

 public void setFileLength(long fileLength)
 {this.fileLength = fileLength;}


 public byte[] getFileContent()
 {return fileContent;}
 public void setFileContent(byte[] fileContent)
 {this.fileContent = fileContent;}


}

如許便可以完成圖象經由過程ObjectInputStream和ObjectOutputStream的串行化流傳了

懂得了以上基本以後起首我們要完成掌握真個UI界面設置,圖片吸收,和鍵盤鼠標舉措監聽:

起首是設置吸收圖片:

public static void reveivePic() throws ClassNotFoundException, IOException{
 Message g = (Message)OIS.readObject();
 FileOutputStream FOS = new FileOutputStream("D:\\OUT\\"+g.getFileName());
 FOS.write(g.getFileContent(),0,(int)g.getFileLength());
 FOS.flush();

 FileInputStream FIS= new FileInputStream("D:\\OUT\\"+g.getFileName());
 BufferedImage BI = ImageIO.read(FIS);
 IIC=new ImageIcon(BI);

 Image img = IIC.getImage();
 Toolkit tk = Toolkit.getDefaultToolkit() ;
  Dimension d =tk.getScreenSize();

  int w = d.width;
  int h =d.height;
  BufferedImage bi = resize(img,800,600);


  imag_lab.setIcon(new ImageIcon(bi));
  imag_lab.repaint();//銷失落之前畫的配景
 }

 private static BufferedImage resize(Image img, int newW, int newH) {
  int w = img.getWidth(null);
  int h = img.getHeight(null);
  BufferedImage dimg = new BufferedImage(newW, newH,BufferedImage.TYPE_INT_BGR);
  Graphics2D g = dimg.createGraphics();
  g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
   RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
  g.dispose();
  return dimg;
 }

如許吸收了來自ObjectInputStream的Message類以後便可以把圖片從新設置到面板pane的年夜小然後展現出來

下一步就是設置面板屬性和監聽器:

public static void showUI(){
 //掌握台題目
  JFrame jf = new JFrame("掌握台");setListener(jf);
  //掌握台年夜小
  jf.setSize(500, 400);
  //imag_lab用於寄存畫面
  imag_lab = new JLabel();
  jf.add(imag_lab);
  //設置掌握台可見
  jf.setVisible(true);
  //掌握台置頂
  jf.setAlwaysOnTop(true);
  jf.setResizable(true);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


 }

監聽器:

 public static void setListener( JFrame frame){
  //panel設置監聽器
  frame.addKeyListener(new KeyAdapter(){
   public void keyPressed(KeyEvent e) {
    sendEventObject(e);
   }

   @Override
   public void keyReleased(KeyEvent e) {
    sendEventObject(e);
   }

   @Override
   public void keyTyped(KeyEvent e) {

   }
 });
  frame.addMouseWheelListener(new MouseWheelListener(){
   public void mouseWheelMoved(MouseWheelEvent e) {
    sendEventObject(e); 
   }
  });
  frame.addMouseMotionListener(new MouseMotionListener(){

   public void mouseDragged(MouseEvent e) {

    sendEventObject(e);
   }

   public void mouseMoved(MouseEvent e) {


    sendEventObject(e);

   }
  });
  frame.addMouseListener(new MouseListener(){
  public void mouseClicked(MouseEvent e) {
    sendEventObject(e);

   }
  public void mouseEntered(MouseEvent e) {

    sendEventObject(e);
   }
  public void mouseExited(MouseEvent e) {

    sendEventObject(e);
   }
  public void mousePressed(MouseEvent e) {

    sendEventObject(e);
   }
 public void mouseReleased(MouseEvent e) {

    sendEventObject(e);
   }

  });
 }

 private static void sendEventObject(InputEvent event){
  try{ System.out.println("send");
  OOS.writeObject(event);
  OOS.flush();

  }catch(Exception ef){
   ef.printStackTrace();
  }

 以上就完成了掌握端。

接上去我們將構建被掌握端:
被掌握端須要應用robot來截圖並發送,並且須要寫一個辦法來對吸收到的InputEvent停止反響
起首是截圖和發送:

 public static void CapturePic() throws AWTException, IOException{
 robot= new Robot();
 Message msg = null;
 Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
 java.awt.Dimension dm =tk.getScreenSize();
 java.awt.Robot robot = new java.awt.Robot();
  for (int i = 0; i < 50; i++) {
  //截取指定年夜小的屏幕區域
  Rectangle rec = new Rectangle(0, 0, (int) dm.getWidth(), (int) dm
   .getHeight());
  BufferedImage bimage = robot.createScreenCapture(rec);
  //將圖片保留到文件中
  String filePath = "D:\\OUT\\screenshot"+i+".jpeg";
  FileOutputStream fops =new FileOutputStream(filePath);
  javax.imageio.ImageIO.write(bimage, "jpeg", fops);
  fops.flush();
  fops.close();
  msg =new Message(filePath);

  System.out.println(msg.getFileName());
  System.out.println("send");
  OOS.writeObject(msg);
  OOS.flush();

  }
 }

留意到這段代碼中應用了D:\OUT\目次作為暫時文件的寄存處所,讀者應用這個代碼的時刻須要本身設置暫時文檔的寄存

然後完成robot關於吸收到的InputEvent指令停止操作:

 public void action() throws AWTException, ClassNotFoundException, IOException{
 Robot robot= new Robot();
 while(true){

 InputEvent e =(InputEvent)OIS.readObject();
  if(e!=null){
 handleEvents(robot,e);}
 }

 }

 public static void handleEvents(Robot action,InputEvent event){
 MouseEvent mevent = null ; //鼠標事宜
 MouseWheelEvent mwevent = null ;//鼠標轉動事宜
 KeyEvent kevent = null ; //鍵盤事宜
 int mousebuttonmask = -100; //鼠標按鍵

 switch (event.getID()){
 case MouseEvent.MOUSE_MOVED :   //鼠標挪動
  mevent = ( MouseEvent )event ;
  action.mouseMove( mevent.getX() , mevent.getY() );
  break ;
 case MouseEvent.MOUSE_PRESSED :   //鼠標鍵按下
  mevent = ( MouseEvent ) event;
  action.mouseMove( mevent.getX() , mevent.getY() );
  mousebuttonmask = getMouseClick(mevent.getButton() );
  if(mousebuttonmask != -100)
  action.mousePress(mousebuttonmask);
  break;
  case MouseEvent.MOUSE_RELEASED :  //鼠標鍵松開
  mevent = ( MouseEvent ) event;
  action.mouseMove( mevent.getX() , mevent.getY() );
  mousebuttonmask = getMouseClick( mevent.getButton() );//獲得鼠標按鍵
  if(mousebuttonmask != -100)
  action.mouseRelease( mousebuttonmask );
  break ;
 case MouseEvent.MOUSE_WHEEL :   //鼠標轉動
  mwevent = ( MouseWheelEvent ) event ;
  action.mouseWheel(mwevent.getWheelRotation());
  break ;
  case MouseEvent.MOUSE_DRAGGED :   //鼠標拖拽
  mevent = ( MouseEvent ) event ;
  action.mouseMove( mevent.getX(), mevent.getY() );
  break ;
  case KeyEvent.KEY_PRESSED :   //按鍵
  kevent = ( KeyEvent ) event;
  action.keyPress( kevent.getKeyCode() );
  break ;
  case KeyEvent.KEY_RELEASED :   //松鍵
  kevent= ( KeyEvent ) event ;
  action.keyRelease( kevent.getKeyCode() );
  break ;
 default: break ;


 }


 }

 private static int getMouseClick(int button) { //獲得鼠標按鍵
 if (button == MouseEvent.BUTTON1) //左鍵 ,中央鍵為BUTTON2
  return InputEvent.BUTTON1_MASK;
 if (button == MouseEvent.BUTTON3) //右鍵
  return InputEvent.BUTTON3_MASK;
 return -100;
 } 

 全部法式到這裡便可以停止了。下面的法式並沒有完成對機械人類線程的封裝。完全可以用的代碼可以在以下站內資本處下載我的資本:http://xiazai.jb51.net/201608/yuanma/Java-RomoteControl(jb51.net).rar

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved