• 大小: 4KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: 其他
  • 标签: V4L2  

资源简介

Linux环境下使用V4L2+opencv以MJPEG格式读取USB摄像头并实时显示的源代码

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

uchar *buffer;

#define IMAGEWIDTH 640
#define IMAGEHEIGHT 480

#define TRUE 1
#define FALSE 0

#define FILE_VIDEO1 “/dev/video1“

static int fd;
struct v4l2_streamparm setfps;
struct v4l2_capability cap;
struct v4l2_fmtdesc fmtdesc;
struct v4l2_format fmtfmtack;
struct v4l2_requestbuffers req;
struct v4l2_buffer buf;
enum   v4l2_buf_type type;

int init_v4l2(void);
int v4l2_grab(void);



int main()
{
        printf(“first~~\n“);
        if(init_v4l2() == FALSE){
            printf(“Init fail~~\n“);
            exit(1);
        }
        printf(“second~~\n“);
        if(v4l2_grab() == FALSE){
            printf(“grab fail~~\n“);
            exit(2);
        }
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory = V4L2_MEMORY_MMAP;
        printf(“third~~\n“);
        cvNamedWindow(“one“CV_WINDOW_AUTOSIZE);
        IplImage* img;
        CvMat cvmat;
        int i = 100;
        double t;
        while(1){
                t = (double)cvGetTickCount();
                ioctl(fdVIDIOC_DQBUF&buf);
                buf.index = 0;
                cvmat = cvMat(IMAGEHEIGHTIMAGEWIDTHCV_8UC3(void*)buffer);//CV_8UC3
                //t = (double)cvGetTickCount();
                img = cvDecodeImage(&cvmat1);
                //t=(double)cvGetTickCount()-t;
                //printf(“used time is %gms\n“(t/(cvGetTickFrequency()*1000)));
                if(!img)    printf(“No img\n“);
                cvShowImage(“one“img);
                cvReleaseImage(&img);
                ioctl(fdVIDIOC_QBUF&buf);
                if((cvWaitKey(1)&255) == 27)    exit(0);
                t=(double)cvGetTickCount()-t;
                printf(“used time is %gms\n“(t/(cvGetTickFrequency()*1000)));
        }

        ioctl(fdVIDIOC_STREAMOFF&type);
        return 0;
}

int init_v4l2(void){
        if ((fd = open(FILE_VIDEO1 O_RDWR)) == -1){
            printf(“Opening video device error\n“);
            return FALSE;
        }
        if (ioctl(fd VIDIOC_QUERYCAP &cap) == -1){
                printf(“unable Querying Capabilities\n“);
                return FALSE;
        }
        else
/*
        {
        printf( “Driver Caps:\n“
                “  Driver: \“%s\“\n“
                “  Card: \“%s\“\n“
                “  Bus: \“%s\“\n“
                “  Version: %d\n“
                “  Capabilities: %x\n“
                cap.driver
                cap.card
                cap.bus_info
                cap.version
                cap.capabilities);

        }

        if((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == V4L2_CAP_VIDEO_CAPTURE){
            printf(“Camera device %s: support capture\n“FILE_VIDEO1);
        }
        if((cap.capabilities & V4L2_CAP_STREAM

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        248  2015-08-12 16:19  singleCamD\singleCamD.pro

     文件      18135  2015-08-12 20:59  singleCamD\singleCamD.pro.user

     文件       6306  2015-08-12 20:46  singleCamD\main.cpp

     目录          0  2015-08-12 20:59  singleCamD

----------- ---------  ---------- -----  ----

                24689                    4


评论

共有 条评论