gpt4 book ai didi

c++ - gtk 图像突然不刷新,没有任何错误或警告

转载 作者:行者123 更新时间:2023-11-30 04:51:39 26 4
gpt4 key购买 nike

我开发了一个工具来从相机捕获图像并在窗口中显示捕获的图像。 GUI 窗口基于 GTK 2.4。开始时,工具运行正常,图像从摄像头捕获并实时显示在窗口上。过了一会儿,窗口上的图像突然不再刷新,但仍然是从相机中捕获的。没有错误或警告。有人遇到过这样的情况吗?谢谢。

Ubuntu 18.04,GTK 2.4

  // loop to call the following code to refresh the image on the window
pixbuf_ = Gdk::Pixbuf::create_from_data(
final_img_buf_.data, Gdk::COLORSPACE_RGB, false, 8, final_img_buf_.cols,
final_img_buf_.rows, static_cast<int>(final_img_buf_.step));
gtk_image_.set(pixbuf_);

编辑于2019-02-27感谢你的回复。我已将 GTK 升级到 GTK+ 3,但它仍然出现。

  // loop to call the following code to refresh the image on the window
pixbuf_ = Gdk::Pixbuf::create_from_data(
final_img_buf_.data, Gdk::COLORSPACE_RGB, false, 8, final_img_buf_.cols,
final_img_buf_.rows, static_cast<int>(final_img_buf_.step));
// ensure the image is normally updating
//std::this_thread::sleep_for (std::chrono::milliseconds(30));
gtk_image_.set(pixbuf_);
Glib::RefPtr<Gdk::Pixbuf> pixbuf = gtk_image_.get_pixbuf();
std::string filename = std::string("./debug/") + std::to_string(CurTimestampMicrosecond()) + std::string(".jpg");
pixbuf->save(filename, "jpeg");

运行一段时间后,窗口不再刷新图像,但图像仍然正确保存。

2019-02-28编辑

// The initialization code
gtk_main_.reset(new Gtk::Main(argc, argv));
ctrl_window_.reset(new CtrlWindow(screen, ctrl_rect)); // inherited from Gtk::Window
thread_ = std::thread([this]() {
Gtk::Main::run(*ctrl_window_);
}

最佳答案

看起来 g_timeout_add 和可能的 g_idle_add 都可能由于其他事件源的处理而延迟,因此在需要精确计时的上下文中不是最佳选择,例如绘制图像在帧更新之前到屏幕。我注意到在 2D 图形上下文中使用 g_timeout_add 会完全卡住应用程序,如果它不能以指定的 fps 加载帧。

gtk_widget_add_callback() 可能更适合您的需求,因为它会尽可能快地绘制缓冲区,而不会挂起,基本上是为您完成杂乱的同步。

关于c++ - gtk 图像突然不刷新,没有任何错误或警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54740284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com