使用 C++ 代码在 Ubuntu 上显示图像的简单方法
A simple way to display images on Ubuntu using c++ code
我已经在 Ubuntu-server 16.04 上安装了 xorg, openbox, fbdev and libgtk-3-dev
,并尝试使用这段代码(我在其他问题中找到它)来显示图像:
#include <gtk/gtk.h>
void destroy(void) {
gtk_main_quit();
}
int main (int argc, char** argv) {
GtkWidget* window;
GtkWidget* image;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
image = gtk_image_new_from_file(argv[1]);
g_signal_connect(G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);
gtk_container_add(GTK_CONTAINER (window), image);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
并且可以通过这个命令编译成功:
gcc -Wall imageTest.c -o img `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`
但是当我尝试 img 1.png
时,结果是:
(process:1338): Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.
Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
(img:1338): Gtk-WARNING **: cannot open display:
您的示例代码是 GTK 2 示例,而不是 GTK 3。像 GTK_OBJECT
或 gtk_signal_connect
这样的东西很久以前就从 GTK 移到了 GLib。
您有 GTK 3 application examples in the GTK 3 documentation, or on the platform-demos。
至于 argv[1]
,请查找有关 argc
和 argv
的 C 教程,它们是您将命令行参数传递给程序的方式(不,那不是linux-具体)。
我将一个 USB 键盘连接到我的板上并尝试 运行 我的程序直接在它的终端中运行并且它没有问题并在我的 SPI LCD 中显示了我的图片。
我已经在 Ubuntu-server 16.04 上安装了 xorg, openbox, fbdev and libgtk-3-dev
,并尝试使用这段代码(我在其他问题中找到它)来显示图像:
#include <gtk/gtk.h>
void destroy(void) {
gtk_main_quit();
}
int main (int argc, char** argv) {
GtkWidget* window;
GtkWidget* image;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
image = gtk_image_new_from_file(argv[1]);
g_signal_connect(G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);
gtk_container_add(GTK_CONTAINER (window), image);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
并且可以通过这个命令编译成功:
gcc -Wall imageTest.c -o img `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`
但是当我尝试 img 1.png
时,结果是:
(process:1338): Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.
Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
(img:1338): Gtk-WARNING **: cannot open display:
您的示例代码是 GTK 2 示例,而不是 GTK 3。像 GTK_OBJECT
或 gtk_signal_connect
这样的东西很久以前就从 GTK 移到了 GLib。
您有 GTK 3 application examples in the GTK 3 documentation, or on the platform-demos。
至于 argv[1]
,请查找有关 argc
和 argv
的 C 教程,它们是您将命令行参数传递给程序的方式(不,那不是linux-具体)。
我将一个 USB 键盘连接到我的板上并尝试 运行 我的程序直接在它的终端中运行并且它没有问题并在我的 SPI LCD 中显示了我的图片。