V4L2 get RAW image data and save _camera

Source: Internet
Author: User
Tags assert sprintf
#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <sys/  ioctl.h> #include <sys/mman.h> #include <linux/videodev2.h> #define U8 unsigned char #define LOGD (...) Do {printf (__va_args__);p rintf ("\ n");} while (0) #define DBG (FMT, ARGS ...)
LOGD ("%s:%d," FMT, __function__, __line__, # #args); #define ASSERT (b) \ do \ {\ if (! b) \ {\ logd ("Error on%s:%d", __function__, __line__); \ return 0; \} \} while (0) #define V Ideo_device "/dev/video0" #define image_width 1920//sensor fixed output 1920*1080 image #define Image_height 1080 #define Image_size
(Image_width * image_height * 2) #define Buffer_count 5//apply for 5 buffer int cam_fd =-1;
struct V4l2_buffer Video_buffer[buffer_count];
u8* Video_buffer_ptr[buffer_count];

U8 Buf[image_size];
    int Cam_open () {cam_fd = open (Video_device, O_RDWR);//Open camera if (cam_fd >= 0) return 0;
else return-1; int Cam_close () {close (CAM_FD);/Off cameraHead return 0;

    int cam_select (int index) {int ret;
    int input = index;
RET = IOCTL (CAM_FD, Vidioc_s_input, &input);/Set input source return ret;
    int Cam_init () {int i;
    int ret;

    struct V4l2_format format;
    memset (&format, 0, sizeof (format));
	Format.type = v4l2_buf_type_video_capture;//frame type, for video capture device//format.fmt.pix.pixelformat = v4l2_pix_fmt_sbggr8; Format.fmt.pix.pixelformat = v4l2_pix_fmt_sgrbg10;//10bit RAW Format format.fmt.pix.width = image_width;//Resolution Format.fm
    T.pix.height = Image_height; RET = IOCTL (CAM_FD, VIDIOC_TRY_FMT, &format);/Set the current format if (Ret!= 0) {DBG ("IOCTL (VIDIOC_TRY_FMT) Faile
        D%d (%s), errno, Strerror (errno));
    return ret;
    } format.type = V4l2_buf_type_video_capture; RET = IOCTL (CAM_FD, VIDIOC_S_FMT, &format);/Set the current format if (Ret!= 0) {DBG ("IOCTL (VIDIOC_S_FMT) failed%d
        (%s) ", errno, Strerror (errno));
    return ret; } struct V4l2_requestbuffers req; Req.count = buffer_count;//Buffer frame Number Req.type = v4l2_buf_type_video_capture;//buffer frame data format req.memory = v4l2_memory_mmap;// Memory-mapped method ret = IOCTL (CAM_FD, Vidioc_reqbufs, &req);//Request buffer if (Ret!= 0) {DBG ("IOCTL" vidioc_reqbufs
        ) failed%d (%s), errno, Strerror (errno));
    return ret;
    } DBG ("Req.count:%d", req.count);
        if (Req.count < Buffer_count) {DBG ("request BUFFER failed");
    return ret;
    } struct V4l2_buffer buffer;
    memset (&buffer, 0, sizeof (buffer));
    Buffer.type = Req.type;
    Buffer.memory = V4l2_memory_mmap;
        for (i=0; i<req.count; i++) {buffer.index = i; RET = IOCTL (CAM_FD, VIDIOC_QUERYBUF, &buffer)//Get buffer frame address if (Ret!= 0) {DBG ("IOCTL" VIDIOC
            _QUERYBUF) failed%d (%s), errno, Strerror (errno));
        return ret;
        } DBG ("Buffer.length:%d", buffer.length);
     DBG ("Buffer.m.offset:%d", buffer.m.offset);   Video_buffer_ptr[i] = (u8*) mmap (NULL, Buffer.length, prot_read|
            Prot_write, map_shared, CAM_FD, buffer.m.offset);//Memory Map if (video_buffer_ptr[i] = = map_failed) {
            DBG ("Mmap () failed%d (%s)", errno, Strerror (errno));
        return-1;
        } buffer.type = V4l2_buf_type_video_capture;
        Buffer.memory = V4l2_memory_mmap;
        Buffer.index = i; RET = IOCTL (CAM_FD, Vidioc_qbuf, &buffer);//Put the buffer frame into the queue if (Ret!= 0) {DBG ("IOCTL" VIDIOC_QB
            UF) failed%d (%s) ", errno, Strerror (errno));
        return ret;
    an int buffer_type = v4l2_buf_type_video_capture;  RET = IOCTL (CAM_FD, Vidioc_streamon, &buffer_type);/Start Data flow if (Ret!= 0) {DBG ("IOCTL (Vidioc_streamon)
        Failed%d (%s), errno, Strerror (errno));
    return ret;

    } DBG ("Cam init done.");
return 0;
    int Cam_get_image (u8* out_buffer, int out_buffer_size) {int ret; struct V4l2_buffer buFfer;
    memset (&buffer, 0, sizeof (buffer));
    Buffer.type = v4l2_buf_type_video_capture;
    Buffer.memory = V4l2_memory_mmap;
    Buffer.index = Buffer_count; RET = IOCTL (CAM_FD, Vidioc_dqbuf, &buffer);//Remove a frame from the queue (ret!= 0) {DBG ("IOCTL (VIDIOC_DQBUF) failed")
        %d (%s), errno, Strerror (errno));
    return ret; } if (Buffer.index < 0 | | Buffer.index >= buffer_count) {DBG ("Invalid buffer index:%d", buffer.in
        DEX);
    return ret;
    } DBG ("Dequeue done, Index:%d", buffer.index);

    memcpy (Out_buffer, Video_buffer_ptr[buffer.index], image_size);/buffer frame data copy out DBG ("copy done."); RET = IOCTL (CAM_FD, Vidioc_qbuf, &buffer);//buffer frame into queue if (ret!= 0) {DBG ("IOCTL (VIDIOC_QBUF) failed%d" (
        %s) ", errno, Strerror (errno));
    return ret;

    } DBG ("Enqueue done.");
return 0;
    int main () {int i;

    int ret;
    ret = Cam_open ();

    ASSERT (ret==0);
    ret = cam_select (0); AsSERT (ret==0);
    ret = Cam_init ();

    ASSERT (ret==0);
    int count = 0;
        while (1) {ret = Cam_get_image (buf, image_size);

        ASSERT (ret==0);
        Char tmp[64] = {"---\ n"};
        For (i=0 i<16; i++) sprintf (&tmp[strlen (TMP)], "%02x", Buf[i]);

        LOGD ("%s", TMP);
        Char filename[32];
        sprintf (filename, "/sdcard/%05d.raw", count++); int fd = open (filename,o_wronly|
            o_creat,00700)//Save image Data if (FD >= 0) {write (FD, buf, image_size);
        Close (FD);
        else {logd ("open () failed:%d (%s)", errno, Strerror (errno));
    ret = Cam_close ();

    ASSERT (ret==0);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.