1

我写了我的远程电脑控制应用程序。服务器发送机器人类捕获的屏幕截图,其他线程监听客户端的输入(例如:鼠标单击)并在服务器 pc 上执行相同的操作。

一段时间后我得到堆溢出,可能应该用 reset() 清理 ObjectOutputStream,但首先要更快,我应该弄清楚如何压缩 Image 对象。据我阅读,ImageIO 应该对我有帮助,但我不想将其保存到文件或将其发送到流中,只想拥有一个转换后的 Image 对象。当然,我为什么不想用 ImageIO.write(blablalb, blablal, myOutputStream) 将它发送到我的 ObjectOutputStream 到监听客户端。问题是,我已经尝试过,并且遇到阅读或其他问题,没有异常或错误,客户端记录器说它有两张图片(未显示)然后卡住了。虽然我没有压缩的方式在这方面效果很好。

因此,我需要帮助如何在不保存或发送的情况下进行转换并将其保存在 Object 中(然后通过 ObjectOutputStream 发送),或者至少修复 ImageIO 使用问题。我更喜欢第一种方式(不想弄乱字节计数代码等等)。谢谢你的帮助。

这是一些代码,我有很多类和代码,但希望这会有所帮助: Image (wrapped into ImageIcon) object sent way

服务端图片发送相关片段

public class BroadcastWorker implements Runnable {
    private ObjectOutputStream output;
    //private BlockingQueue<Image> imagesBuffer;
    private boolean finnish = false;
    private long frequency = 50;
    private Screen screen = new Screen();

    public BroadcastWorker(Socket connection) throws IOException {
        output = new ObjectOutputStream(connection.getOutputStream());
        //ImageIO.createImageOutputStream(output);
        // outputui resetas atlaisvina vietos

    }

    /*
    public void addImage(Image image) {
        imagesBuffer.add(image);
    }
    * 
    */

    private void send(Image image) throws IOException {
        System.out.println("Sending image");
        output.writeObject(new ImageIcon(image));
        //ImageOutputStream imageOutput = ImageIO.createImageOutputStream(output);
        //ImageIO.write((RenderedImage)image, "jpeg", imageOutput);
        System.out.println("image sent");
    }

    @Override
    public void run() {
        while (!finnish) {
            try {
                Thread.sleep(frequency);
            } catch (InterruptedException ex) {
                Logger.getLogger(BroadcastWorker.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                send(screen.getScreenshot());
            } catch (IOException ex) {
                Logger.getLogger(BroadcastWorker.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}

ClientGui 类,部分代码与接收和设置图像相关

public class ClientGui1 extends javax.swing.JFrame {
    private Socket connection;
    private ScreenReceiveWorker receiver = new ScreenReceiveWorker();
    private ActionSendWorker actionSender = new ActionSendWorker();
    /**
    * 
    */
    class ScreenReceiveWorker extends SwingWorker {
        private Image progress;

        private void setScreen(Image progress) {
            this.progress = progress;
        }

        private Image getScreen() {
            return progress;
        }

        @Override
        protected Object doInBackground() throws Exception {
            ObjectInputStream input = new ObjectInputStream(connection.getInputStream());
            //ImageInputStream imageInput = ImageIO.createImageInputStream(input);

            while(true) {
                Thread.sleep(200);
                System.out.println("Screeeen");
                Image screen = ((ImageIcon) input.readObject()).getImage();
                //Image screen = ImageIO.read(imageInput);
                System.out.println("received");
                panelScreen.setImage(screen);
                panelScreen.repaint();

            }
        }
    }

......
.....
4

0 回答 0