?? test_cam.c
字號:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <linux/videodev.h>
typedef struct{
int fd;
int use_mmap;
int width, height;
int frame_rate;
int frame_size;
struct video_capability capability;
struct video_buffer buffer;
struct video_window vwin;
struct video_picture picture;
struct video_mmap vmmap;
struct video_mbuf vmbuf;
unsigned char *frame_buffer;
int dev;
int capturing;
int buffer_size ;
} video_device;
#define DEFAULT_DEVICE "/dev/v4l/video0" ;
int camera_open(char *dev, video_device *vd);
int camera_close(video_device *vd);
int camera_get_capability(video_device *vd);
int camera_get_picture(video_device *vd);
//int camera_grab_init(v4l_device *vd, int input, int norm);
unsigned char * camera_grab_image(video_device *vd);
//int camera_grab_sync(v4l_device *vd);
//int camera_mmap_init(v4l_device *vd);
//int camera_get_mbuf(v4l_device *vd);
video_device vd;
int camera_open (char *dev, video_device *vd)
{
printf("step1 \n");
//if (!dev)
//dev = DEFAULT_DEVICE;
if ((vd->fd = open("/dev/v4l/video0", O_RDWR))<0)
{
printf("open error \n");
return -1;
}
printf("step2 \n");
if (camera_get_capability(vd)<0)
{
printf("camera_get_capability error \n");
return -1;
}
printf("step3 \n");
if (camera_get_picture(vd)<0)
{
printf("camera_get_picture error \n");
return -1;
}
printf("step4 \n");
return 0;
}
int camera_get_capability(video_device *vd)
{
if (ioctl(vd->fd, VIDIOCGCAP, &(vd->capability))<0)
{
return -1;
}
return 0;
}
int camera_get_picture(video_device *vd)
{
if (ioctl(vd->fd, VIDIOCGPICT, &(vd->picture))<0)
{
return -1;
}
return 0;
}
int camera_close(video_device *vd)
{
close(vd->fd);
return 0;
}
unsigned char * camera_grab_image(video_device *vd) {
int len;
int i;
//assert(vd != (video_device *)0);
if (vd->use_mmap) {
if (!vd->capturing) {
// 等待請求獲取完整的幀
for (i = 0; i< vd->vmbuf.frames; ++i) {
vd->vmmap.frame = i;
if (ioctl(vd, VIDIOCMCAPTURE, &vd->vmmap))
{
perror("VIDIOCMCAPTURE");
return 0;
}
}
// 從零開始讀
vd->vmmap.frame = 0;
vd->capturing = 1;
}
// 給ioctl()傳入VIDIOCSYNC,檢查幀是否已經(jīng)獲取完成
if (ioctl(vd->dev, VIDIOCSYNC, &vd->vmmap.frame)) {
//perror("VIDIOCSYNC:");
return 0;
}
return vd->frame_buffer + vd->vmbuf.offsets[vd->vmmap.frame];
}
// 否則去讀取確切的大小
len = read(vd->dev, vd->frame_buffer, vd->buffer_size);
if(len<=0)
return 0;
if (len!=vd->buffer_size) {
fprintf(stderr, "Expected to read %d bytes, actually read %d\n", vd->buffer_size, len);
return 0;
}
return vd->frame_buffer;
}
int main()
{
if (camera_open("/dev/v4l/video0", &vd))
return -1;
while(1){
sleep(1);
camera_grab_image(&vd);
}
camera_close(&vd);
return 1;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -