1、收先安装gtk3,我的测试机器是ubutn16.04,只要执行下面的安装命令就可以了
apt-get install libgtk-3-dev
使用下列命令验证是否安装好gtk3:
pkg-config --cflags --libs gtk+-3.0
2、显示结果类似如下:
-pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/mirclient -I/usr/include/mircore -I/usr/include/mircookie -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0说明gtk3可以使用了。
3、文件名:“enquire_camera.c” 代码如下:
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <zconf.h>
#include <linux/videodev2.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk-3.0/gtk/gtk.h>
//dhl:gtk窗口组件
GtkWidget *window;
GtkWidget *fixed;
GtkWidget *button1,*button2;
GtkWidget *text,*text1,*text2,*text3;
GtkTextBuffer *buffer,*buffer1,*buffer2,*buffer3;
GtkWidget *label1,*label2,*label3,*label_line;
//dhl:消息缓存
char disp[2048]={0};
char temp[128]={0};
//dhl:发现摄像头
void button1_clicked(GtkWidget *widget, gpointer data)
{
char camera_devices_name[128]={0};
for (int loop=0;loop<10;loop++) {
sprintf(camera_devices_name,"/dev/video%d",loop);
int fd = open(camera_devices_name, O_RDWR);
if(fd < 0)
{
printf("打开设备失败(%s)\n",camera_devices_name);
close(fd);
continue;
}
close(fd);
sprintf(temp,"发现摄像头:%s\n",camera_devices_name);
strcat(disp,temp);
gtk_text_buffer_set_text(buffer,disp,-1);
}
sprintf(disp,"%s","");
}
//dhl:查询配置信息
void button2_clicked(GtkWidget *widget, gpointer data)
{
int line_num=0;
gchar *device_name;
GtkTextIter start,end;
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buffer1),&start,&end);
const GtkTextIter s=start,e=end;
device_name=gtk_text_buffer_get_text(GTK_TEXT_BUFFER(buffer1),&s,&e,FALSE);
if (!strcmp(device_name,""))
{
sprintf(temp,"请输入摄像头设备文件名\n");
strcat(disp,temp);
gtk_text_buffer_set_text(buffer,disp,-1);
sprintf(disp,"%s","");
return;
}
//dhl:查询摄像头支持的视频格式
int fd = open(device_name, O_RDWR);
if(fd < 0)
{
printf("打开设备失败\n");
return;
}
struct v4l2_fmtdesc v4fmt;
v4fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; //视频捕获设备
int i=0;
while(1)
{
v4fmt.index = i++;
int ret = ioctl(fd, VIDIOC_ENUM_FMT, &v4fmt);
if(ret < 0)
{
printf("获取摄像头格式失败");
break;
}
printf("index=%d\n", v4fmt.index);
printf("flags=%d\n", v4fmt.flags);
printf("description=%s\n", v4fmt.description);
unsigned char *p = (unsigned char *)&v4fmt.pixelformat;
printf("pixelformat=%c%c%c%c\n", p[0],p[1],p[2],p[3]);
printf("reserved=%d\n", v4fmt.reserved[0]);
sprintf(temp,"摄像头支持的视频格式(%d)\n",i);
strcat(disp,temp);
sprintf(temp,"index=%d,", v4fmt.index);
strcat(disp,temp);
sprintf(temp,"flags=%d,", v4fmt.flags);
strcat(disp,temp);
sprintf(temp,"description=%s,", v4fmt.description);
strcat(disp,temp);
sprintf(temp,"pixelformat=%c%c%c%c,", p[0],p[1],p[2],p[3]);
strcat(disp,temp);
sprintf(temp,"reserved=%d\n", v4fmt.reserved[0]);
strcat(disp,temp);
}
close(fd);
//dhl:MJPG支持的所有分辨率如下
fd = open(device_name, O_RDWR);
if(fd < 0)
{
perror("打开设备失败");
return;
}
struct v4l2_frmsizeenum frmsize;
frmsize.index = 0;
frmsize.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
printf("MJPEG格式支持所有分辨率如下:\n");
//frmsize.pixel_format = V4L2_PIX_FMT_YUYV;
frmsize.pixel_format = V4L2_PIX_FMT_MJPEG;
sprintf(temp,"MJPG支持的分辨率:\n");
strcat(disp,temp);
line_num=1;
while(ioctl(fd,VIDIOC_ENUM_FRAMESIZES,&frmsize) == 0){
printf("frame_size<%d*%d>\n",frmsize.discrete.width,frmsize.discrete.height);
frmsize.index++;
if ((line_num % 5) != 0) {
sprintf(temp,"MJPEG frame_size<%d*%d>, ",frmsize.discrete.width,frmsize.discrete.height);
}else {
sprintf(temp,"MJPEG frame_size<%d*%d>\n",frmsize.discrete.width,frmsize.discrete.height);
}
strcat(disp,temp);
line_num++;
if (line_num == 6) {
line_num = 1;
}
}
close(fd);
sprintf(temp,"%s","\n");
strcat(disp,temp);
//dhl:YUV支持所有分辨率如下
fd = open(device_name, O_RDWR);
if(fd < 0)
{
perror("打开设备失败");
return;
}
//struct v4l2_frmsizeenum frmsize;
frmsize.index = 0;
frmsize.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
printf("MJPEG格式支持所有分辨率如下:\n");
frmsize.pixel_format = V4L2_PIX_FMT_YUYV;
//frmsize.pixel_format = V4L2_PIX_FMT_MJPEG;
sprintf(temp,"YUV支持的分辨率:\n");
strcat(disp,temp);
line_num = 1;
while(ioctl(fd,VIDIOC_ENUM_FRAMESIZES,&frmsize) == 0){
printf("frame_size<%d*%d>\n",frmsize.discrete.width,frmsize.discrete.height);
frmsize.index++;
if ((line_num % 5) != 0) {
sprintf(temp,"YUYV frame_size<%d*%d>, ",frmsize.discrete.width,frmsize.discrete.height);
}else {
sprintf(temp,"YUYV frame_size<%d*%d>\n",frmsize.discrete.width,frmsize.discrete.height);
}
strcat(disp,temp);
line_num++;
if (line_num == 6) {
line_num = 1;
}
}
close(fd);
gtk_text_buffer_set_text(buffer,disp,-1);
sprintf(disp,"%s","");
}
int main(int argc,char *argv[])
{
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"USB摄像头测试程序(v1.0.20230728)");
//gtk_window_set_resizable(GTK_WINDOW(window),FALSE);
gtk_window_set_default_size(GTK_WINDOW(window),1280,800);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window),fixed);
text = gtk_text_view_new();
buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
gtk_text_buffer_set_text(buffer,"",-1);
gtk_fixed_put(GTK_FIXED(fixed),text,20,480);
gtk_widget_set_size_request(text,1240,300);
button1 = gtk_button_new_with_label("查询摄像头");
gtk_fixed_put(GTK_FIXED(fixed),button1,320,14);
gtk_widget_set_size_request(button1,100,20);
label1 = gtk_label_new("摄像头:");
gtk_fixed_put(GTK_FIXED(fixed),label1,440,14);
gtk_widget_set_size_request(label1,50,30);
text1 = gtk_text_view_new();
buffer1=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
gtk_text_buffer_set_text(buffer1,"",-1);
gtk_fixed_put(GTK_FIXED(fixed),text1,500,16);
gtk_widget_set_size_request(text1,100,28);
sprintf(disp,"/dev/video0");
gtk_text_buffer_set_text(buffer1,disp,-1);
button2 = gtk_button_new_with_label("查询配置");
gtk_fixed_put(GTK_FIXED(fixed),button2,610,14);
gtk_widget_set_size_request(button2,80,35);
label2 = gtk_label_new("分辨率:");
gtk_fixed_put(GTK_FIXED(fixed),label2,740,18);
gtk_widget_set_size_request(label2,50,30);
text2 = gtk_text_view_new();
buffer2=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text2));
gtk_text_buffer_set_text(buffer2,"",-1);
gtk_fixed_put(GTK_FIXED(fixed),text2,800,16);
gtk_widget_set_size_request(text2,100,28);
sprintf(disp,"640x480");
gtk_text_buffer_set_text(buffer2,disp,-1);
label3 = gtk_label_new("帧率:");
gtk_fixed_put(GTK_FIXED(fixed),label3,930,18);
gtk_widget_set_size_request(label3,50,30);
text3 = gtk_text_view_new();
buffer3=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text3));
gtk_text_buffer_set_text(buffer3,"",-1);
gtk_fixed_put(GTK_FIXED(fixed),text3,980,16);
gtk_widget_set_size_request(text3,100,28);
sprintf(disp,"30");
gtk_text_buffer_set_text(buffer3,disp,-1);
g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(button1), "clicked", G_CALLBACK(button1_clicked),NULL);
g_signal_connect(G_OBJECT(button2), "clicked", G_CALLBACK(button2_clicked),NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
4、编译命令:gcc enquire_camera.c -o camera_enquire `pkg-config --cflags --libs gtk+-3.0` -w
5、运行 “./camera_enquire”
6、运行结果: