V4L2 Camera 驱动 Capture测试程序(一)

2014-11-24 08:47:24 来源: 作者: 浏览: 0

在网上找了一个测试程序, 看了看,是根据capture.c修改的。测试步骤如下


1. gcc -o capture_image capture_image.c


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


2. 启动 cheese后,就可以看到滚动的彩色条格


附上测试程序


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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
1;
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;
}
}

static int read_frame (void)
{
struct v4l2_buffer buf;
unsigned int i;

CLEAR (buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;

if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) {
switch (errno) {
case EAGAIN:
return 0;
case EIO:



default:
errno_exit ("VIDIOC_DQBUF");
}
}

assert (buf.index < n_buffers);
printf("v4l2_pix_format->field(%d)/n", buf.field);
//assert (buf.field ==V4L2_FIELD_NONE);
process_image (buffers[buf.index].start);
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
errno_exit ("VIDIOC_QBUF");

return 1;
}

static void run (void)
{
unsigned int count;
int frames;
frames = 30 * time_in_sec_capture;

while (frames-- > 0) {
for (;;) {
fd_set fds;
struct timeva l tv;
int r;
FD_ZERO (&fds);
FD_SET (fd, &fds);


tv.tv_sec = 2;
tv.tv_usec = 0;

r = select (fd + 1, &fds, NULL, NULL, &tv);

if (-1 == r) {
if (EINTR == errno)
continue;
errno_exit ("select");
}

if (0 == r) {
fprintf (stderr, "select timeout/n");
exit (EXIT_FAILURE);
}

if (read_frame ())
break;

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: