V4L2摄像头拍摄静态图片

只是备份一下代码(此代码来自一个好友),如果有错误,或者另有高见,还请大方之家多多指点,如果可以另大家有所裨益,乃是在下三生有幸。
下面代码是一个关于V4L2调用摄像头的静态存取。

#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/videodev2.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

int main(){
    int fd = open("/dev/video1",O_RDWR);
    int size,i;
    char name[6]="0.jpg";
    char szErrorBuffer[256];
    void *buff = malloc(640*480);
    struct v4l2_capability cap;
    struct v4l2_format fmt;
        ioctl(fd, VIDIOC_QUERYCAP, &cap);
     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        fmt.fmt.pix.width  = 640;
        fmt.fmt.pix.height = 480;
        fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
        ioctl(fd,VIDIOC_S_FMT,&fmt);
    for (i=0;i<6;i++){
        size = read(fd,buff,640*480);
        if (size==-1){
            memset(szErrorBuffer,0x00,sizeof(szErrorBuffer));
            sprintf(szErrorBuffer,"read:%s\n",(char*)strerror(errno));
            write(STDERR_FILENO,szErrorBuffer,strlen(szErrorBuffer));   
        }
        int filefd = open(name,O_RDWR | O_CREAT,0777);
        write(filefd, buff, size);
        if (name[0]=='5')
            name[0]='0';
        else
            name[0]++;      
        sleep(1);
    }
    close(fd);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38313674/article/details/80156783