Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

V4L2 Camera 驅動的 Capture 測試程式

網上找了一個測試程式,看過後發現是根據capture.c修改而來。測試步驟如下:

1. gcc -o capture_image capture_image.c

2. ctrl+alt+f1 切換到Ubuntu的控制台,之所以要切到控制台模式,是因為在圖形模式下看不到測試圖形,這可能和framebuffer的設定有關。

3. sudo modprobe vivi

4. sudo ./capture_image -d /dev/video0

這時可看到螢幕左上角有一個640x480大小的視窗,內容是不斷移動的彩色條格,持續5秒。

在Ubuntu下也可用cheese測試:

1. sudo apt-get install cheese

2. sudo modprobe vivi

3. 啟動cheese後,就能看到滾動的彩色條格。

附上測試程式:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h> 
#include <fcntl.h> 
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <linux/videodev2.h>
#include <linux/fb.h>
#define CLEAR(x) memset (&(x), 0, sizeof (x))
 
struct buffer {
    void * start;
    size_t length;
};
 
static char * dev_name = NULL;
static int fd = -1;
struct buffer * buffers = NULL;
static unsigned int n_buffers = 0;
static int time_in_sec_capture = 5;
static int fbfd = -1;
static struct fb_var_screeninfo vinfo;
static struct fb_fix_screeninfo finfo;
static char *fbp = NULL;
static long screensize = 0;
 
static void errno_exit (const char * s)
{
    fprintf (stderr, "%s error %d, %s/n", s, errno, strerror (errno));
    exit (EXIT_FAILURE);
}
 
static int xioctl (int fd, int request, void * arg)
{
    int r;
    do r = ioctl (fd, request, arg);
    while (-1 == r && EINTR == errno);
    return r;
}
 
inline int clip(int value, int min, int max) {
    return (value > max ? max : value < min ? min : value);
  }
 
static void process_image (const void * p){
  //ConvertYUVToRGB32
  unsigned char* in = (char*)p;
  int width = 640;
  int height = 480;
  int istride = 1280;
  int x, y, j;
  int y0, u, y1, v, r, g, b;
  long location = 0;
  for (y = 100; y < height + 100; ++y) {
      for (j = 0, x = 100; j < width * 2; j += 4, x += 2) {
        location = (x + vinfo.xoffset) * (vinfo.bits_per_pixel / 8) + (y + vinfo.yoffset) * finfo.line_length;
        y0 = in[j];
        u = in[j + 1] - 128;
        y1 = in[j + 2];
        v = in[j + 3] - 128;
        r = (298 * y0 + 409 * v + 128) >> 8;
        g = (298 * y0 - 100 * u - 208 * v + 128) >> 8;
        b = (298 * y0 + 516 * u + 128) >> 8;
        fbp[location + 0] = clip(b, 0, 255);
        fbp[location + 1] = clip(g, 0, 255);
        fbp[location + 2] = clip(r, 0, 255);
        fbp[location + 3] = 255;
        r = (298 * y1 + 409 * v + 128) >> 8;
        g = (298 * y1 - 100 * u - 208 * v + 128) >> 8;
        b = (298 * y1 + 516 * u + 128) >> 8;
        fbp[location + 4] = clip(b, 0, 255);
        fbp[location + 5] = clip(g, 0, 255);
        fbp[location + 6] = clip(r, 0, 255);
        fbp[location + 7] = 255;
      }
    in += istride;
  }
}

由於篇幅關係,此處僅列出核心的影像處理函數;程式的其餘部分(read_frame、run、start_capturing、init_mmap、init_device、main等)遵循標準V4L2 capture流程:以VIDIOC_REQBUFS請求緩衝區、VIDIOC_QUERYBUF與mmap映射、VIDIOC_QBUF排入佇列、VIDIOC_STREAMON啟動串流,再透過select等待並以VIDIOC_DQBUF取回畫面。

要特別注意的是,這個測試程式是針對vivi驅動硬編碼的,未必適合其他camera驅動。比如手邊的logitech stv06xx usb camera因為不支援640x480模式,程式第59、60行就會有問題;第348行if (-1 == xioctl (fd, VIDIOC_S_FMT, &fmt)) 其實應是一個協商過程:

343  fmt.fmt.pix.width = 640;   
344  fmt.fmt.pix.height = 480; 
345  fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; 
346  fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; 
這幾行只是應用的期望格式,驅動會據此選一個相近的格式回傳,應用最後的顯示處理要依回傳格式來調整,也就是process_image要做相應修改。

Copyright © Linux教程網 All Rights Reserved