【问题标题】:Update window every time image gets received每次收到图像时更新窗口
【发布时间】:2011-11-11 11:14:59
【问题描述】:

我通过套接字获取图像。现在,我实际上得到了它,但正如我预期的那样只有一次。

我检查了一些可以添加事件的类,例如 ImageReader,但它不会与我的 JFrame 通信。当我完成我的框架时,它似乎根本没有更新。

服务器:

public static void main(String[] args) {
    try {
        DefaultWindow window = new DefaultWindow();
        window.frame.setVisible(true);

        ServerSocket ss = new ServerSocket(4305);
        Socket cs = ss.accept(); // only 1 client is assumed

        // this bit should be in some sort of complete event
        BufferedImage bi = ImageIO.read(cs.getInputStream());

        window.image.setIcon(new ImageIcon(bi));
        window.frame.validate();
        window.frame.repaint();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

我该怎么办?最佳做法是什么?


编辑 11-11-11 16:19

我已按照 mKorbel 的回答,它似乎工作得更好,但我仍然收到错误。

我的客户端脚本:

public static void main(String[] args) {
    try {
        Socket cs = new Socket(InetAddress.getByName("localhost"), 4305);
        OutputStream os = cs.getOutputStream();

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        BufferedImage[] screenshots = new BufferedImage[gs.length];

        DisplayMode mode;
        Rectangle bounds;

        while(true) {
            try {
                for(int i = 0; i < gs.length; i++)
                {
                    mode = gs[i].getDisplayMode();
                    bounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());
                    screenshots[i] = new Robot(gs[i]).createScreenCapture(bounds);
                }

                if(screenshots[0] != null) {
                    ImageIO.write(screenshots[0], "png", os);
                }
                os.flush();

                Thread.sleep(500);
            } catch (AWTException e) {
            } catch (InterruptedException e) {}
        }
    } catch(IOException e) {}
}

我在这一行收到了 IndexOutOfBoundsException:ImageIO.write(screenshots[0], "png", os); 我将输出写入输出流。

似乎 while 循环只运行了 3 次。为什么只有3次?为什么会出现 IndexOutOfBoundsException?


编辑 11-11-11 16:38

我似乎已经对其进行了排序,但是我的标签不会更新。有什么想法吗?

服务器:

public static void main(String[] args) {
    try {
        final DefaultWindow window = new DefaultWindow();
        window.frame.setVisible(true);

        ServerSocket ss = new ServerSocket(4305);
        final Socket cs = ss.accept();

        SwingWorker<ImageIcon, Void> sw = new SwingWorker<ImageIcon, Void>() {
            BufferedImage bi = ImageIO.read(cs.getInputStream());

            BufferedImage bis;
            Graphics2D graphics2D;
            ImageIcon icon = null;

            @Override
            protected ImageIcon doInBackground() throws Exception {
                bis = new BufferedImage(window.image.getWidth(), window.image.getHeight(), BufferedImage.TYPE_INT_ARGB);

                graphics2D = bis.createGraphics();
                graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                graphics2D.drawImage(bi, 0, 0, window.image.getWidth(), window.image.getHeight(), null);
                graphics2D.dispose();

                if(icon != null) {
                    icon.getImage().flush();
                }

                icon = new ImageIcon(bis);
                return icon;
            }

            protected void done() {
                window.image.setIcon(icon);
                window.frame.validate();
                window.frame.repaint();
            }
        };

        sw.execute();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • 这是客户端还是服务器?
  • 这是服务器。注意ServerSocketInputStream 对象。反正我会加的
  • 我看到一个服务器在端口 4035 上接受连接,但我没有看到正在写入的图像。您是否在bi 中获得了有效图像? window 是建立在event dispatch thread 之上的吗? sscce 可能会有所帮助。
  • @trashgod 我在标签中显示图像(请参阅window.image.setIcon(icon)),因此无需在任何地方写任何东西。是的,图像是有效的,因为它确实显示了(第一张)图像(阅读问题并编辑)。最后不,窗口不是建立在事件调度线程上的。
  • DefaultWindow 未显示。 Swing GUI 对象应event dispatch thread 上构建和操作。

标签: java swing sockets bufferedimage concurrency


【解决方案1】:

因为你对Concurency in Swing 有一些问题,所以把它放到后台任务中

  • SwingWorker,类似于教程中的示例,在这种情况下,GUI 仍然可以访问,并且不等待任务结束

  • Runnable#Thread,但到 GUI 的输出必须包装到 invokeLater(),在这种情况下,GUI 仍然可以访问并且不等待任务结束

  • 简单地将其包装到invokeLater(),但在这种情况下,无法访问 GUI 冻结并等待任务结束,但这种方式是错误的,请不要这样做

将您的BufferedImage 设为IconJLabel

【讨论】:

  • 感谢您的回答,但不幸的是,我看不出这对我有什么帮助。对不起
  • 我认为这是我的图标缓存。有什么解决方案吗?
  • +1 这肯定需要一个单独的线程来阅读; SwingWorker很方便。
  • 好的,我尝试了你所说的,到目前为止它有效,但现在我在ImageIO.write() 上收到 IndexOutOfBoundsException,虽然我不确定为什么......请参阅我的更新帖子
猜你喜欢
  • 2013-11-06
  • 2023-03-22
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多