v4l2开启摄像头、抓取图像代码实战

先上核心代码:

#include "v4l2Camera.h"
#include <sys/stat.h>
#include <sys/mman.h>
#include <sstream>
#include <assert.h>
#include <opencv2/opencv.hpp>

namespace v4l2_camera {

#define CLEAR(x) memset(&(x), 0, sizeof(x))

int xioctl(int fh, int request, void *arg) {
    int ret_value;
    do {
        ret_value = ioctl(fh, request, arg);
    } while (ret_value == -1 && errno == EINTR);
    return ret_value;
}

int print_error_detail(const char* err_msg) {
    std::cerr << err_msg << " error occurred! errno: " << errno << ", error description: " << strerror(errno) << std::endl;
    return -1;
}

void Camera::process_image(void* p_data, int size) {
    std::vector<unsigned char> data_vec((unsigned char*)p_data + 0, (unsigned char*)p_data + size);

    std::cout << "size in bytes: " << size << std::endl;
    auto image = cv::imdecode(data_vec, cv::IMREAD_GRAYSCALE);

    //TODO LG ADD
    std::cout << "rows, cols: " << image.rows << " , " << image.cols << " , " << image.channels() << std::endl;
    // cv::Mat image(image_height, image_width, CV_8UC1, p_data);
    cv::imshow("image" ,image);
    cv::waitKey(1);
}

int Camera::read_frame() {
    struct v4l2_buffer buf;
    CLEAR(buf);
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory = V4L2_MEMORY_MMAP;
    
    //get image buffer from output queue
    if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
        switch (errno) {
            case EAGAIN:
                return 0;
            case EIO:payload
                /* Could ignore EIO, see spec. */
                /* fall through */
            default:
                return print_error_detail("VIDIOC_DQBUF");
        }
    }
    assert(buf.index < n_buffers);
    // process_image(buffers[buf.index].start, buf.byt
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值