V4L2视频

来源:互联网 发布:python deap 编辑:程序博客网 时间:2024/05/17 09:11

这里的视频采集是为了得到某种固定的格式的图像,然后提供给编码器进行编码操作。


这里视频采集的有几个比较关键的参数

1.分辨率

2.帧率

3.采集的格式


我们知道Linux内核使用V4L2框架进行摄像头的采集和处理,所以我们要做的事情就是 根据给定的 分辨率和帧率 给出视频流即可

这里我使用的平台是Android,当然内核也是linux,只是不同之处是后面增加的JNI封装,提供给Java程序掉用,不过不需要可以忽略掉JNI。


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #ifndef LINUX  
  2. #include <jni.h>  
  3. #include <android/log.h>  
  4. #include <android/bitmap.h>  
  5. #endif   
  6.   
  7. #include <string.h>  
  8.   
  9. #include <stdio.h>  
  10. #include <stdlib.h>  
  11. #include <string.h>  
  12. #include <assert.h>  
  13.   
  14. #include <fcntl.h>              /* low-level i/o */  
  15. #include <unistd.h>  
  16. #include <errno.h>  
  17. #include <malloc.h>  
  18. #include <sys/stat.h>  
  19. #include <sys/types.h>  
  20. #include <sys/time.h>  
  21. #include <sys/mman.h>  
  22. #include <sys/ioctl.h>  
  23.   
  24. #include <asm/types.h>          /* for videodev2.h */  
  25.   
  26. #include <linux/videodev2.h>  
  27. #include <linux/usbdevice_fs.h>  
  28.   
  29. #define  LOG_TAG    "TEST"  
  30. #ifndef LINUX  
  31. #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)  
  32. #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)  
  33. #define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)  
  34. #else  
  35. #define  LOGI   printf  
  36. #define  LOGE   printf  
  37. #define  LOGD   printf  
  38. #endif  
  39.   
  40. #define CLEAR(x) memset (&(x), 0, sizeof (x))  
  41.   
  42. //#define IMG_WIDTH 640  
  43. //#define IMG_HEIGHT 480  
  44.   
  45. #define ERROR_DEVICE_NOT_EXIST -1  
  46. #define ERROR_DEVICE_TYPE_ERROR -2  
  47. #define ERROR_DEVICE_OPEN_FALIED -3  
  48. #define ERROR_DEVICE_CAP_NOT_SUPPORT -4  
  49. #define ERROR_DEVICE_CAP_ERROR -5  
  50.   
  51. #define ERROR_VIDIOC_QUERYBUF -6  
  52. #define ERROR_VIDIOC_QBUF -7  
  53. #define ERROR_VIDIOC_DQBUF -70  
  54. #define ERROR_REQBUFS -71  
  55.   
  56.   
  57. #define ERROR_MMAP_FAILD -8  
  58. #define ERROR_UNMMAP_FAILD -88  
  59. #define ERROR_LOCAL -9  
  60. #define ERROR_VIDIOC_STREAMON -10  
  61. #define ERROR_VIDIOC_STREAMOFF -11  
  62. #define ERROR_SELECT -12  
  63.   
  64. #define SUCCESSED 0  
  65.   
  66. struct buffer {  
  67.         void *                  start;  
  68.         size_t                  length;  
  69. };  
  70.   
  71. typedef struct camera {  
  72.     char device_name[32];//用指针不方便修改  
  73.     int fd;  
  74.     int width;  
  75.     int height;  
  76.     int display_depth;  
  77.     int image_size;  
  78.     int frame_number;  
  79.     int framerate;  
  80.     struct v4l2_capability v4l2_cap;  
  81.     struct v4l2_cropcap v4l2_cropcap;  
  82.     struct v4l2_format v4l2_fmt;  
  83.     struct v4l2_crop crop;  
  84.     struct buffer *buffers;  
  85. }Camera;  
  86.   
  87.   
  88.   
  89. int xioctl(int fd, int request, void *arg);  
  90. int open_camera(Camera *cam);  
  91. int init_camera(Camera *cam);  
  92. int initmmap(Camera *cam);  
  93. int startcapturing(Camera *cam);  
  94.   
  95. int readframeonce(Camera *cam,char *buffer,int *size);  
  96. int readframe(Camera *cam,char *buffer,int *size);  
  97. void processimage (const void *p);  
  98.   
  99. int stopcapturing(Camera *cam);  
  100. int uninitdevice(Camera *cam);  
  101. int closedevice(Camera *cam);  
  102.   
  103.   
  104.   
  105. #ifndef LINUX  
  106. extern "C"  
  107. {  
  108. jint Java_com_ist_Camera_prepareCamera( JNIEnv* env,jclass thiz, jint videoid ,jint width,jint height,jint framerate);  
  109. void Java_com_ist_Camera_processCamera( JNIEnv* env,jclass thiz,jbyteArray buf);  
  110. void Java_com_ist_Camera_stopCamera(JNIEnv* env,jclass thiz);  
  111.                                                   
  112. }  
  113.   
  114. #endif  

这里我定义了一个Camera结构,和一些函数用来做图像采集。封装一系列摄像头的操作。

看JNI的封装会比较清晰一点, 准备 处理、关闭。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "ImageProc.h"  
  2. #include <string>  
  3. using  namespace std;  
  4.   
  5.   
  6.   
  7. // 定义一个 Camera的指针  
  8. Camera *camera=NULL;  
  9. int n_buffers=0;  
  10. char * g_buffer=NULL;  
  11. char * g_buffer2=NULL;  
  12.   
  13.   
  14.   
  15. int setInterge(JNIEnv *env, jobject thiz, jobject p1, int value)   
  16. {  
  17.     jclass c;  
  18.     jfieldID id;  
  19.     c = env->FindClass("java/lang/Integer");  
  20.     if (c==NULL)  
  21.     {  
  22.         LOGD("FindClass failed");  
  23.         return -1;  
  24.     }  
  25.   
  26.     id = env->GetFieldID(c, "value""I");  
  27.     if (id==NULL)  
  28.     {  
  29.         LOGD("GetFiledID failed");  
  30.         return -1;  
  31.     }  
  32.   
  33.     env->SetIntField(p1, id, value);  
  34.   
  35.     return 0;  
  36. }  
  37.   
  38.   
  39.   
  40.   
  41.   
  42.   
  43. void  yuyv_2_yuv420( int inWidth, int inHeight  ,unsigned char * psrc,unsigned char  *pDest)    
  44. {    
  45.     int       i,j;    
  46.     unsigned char *Y ,*U,*V;    
  47.     unsigned char y1,y2,u,v;    
  48.         
  49.     Y=pDest;    
  50.     U=pDest+inHeight*inWidth;    
  51.     V=U+inHeight*inWidth/4;    
  52.     
  53.         
  54.      for(i=0;i<inHeight;i++)    
  55.         {    
  56.             for(j=0;j<inWidth/2;j++)    
  57.             {    
  58.                 y1 = *( psrc + (i*inWidth/2+j)*4);    
  59.                 u  = *( psrc + (i*inWidth/2+j)*4 + 1);    
  60.                 y2 = *( psrc + (i*inWidth/2+j)*4 + 2);    
  61.                 v  = *( psrc + (i*inWidth/2+j)*4 + 3);    
  62.                 
  63.             *Y++=y1;    
  64.             *Y++=y2;    
  65.                 
  66.             if(i%2==0)    
  67.             {    
  68.              *U++=u;    
  69.              *V++=v;    
  70.             }    
  71.         }    
  72.     }    
  73.     
  74. }    
  75.   
  76.   
  77.   
  78. int xioctl(int fd, int request, void *arg)  
  79. {  
  80.     int r = 0;  
  81.     do {  
  82.         r = ioctl(fd, request, arg);  
  83.     } while (-1 == r && EINTR == errno);  
  84.   
  85.     return r;  
  86. }  
  87.   
  88.   
  89. //打开video设备  
  90. int open_camera(Camera *cam)  
  91. {  
  92.     struct stat st;  
  93.   
  94.     //stat() 获得文件属性,并判断是否为字符设备文件  
  95.       
  96.     if (-1 == stat (cam->device_name, &st)) {  
  97.         LOGE("Cannot identify '%s': %d, %s", cam->device_name, errno, strerror (errno));  
  98.         return ERROR_DEVICE_NOT_EXIST;//改ID设备不存在  
  99.     }  
  100.   
  101.     if (!S_ISCHR (st.st_mode)) {  
  102.         LOGE("%s is no device", cam->device_name);  
  103.         return ERROR_DEVICE_TYPE_ERROR;//设备类型不匹配  
  104.     }  
  105.   
  106.     cam->fd = open(cam->device_name, O_RDWR| O_NONBLOCK, 0); //  | O_NONBLOCK  
  107.   
  108.     if (-1 == cam->fd) {  
  109.         LOGE("Cannot open '%s': %d, %s", cam->device_name, errno, strerror (errno));  
  110.         return ERROR_DEVICE_OPEN_FALIED;//打开失败  
  111.     }  
  112.     LOGD(" open '%s' ok", cam->device_name);  
  113.     //  
  114.       
  115.       
  116.     return SUCCESSED;  
  117. }  
  118.   
  119.   
  120.   
  121.   
  122. //初始化设备  
  123. int init_camera( Camera *cam) {  
  124.     struct v4l2_capability *cap = &(cam->v4l2_cap);  
  125.     struct v4l2_cropcap *cropcap = &(cam->v4l2_cropcap);  
  126.     struct v4l2_crop *crop = &(cam->crop);  
  127.     struct v4l2_format *fmt = &(cam->v4l2_fmt);  
  128.   
  129.     unsigned int min;   
  130.     int ret =0;  
  131.     struct v4l2_fmtdesc fmtdesc;  
  132.       
  133.     //VIDIOC_QUERYCAP 命令 来获得当前设备的各个属性  
  134.     if (-1 == xioctl(cam->fd, VIDIOC_QUERYCAP, cap)) {  
  135.         if (EINVAL == errno) {  
  136.             LOGE("%s is no V4L2 device\n", cam->device_name);  
  137.             return ERROR_DEVICE_CAP_ERROR;  
  138.         } else {  
  139.             LOGE("%s error %d, %s\n""VIDIOC_QUERYCAP", errno, strerror(errno));  
  140.             return ERROR_DEVICE_CAP_ERROR;  
  141.         }  
  142.     }  
  143.       
  144.     LOGI("\nVIDOOC_QUERYCAP\n");  
  145.     LOGI("the camera driver is %s\n", cap->driver);  
  146.     LOGI("the camera card is %s\n", cap->card);//UVC Camera (046d:081b)  
  147.     LOGI("the camera bus info is %s\n", cap->bus_info);  
  148.     LOGI("the version is %d\n", cap->version);//199168  
  149.       
  150.       
  151.     if (!(cap->capabilities & V4L2_CAP_VIDEO_CAPTURE)) {  
  152.         LOGE( "%s is no video capture device\n", cam->device_name);  
  153.         return ERROR_DEVICE_CAP_ERROR;  
  154.     }  
  155.       
  156.       
  157.     fmtdesc.index=0;  
  158.     fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  159.     LOGE("Support format:\n");  
  160.     while(ioctl(cam->fd,VIDIOC_ENUM_FMT,&fmtdesc)!=-1)  
  161.     {  
  162.      LOGE("\t%d.%s\n",fmtdesc.index+1,fmtdesc.description);  
  163.      fmtdesc.index++;  
  164.     }  
  165.       
  166.     if (!(cap->capabilities & V4L2_CAP_STREAMING)) {  
  167.         LOGE( "%s does not support streaming i/o\n",cam->device_name);  
  168.         return ERROR_DEVICE_CAP_ERROR;  
  169.     }  
  170.       
  171.   
  172.     //获得设备对 Image Cropping 和 Scaling 的支持  
  173.     CLEAR (*cropcap);  
  174.   
  175.       
  176.     cropcap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  177.     crop->c.width = cam->width;  
  178.     crop->c.height = cam->height;  
  179.     crop->c.left = 0;  
  180.     crop->c.top = 0;  
  181.     crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  182.       
  183.     //设置图形格式  
  184.     CLEAR (*fmt);  
  185.     LOGI( "%s setfmt go...!\n", cam->device_name);  
  186.     fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  187.     fmt->fmt.pix.width = cam->width;  
  188.     fmt->fmt.pix.height = cam->height;  
  189.     //fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_H264; //yuv422  
  190.     fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;   
  191.     fmt->fmt.pix.field = V4L2_FIELD_INTERLACED; //隔行扫描  
  192.       
  193.     //检查流权限  
  194.     if (-1 == xioctl(cam->fd, VIDIOC_S_FMT, fmt)){  
  195.         LOGE("%s error %d, %s\n""VIDIOC_S_FMT", errno, strerror(errno));  
  196.         return ERROR_DEVICE_CAP_ERROR;  
  197.           
  198.     }  
  199.   
  200.     min = fmt->fmt.pix.width * 2;  
  201.     //每行像素所占的 byte 数  
  202.     if (fmt->fmt.pix.bytesperline < min)  
  203.         fmt->fmt.pix.bytesperline = min;  
  204.     min = fmt->fmt.pix.bytesperline * fmt->fmt.pix.height;  
  205.     if (fmt->fmt.pix.sizeimage < min)  
  206.         fmt->fmt.pix.sizeimage = min;  
  207.   
  208.       
  209.     LOGI("NEW yuyv buffer length = %d \n",cam->width*cam->height *2);   
  210.     LOGI("NEW yuv420 buffer length = %d \n",cam->width*cam->height /2*3);  
  211.     g_buffer = new char [cam->width*cam->height *2];  
  212.     g_buffer2 = new char [cam->width*cam->height/2*3];  
  213.     //设置帧率  
  214.       
  215.       
  216.     struct v4l2_streamparm Stream_Parm;  
  217.     memset(&Stream_Parm, 0, sizeof(struct v4l2_streamparm));  
  218.     Stream_Parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;   
  219.   
  220.     Stream_Parm.parm.capture.timeperframe.denominator =cam->framerate;;  
  221.     Stream_Parm.parm.capture.timeperframe.numerator = 1;  
  222.   
  223.     ret = xioctl(cam->fd, VIDIOC_S_PARM, &Stream_Parm);  
  224.     LOGI("VIDIOC_S_PARM= %d \n",ret);     
  225.     return initmmap (cam);  
  226.   
  227. }  
  228.   
  229.   
  230. //I/O模式选择  
  231. int initmmap( Camera *cam)  
  232. {  
  233.     struct v4l2_requestbuffers req;  
  234.     CLEAR (req);  
  235.     LOGI("initmap\n");  
  236.     req.count               = 4;  
  237.     req.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  238.     req.memory              = V4L2_MEMORY_MMAP;  
  239.   
  240.     if (-1 == xioctl (cam->fd, VIDIOC_REQBUFS, &req)) {  
  241.         if (EINVAL == errno) {  
  242.             LOGE("%s does not support memory mapping", cam->device_name);  
  243.             return ERROR_LOCAL;  
  244.         } else {  
  245.             return ERROR_REQBUFS;  
  246.         }  
  247.     }  
  248.   
  249.     if (req.count < 2) {  
  250.         LOGE("Insufficient buffer memory on %s", cam->device_name);  
  251.         return ERROR_LOCAL;  
  252.     }  
  253.   
  254.     cam->buffers =(struct buffer *) calloc (req.count, sizeof (*  (cam->buffers) ));  
  255.   
  256.     if (!cam->buffers) {  
  257.         LOGE("Out of memory");  
  258.         return ERROR_LOCAL;  
  259.     }  
  260.   
  261.     for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {  
  262.         struct v4l2_buffer buf;  
  263.   
  264.          CLEAR (buf);  
  265.   
  266.         buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  267.         buf.memory      = V4L2_MEMORY_MMAP;  
  268.         buf.index       = n_buffers;  
  269.   
  270.         if (-1 == xioctl (cam->fd, VIDIOC_QUERYBUF, &buf))  
  271.             return ERROR_VIDIOC_QUERYBUF;  
  272.         LOGI("MMAP_SIZE=%d\n",buf.length);  
  273.         cam->buffers[n_buffers].length = buf.length;  
  274.         cam->buffers[n_buffers].start =  
  275.         mmap (NULL ,  
  276.             buf.length,  
  277.             PROT_READ | PROT_WRITE,  
  278.             MAP_SHARED,  
  279.             cam->fd, buf.m.offset);  
  280.   
  281.         if (MAP_FAILED == cam->buffers[n_buffers].start)  
  282.             return ERROR_MMAP_FAILD;  
  283.     }  
  284.   
  285.     return SUCCESSED;  
  286. }  
  287.   
  288. int startcapturing(Camera *cam)  
  289. {  
  290.     unsigned int i;  
  291.     enum v4l2_buf_type type;  
  292.   
  293.     for (i = 0; i < n_buffers; ++i) {  
  294.         struct v4l2_buffer buf;  
  295.   
  296.         CLEAR (buf);  
  297.   
  298.         buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  299.         buf.memory      = V4L2_MEMORY_MMAP;  
  300.         buf.index       = i;  
  301.   
  302.         if (-1 == xioctl (cam->fd, VIDIOC_QBUF, &buf))  
  303.             return ERROR_VIDIOC_QBUF;  
  304.     }  
  305.   
  306.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  307.   
  308.     if (-1 == xioctl (cam->fd, VIDIOC_STREAMON, &type))  
  309.         return ERROR_VIDIOC_STREAMON;  
  310.   
  311.     return SUCCESSED;  
  312. }  
  313.   
  314. int readframeonce(Camera *cam,char *buf ,int * size)  
  315. {  
  316.     for (;;) {  
  317.         fd_set fds;  
  318.         struct timeval tv;  
  319.         int r;  
  320.   
  321.         FD_ZERO (&fds);  
  322.         FD_SET (cam->fd, &fds);  
  323.   
  324.         tv.tv_sec = 2;  
  325.         tv.tv_usec = 0;  
  326.   
  327.         r = select (cam->fd + 1, &fds, NULL, NULL, &tv);  
  328.   
  329.         if (-1 == r) {  
  330.             if (EINTR == errno)  
  331.                 continue;  
  332.   
  333.             return ERROR_SELECT;  
  334.         }  
  335.   
  336.         if (0 == r) {  
  337.             LOGE("select timeout");  
  338.             return ERROR_LOCAL;  
  339.   
  340.         }  
  341.   
  342.         if (readframe (cam,buf,size)==1)  
  343.             break;  
  344.   
  345.     }  
  346.   
  347.     return SUCCESSED;  
  348.   
  349. }  
  350.   
  351.   
  352.   
  353.   
  354.   
  355. void cat_data( unsigned char * buf,int size){  
  356.     int i;   
  357.     string ss("");  
  358.      char cc[10];  
  359.     for( i=0;i<size;i++){  
  360.         sprintf(cc,"%02x ",buf[i]);  
  361.         ss.append(cc);  
  362.     }  
  363.     LOGI(ss.c_str());  
  364.     LOGI("\n");  
  365. }  
  366.   
  367.   
  368.   
  369.   
  370.   
  371.   
  372. int readframe(Camera *cam,char *buffer,int *size){  
  373.   
  374.     struct v4l2_buffer buf;  
  375.     unsigned int i;  
  376.   
  377.     CLEAR (buf);  
  378.   
  379.     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  380.     buf.memory = V4L2_MEMORY_MMAP;  
  381.     //buf.memory = V4L2_MEMORY_USERPTR;  
  382.     //LOGE("fd=%d,request=%d,buf=%d",fd,VIDIOC_DQBUF,&buf);  
  383.     if (-1 == xioctl (cam->fd, VIDIOC_DQBUF, &buf)) {  
  384.         switch (errno) {  
  385.             case EAGAIN:  
  386.                 return 0;  
  387.             case EIO:  
  388.             default:  
  389.                 return ERROR_VIDIOC_DQBUF;  
  390.         }  
  391.     }  
  392.   
  393.     assert (buf.index < n_buffers);  
  394.   
  395.       
  396.     memcpy(g_buffer,cam->buffers[buf.index].start,cam->buffers[buf.index].length);  
  397.     //*size =cam->buffers[buf.index].length;  
  398.     yuyv_2_yuv420(cam->width,cam->height,(unsigned char *)g_buffer,(unsigned char *)g_buffer2);  
  399.     *size =(cam->buffers[buf.index].length/4*3 );   
  400.           
  401.       
  402.     LOGI("get yuv420 SIZE = %d",*size);  
  403.     if (-1 == xioctl (cam->fd, VIDIOC_QBUF, &buf))  
  404.         return ERROR_VIDIOC_QBUF;  
  405.     return 1;  
  406. }  
  407.   
  408. int stopcapturing(Camera *cam)  
  409. {  
  410.     enum v4l2_buf_type type;  
  411.   
  412.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  413.   
  414.     if (-1 == xioctl (cam->fd, VIDIOC_STREAMOFF, &type))  
  415.         return ERROR_VIDIOC_STREAMOFF;  
  416.   
  417.     return SUCCESSED;  
  418.   
  419. }  
  420.   
  421. int uninitdevice(Camera *cam)  
  422. {  
  423.     unsigned int i;  
  424.   
  425.     for (i = 0; i < n_buffers; ++i)  
  426.         if (-1 == munmap (cam->buffers[i].start, cam->buffers[i].length))  
  427.             return ERROR_UNMMAP_FAILD;  
  428.   
  429.     free(cam->buffers);  
  430.     free(g_buffer);  
  431.     free(g_buffer2);  
  432.     return SUCCESSED;  
  433. }  
  434.   
  435.   
  436.   
  437. //关闭设备  
  438. int closedevice(Camera *cam)  
  439. {  
  440.     if (-1 == close (cam->fd)){  
  441.         cam->fd = -1;  
  442.         return ERROR_LOCAL;  
  443.     }  
  444.   
  445.     cam->fd = -1;  
  446.     return SUCCESSED;  
  447. }  
  448.   
  449.   
  450.   
  451.   
  452. #ifndef LINUX  
  453.   
  454.   
  455. jint Java_com_ist_Camera_prepareCamera( JNIEnv* env,jclass thiz, jint videoid,jint width,jint height,jint framerate){  
  456.         int ret;  
  457.         camera = ( Camera *) malloc( sizeof( Camera ) );  
  458.           
  459.         sprintf(camera->device_name,"/dev/video%d",videoid);  
  460.         camera->buffers = NULL;  
  461.         camera->width = width;  
  462.         camera->height = height;  
  463.         camera->display_depth = 5; /* RGB24 */  
  464.         camera->framerate = framerate;  
  465.           
  466.         ret = open_camera(camera);  
  467.         if(ret !=0 ){  
  468.             return ret;//open_fail!  
  469.         }  
  470.         ret = init_camera(camera);  
  471.         LOGE("init_camera return %d\n",ret);  
  472.           
  473.         if(ret == SUCCESSED){  
  474.             ret = startcapturing(camera);  
  475.             LOGE("startcapturing return %d\n",ret);  
  476.         }   
  477.   
  478.         if(ret != SUCCESSED){  
  479.             stopcapturing(camera);  
  480.             uninitdevice (camera);  
  481.             closedevice (camera);  
  482.             LOGE("device closed\n");      
  483.         }  
  484.            
  485.         return SUCCESSED;  
  486.   
  487. }  
  488.   
  489.   
  490. void   
  491. Java_com_ist_Camera_processCamera( JNIEnv* env,jclass thiz,jbyteArray buf){  
  492.                                           
  493.                                           
  494.     int size =0;  
  495.     readframeonce(camera,g_buffer2,&size);  
  496.     env->SetByteArrayRegion(buf, 0, size, (jbyte*)g_buffer2);  
  497.       
  498. }  
  499.   
  500. void   
  501. Java_com_ist_Camera_stopCamera(JNIEnv* env,jclass thiz){  
  502.   
  503.     stopcapturing (camera);  
  504.     uninitdevice (camera);  
  505.     closedevice (camera);  
  506.   
  507.     free(camera);;  
  508. }  
  509. #else  
  510.   
  511. int main(int argc,char *argv[]){  
  512.         if(argc<2){  
  513.             perror("argc <2\n");  
  514.             exit(-1);  
  515.         }  
  516.         int ret;  
  517.         camera = ( Camera *) malloc( sizeof( Camera ) );  
  518.         sprintf(camera->device_name,"/dev/video%d",atoi(argv[1]));  
  519.         camera->buffers = NULL;  
  520.         camera->width = 176;  
  521.         camera->height = 144;  
  522.         //camera->display_depth = 5; /* RGB24 */  
  523.           
  524.           
  525.           
  526.         ret = open_camera(camera);  
  527.         if(ret !=0 ){  
  528.             return ret;//open_fail!  
  529.         }  
  530.         ret = init_camera(camera);  
  531.         LOGE("init_camera return %d\n",ret);  
  532.           
  533.         if(ret == SUCCESSED){  
  534.             ret = startcapturing(camera);  
  535.             LOGE("startcapturing return %d\n",ret);  
  536.         }  
  537.   
  538.         if(ret != SUCCESSED){  
  539.             stopcapturing(camera);  
  540.             uninitdevice (camera);  
  541.             closedevice (camera);  
  542.             LOGE("device CLOSED!\n");     
  543.         }  
  544.           
  545.         //获取一帧  
  546.         char * buf;  
  547.         int size =0;  
  548.         int i= readframeonce(camera,buf,&size);  
  549.         //printf("get %d frame return \n",ret);  
  550.           
  551.         readframeonce(camera,buf,&size);  
  552.         readframeonce(camera,buf,&size);  
  553.         readframeonce(camera,buf,&size);  
  554.         readframeonce(camera,buf,&size);  
  555.   
  556.         stopcapturing(camera);  
  557.         sleep(1);  
  558.         uninitdevice (camera);  
  559.         sleep(1);  
  560.         closedevice (camera);  
  561.         free(camera);  
  562.         return SUCCESSED;  
  563.   
  564. }  
  565.   
  566. #endif  

这里定义了一个宏 LINUX ,来区分是JNI代码还是linux代码,测试的时候可以在本机编译的时候测试一下,比较方便一点。


这里主要 init_camera 函数 图像格式里面设置了分辨率和帧率。


这里需要注意的是摄像头支持的分辨率和帧率需要实现查看一下支持的格式,设置错误的参数会导致获取不到图像。

如命令:

v4l2-ctl --list-formats-ext  -d /dev/video0

Pixel Format: 'YUYV'
    Name        : YUV 4:2:2 (YUYV)
        Size: Discrete 640x480
            Interval: Discrete 0.033 s (30.000 fps)
            Interval: Discrete 0.040 s (25.000 fps)
            Interval: Discrete 0.050 s (20.000 fps)
            Interval: Discrete 0.067 s (15.000 fps)
            Interval: Discrete 0.100 s (10.000 fps)
            Interval: Discrete 0.200 s (5.000 fps)

..........


然后我们采用YUYV格式采集,基本上UVC摄像头都是支持这种格式的。

初始化的最后我们调用了initmmap函数,因为图像比较大,我们使用内存映射效率会高一些。

接着我们就可以调用startcapturing   和stopcapturing 设置和停止采集,获取一帧图像用readframe函数,最后uninitdevice释放资源,closedevice 关闭设备节点。

具体的流程可以参考main 函数的调用顺序即可。


这里因为采集的图像我是要用安卓自带的H264编码器进行编码,所以我通过yuyv_2_yuv420将图像转化了格式。

关于yuv图像的各种各类的打包格式,这里就进行介绍了。

这里比较关键的一点是不同yuv格式的存储空间和排列顺序都是不一样,注意一下分配的缓冲区大小就比较重要了。



至此,介绍完了在V4L2层根据分辨率和帧率获取了视频流。


最后附上代码下载地址:

http://download.csdn.NET/detail/zmnqazqaz/9543286

0 0
原创粉丝点击