IMX8 + Linux + 海康威视 实现获取摄像头 源码
时间: 2024-01-04 14:01:58 浏览: 263
获取海康威视摄像头的源码,需要先了解一下摄像头的型号和接口类型。如果是USB接口的摄像头,可以使用V4L2(UVC)驱动来进行操作。如果是CSI接口的摄像头,需要根据具体的型号和规格书来开发对应的驱动程序。以下是一个使用V4L2驱动的示例程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#define CAMERA_DEVICE "/dev/video0"
#define CAPTURE_FILE "frame.raw"
#define IMAGE_WIDTH 640
#define IMAGE_HEIGHT 480
struct buffer {
void *start;
size_t length;
};
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;
}
static void process_image(const void *p, int size)
{
FILE *fp;
fp = fopen(CAPTURE_FILE, "wb");
if (fp) {
fwrite(p, size, 1, fp);
fclose(fp);
}
}
static int read_frame(int fd, struct buffer *buffers)
{
struct v4l2_buffer buf;
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");
}
}
process_image(buffers[buf.index].start, buf.bytesused);
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
return 1;
}
static void mainloop(int fd, struct buffer *buffers)
{
unsigned int count;
count = 100;
while (count-- > 0) {
for (;;) {
fd_set fds;
struct timeval 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(fd, buffers))
break;
}
}
}
static void stop_capturing(int fd)
{
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
errno_exit("VIDIOC_STREAMOFF");
}
static void start_capturing(int fd, struct buffer *buffers)
{
unsigned int i;
enum v4l2_buf_type type;
for (i = 0; i < 4; ++i) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
errno_exit("VIDIOC_QUERYBUF");
buffers[i].length = buf.length;
buffers[i].start = mmap(NULL /* start anywhere */, buf.length,
PROT_READ | PROT_WRITE /* required */,
MAP_SHARED /* recommended */, fd, buf.m.offset);
if (MAP_FAILED == buffers[i].start)
errno_exit("mmap");
}
for (i = 0; i < 4; ++i) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
errno_exit("VIDIOC_STREAMON");
}
static void uninit_device(struct buffer *buffers)
{
unsigned int i;
for (i = 0; i < 4; ++i)
if (-1 == munmap(buffers[i].start, buffers[i].length))
errno_exit("munmap");
}
static void init_mmap(int fd, struct buffer **buffers, unsigned int *n_buffers)
{
struct v4l2_requestbuffers req;
CLEAR(req);
req.count = 4;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
if (EINVAL == errno) {
fprintf(stderr, "%s does not support "
"memory mapping\n", CAMERA_DEVICE);
exit(EXIT_FAILURE);
} else {
errno_exit("VIDIOC_REQBUFS");
}
}
if (req.count < 2) {
fprintf(stderr, "Insufficient buffer memory on %s\n",
CAMERA_DEVICE);
exit(EXIT_FAILURE);
}
*buffers = calloc(req.count, sizeof(**buffers));
if (!*buffers) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
for (*n_buffers = 0; *n_buffers < req.count; ++*n_buffers) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = *n_buffers;
if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
errno_exit("VIDIOC_QUERYBUF");
(*buffers)[*n_buffers].length = buf.length;
(*buffers)[*n_buffers].start = mmap(NULL /* start anywhere */, buf.length,
PROT_READ | PROT_WRITE /* required */,
MAP_SHARED /* recommended */, fd, buf.m.offset);
if (MAP_FAILED == (*buffers)[*n_buffers].start)
errno_exit("mmap");
}
}
static void init_device(int fd)
{
struct v4l2_capability cap;
struct v4l2_format fmt;
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
if (EINVAL == errno) {
fprintf(stderr, "%s is no V4L2 device\n", CAMERA_DEVICE);
exit(EXIT_FAILURE);
} else {
errno_exit("VIDIOC_QUERYCAP");
}
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
fprintf(stderr, "%s is no video capture device\n", CAMERA_DEVICE);
exit(EXIT_FAILURE);
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
fprintf(stderr, "%s does not support streaming i/o\n", CAMERA_DEVICE);
exit(EXIT_FAILURE);
}
CLEAR(fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = IMAGE_WIDTH;
fmt.fmt.pix.height = IMAGE_HEIGHT;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
errno_exit("VIDIOC_S_FMT");
if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV) {
fprintf(stderr, "Libv4l only supports V4L2_PIX_FMT_YUYV format\n");
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[])
{
struct buffer *buffers;
unsigned int n_buffers;
int fd = open(CAMERA_DEVICE, O_RDWR | O_NONBLOCK, 0);
if (-1 == fd) {
fprintf(stderr, "Cannot open %s\n", CAMERA_DEVICE);
exit(EXIT_FAILURE);
}
init_device(fd);
init_mmap(fd, &buffers, &n_buffers);
start_capturing(fd, buffers);
mainloop(fd, buffers);
stop_capturing(fd);
uninit_device(buffers);
close(fd);
return 0;
}
```
这个程序会从摄像头中获取视频流,并将每一帧保存到文件中。您需要根据自己的需求进行修改。需要注意的是,这个程序只适用于使用V4L2驱动的摄像头,如果您使用的是CSI接口的摄像头,需要开发对应的驱动程序。
阅读全文