V4L video collection problems

Source: Internet
Author: User
V4L video collection-Linux general technology-Linux technology and application information. For more information, see the following section. Hello everyone, now we have encountered a problem in design, that is, the V4L program used on the Internet can only collect one image, but we want to collect multiple images consecutively so that we can convert them into video formats, A timer is set, but only one jpg image can be collected. I will post the program below. Please help solve this problem. (Some others wrote something like spcaview.) I personally think it is a storage address problem, but I don't know how to modify it. In other cases, the ubuntu10.10 system, the ZC301 camera, is only capable of implementing functions on a PC.

I tried this source code, but it was unsuccessful. Have you ever used this? Can you tell me how to use it?

# Include
# Include "v4l. h"
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define norm VIDEO_MODE_NTSC
# Define DEFAULT_FILE_NAME "picture"
# Define DEFAULT_DEVICE "/dev/video0"
Int v4l_open (char * dev, v4ldevice * vd)
{
If (! Dev)
Dev = DEFAULT_DEVICE;

If (vd-> fd = open (dev, O_RDWR | O_NONBLOCK, 10705) <0) // open in non-blocking mode
{
Return-1;
} Else {
Printf ("open success !!!! \ N ");
}

If (v4l_get_capability (vd) <0)
{Return-1;} else {
Printf ("\ n =========== Get struct video_capability =========================\ n ");
Printf (vd-> capability. name); printf (", Type: % d \ n", vd-> capability. type );
Printf ("Maxwidth: % d, Maxheight: % d \ n", vd-> capability. maxwidth, vd-> capability. maxheight );
Printf ("Minwidth: % d, Minheight: % d \ n", vd-> capability. minwidth, vd-> capability. minheight );
Printf ("Channels: % d, Audios: % d \ n", vd-> capability. channels, vd-> capability. audios );}
If (v4l_get_picture (vd) <0)
{Return-1;} else {
Printf ("\ n ================= Get struct video_picture1 =======================\ n" ); // The image information obtained before modification
Printf ("Brightness: % d, Hue: % d, Colour: % d \ n", vd-> picture. brightness, vd-> picture. hue, vd-> picture. color );
Printf ("Contrast: % d, Whiteness: % d (Black and white only) \ n ",
Vd-> picture. contrast, vd-> picture. whiteness );
Printf ("Capture depth: % d, Palette: % d \ n", vd-> picture. depth, vd-> picture. palette );
}
// Return 0; gcc version: 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
Printf ("\ n =================== Set vpic. palette to VIDEO_PALETTE_YUV420P, and picture. depth to 16. =========\ n ");
Vd-> picture. palette = VIDEO_PALETTE_YUV420P; // set the image format in the color palette to correspond to the 107 rows in videodev. h.
Vd-> picture. depth = 14; // sets the pixel depth.
// Vd-> picture. hue = 25854; // tone
// Vd-> picture. color = 25854 ;//
// Vd-> picture. brightness = 22869; // brightness
/Vd-> picture. contrast = 22768; // contrast
// Vd-> picture. whiteness = 29321; // color
If (ioctl (vd-> fd, VIDIOCSPICT, & vd-> picture) =-1) // sets image features
{Fprintf (stderr, "Unable to find a supported capture format. \ n ");
Close (vd-> fd );
Exit (1 );
} Else {
Printf ("\ n =============== Get struct video_picture2 =========================\ n" ); // information obtained after modification
Printf ("Brightness: % d, Hue: % d, Colour: % d \ n", vd-> picture. brightness, vd-> picture. hue, vd-> picture. color );
Printf ("Contrast: % d, Whiteness: % d (Black and white only) \ n ",
Vd-> picture. contrast, vd-> picture. whiteness );
Printf ("Capture depth: % d, Palette: % d \ n", vd-> picture. depth, vd-> picture. palette );
}


Return 0;
}

Int v4l_get_capability (v4ldevice * vd)
{
If (ioctl (vd-> fd, VIDIOCGCAP, & (vd-> capability) <0)
{
Perror ("v4l_get_capability :");
Return-1;
}

Return 0;

}

Int v4l_get_picture (v4ldevice * vd)
{
If (ioctl (vd-> fd, VIDIOCGPICT, & (vd-> picture) <0) // obtain the image feature data structure
{
Perror ("v4l_get_picture ");
Return-1;
}

Return 0;

}

Int v4l_grab_init (v4ldevice * vd, int width, int height) // initialize
{
Vd-> mmap. width = width;
Vd-> mmap. height = height;
Vd-> mmap. format = vd-> picture. palette;
Vd-> frame = 0;
Vd-> framestat [0] = 0;
Vd-> framestat [1] = 0;
Return 0;
}

Int v4l_mmap_init (v4ldevice * vd)
{
If (v4l_get_mbuf (vd) <0)
Return-1;
If (vd-> map = mmap (0, vd-> mbuf. size, PROT_READ | PROT_WRITE, MAP_SHARED, vd-> fd, 0) <0)
{
Return-1;
}
Return 0;
}

Int v4l_get_mbuf (v4ldevice * vd) // query the actual number of available caches
{
If (ioctl (vd-> fd, VIDIOCGMBUF, & (vd-> mbuf) <0)
{
Perror ("v4l_get_mbuf :");
Return-1;
}
Printf ("size = % d \ n", vd-> mbuf. size );
Return 0;
}

Int v4l_grab_start (v4ldevice * vd, int frame) // start
{
Vd-> mmap. frame = frame;
If (ioctl (vd-> fd, VIDIOCMCAPTURE, & (vd-> mmap) <0)
{
Exit (-1 );
Return-1;
}
Vd-> framestat [frame] = 1;
Return 0;
}
Int v4l_grab_sync (v4ldevice * vd, int frame)
{
If (ioctl (vd-> fd, VIDIOCSYNC, & frame) <0)
{
Return-1;
}
Vd-> framestat [frame] = 0;
Return 0;
}

Unsigned char * v4l_get_address (v4ldevice * vd)
{
Return (vd-> map + vd-> mbuf. offsets [vd-> frame]);
}
Int v4l_close (v4ldevice * vd)
{
Close (vd-> fd );
Return 0;
}



Int main ()
{
Char * buffer = NULL;
V4ldevice VD;
V4ldevice * vd = & VD;

Int frame = 0;
Int f_d;
Int loop_count = 100;
Int sleep_time_milsec = 1000; // sleep for 1 second
Char file_name [2, 100];

If (0 = v4l_open ("/dev/video0", vd) // open the device
Printf ("open success! \ N ");
Else
Printf ("open failure \ n ");
// Set (vd );
/* If (0 = v4l_set_norm (vd, norm ))
Printf ("set_norm success \ n ");
Else
Printf ("set_norm failure \ n ");*/
If (0 = v4l_grab_init (vd, CIF_WIDTH, CIF_HEIGHT) // initialize the device and define the size of the obtained Image
Printf ("init success! \ N ");
Else
Printf ("init failure \ n ");
If (0 = v4l_mmap_init (vd) // memory ing
Printf ("memory map success! \ N ");
Else
Printf ("memory map failure \ n ");

While (loop_count)
{
If (0 = v4l_grab_start (vd, frame) // start to obtain the image
Printf ("get picture success! \ N ");
Else
{
Printf ("get picture failure \ n ");
Break;
}

V4l_grab_sync (vd, frame); // wait for a frame to be uploaded

Buffer = (char *) v4l_get_address (vd); // obtain the address of this frame.
Printf ("img address % p \ n", buffer );

Sprintf (file_name, "% s-% d", DEFAULT_FILE_NAME, frame );
F_d = open (file_name, O_RDWR | O_CREAT, 0666); // obtain the file descriptor. open the piture file and return its descriptor DEFAULT_FILE_NAME, which is defined earlier.
Write (f_d, buffer, CIF_WIDTH * 3 * CIF_HEIGHT); // save it to the file and write it to the piture file.
Close (f_d );

Sleep (sleep_time_milsec );
Loop_count --;
Frame ++;
}

V4l_close (vd );
Return 0;

}


Cainiao online waiting ....
Related Article

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.