1

我已经可以发送并加载到 JPanel 中,并且在同一台计算机上时一切正常。当它在另一台计算机(LAN)上时,问题就出现了,屏幕捕获速度很慢并且每秒左右发送数据(imageIcon)......发送它真的需要很多时间。“鼠标/键盘”控件几乎实时地在本地主机/局域网上运行良好。唯一的问题是屏幕截图:/谁能帮我提高FPS?

该文件是服务器处理程序,它管理将图像屏幕作为对象发送...

Socket Client = null;
Robot Robot = null;
Rectangle Rectangle = null;
boolean continueLoop = true;
String nom_pc;

public ServerHandler(Socket client, Robot robot, Rectangle rect,String n_pc) {
    this.Client = client;
    this.Robot = robot;
    Rectangle = rect;
    nom_pc = n_pc;
    start();
}

@Override
public void run() {
    ObjectOutputStream oos = null;

    try {

        oos = new ObjectOutputStream(Client.getOutputStream());

        oos.writeObject(Rectangle);
    } catch (IOException ex) {
    }

    while (continueLoop) {
        BufferedImage image = Robot.createScreenCapture(Rectangle);
        ImageIcon imageIcon = new ImageIcon(image);  
        try {

            oos.writeObject(imageIcon);

            oos.flush();
            oos.reset();
            System.out.println("Enviado Screen...");

        } catch (IOException ex) {

            try {
                System.out.println("Se ha perdido conexion con el servidor remoto: "
                        + Client.getInetAddress() + ":" + Client.getLocalPort());
                continueLoop = false;
                Client.close();                    
                variables_saves.PCCN_ARRAYLIST.remove(Client);
                variables_saves.setNombre_pc(nom_pc);
                if (variables_saves.getNombre_pc().equals("ConflictoIP".toLowerCase())) {
                    variables_saves.PCCNNAME_ARRAYLIST.remove("Dirección IP Duplicada:" + Client.getInetAddress().getHostAddress());
                } else if (variables_saves.getNombre_pc().equals("NoIP".toLowerCase())) {
                    variables_saves.PCCNNAME_ARRAYLIST.remove("Desconocido");
                } else if (variables_saves.getNombre_pc().equals("Anonymous")) {
                    variables_saves.PCCNNAME_ARRAYLIST.remove("Anonymous");
                } else {
                    variables_saves.PCCNNAME_ARRAYLIST.remove(variables_saves.getNombre_pc());
                }
                Home.Connected_pc_list.setListData(variables_saves.PCCNNAME_ARRAYLIST.toArray());
            } catch (Exception e) {
            }

        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }

}

这是客户端,它是一个“APPLET”,它实际上接收屏幕并绘制到 JPanel

import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

 class ScreenReciever extends Thread {
    private ObjectInputStream cObjectInputStream = null;
     private JPanel cPanel = null;

    private boolean continueLoop = true;

    public ScreenReciever(ObjectInputStream ois,JPanel p){
        cObjectInputStream = ois;
        cPanel = p;


    }
        public void run(){

            try {

                //Read screenshots of the client then draw them
                while(continueLoop){

                //Recieve client screenshot and resize it to the current panel size
                    ImageIcon imageIcon = (ImageIcon) cObjectInputStream.readObject();      

                    System.out.println("New image recieved");
                    Image image = imageIcon.getImage();
                    image = image.getScaledInstance(cPanel.getWidth(),cPanel.getHeight()
                                                        ,Image.SCALE_FAST);
                    //Draw the recieved screenshot
                    Graphics graphics = cPanel.getGraphics();

                    graphics.drawImage(image, 0, 0, cPanel.getWidth(),cPanel.getHeight(),cPanel);

                }
            } catch (IOException ex) {
                System.out.println("Connection with computer LOST");
                try {
                    Thread.sleep(3000);
                    System.exit(0);
                } catch (InterruptedException ex1) {
                    Logger.getLogger(ScreenReciever.class.getName()).log(Level.SEVERE, null, ex1);
                }
          } catch(ClassNotFoundException ex){
             // ex.printStackTrace();
          }
     }

}

请帮我改进代码,因为我不知道该做什么了,我必须在下周完成这个项目......

一件事......我之前确实使用过这种方法并且我没有问题,现在唯一的区别是服务器(是一个程序)和客户端是一个加载到浏览器中的Applet......

4

0 回答 0