Camera Image Acquisition Program

Source: Internet
Author: User
Camera Image Acquisition Program-general Linux technology-Linux programming and kernel information. The following is a detailed description. [I = s] This post was last edited by sydnash at, September 13 ,.

The following is a camera image acquisition program I wrote. This program runs vd-> fd = open ("/dev/video0", O_RDWR). A segment error occurs. Let's take a look, I really don't know why the wrong address segment is read here
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Typedef struct v4l_struct {
Int fd;

Struct video_capability capability;

Struct video_channel [4];

Struct video_picture picture;

Struct video_window window;

Struct video_capture capture;

Struct video_buffer buffer;

Struct video_mmap mmap;

Struct video_mbuf mbuf;

Unsigned char * map;

Int frame;

Int framestat [2];

} Device;
/* Function name: v4l_get_capability

Function: gets device properties.

Input: vd

Output: None

Return Value:-1 ---- 0 failed ---- success
*/
/* Int v4lopen (char * name, device * vd ){
If (vd-> fd = open ("/dev/video0", O_RDWR) <0 ){
Perror ("v4lopen ");
Return-1;
}
Return 0;
}*/
/* Function name: v4lgetcapability

Function: gets device properties.

Input: vd

Output: None

Return Value:-1 ---- 0 failed ---- success

*/

Int v4lgetcapability (device * vd ){

If (ioctl (vd-> fd, VIDIOCGCAP, & (vd-> capability) <0 ){

Perror ("v4lgetcapability ");

Return-1;

}

Return 0;

}
/* Function name: v4lgetpicture

Function: obtains image window information.

Input: vd

Output: None

Return Value:-1 ---- 0 failed ---- success

*/
Int v4lgetpicture (device * vd ){
If (ioctl (vd-> fd, VIDIOCGPICT, & (vd-> picture) <0 ){
Perror ("v4lgetpicture ");
Return-1;
}
Return 0;
}
/* Function name: v4lgetmbuf

Function: obtains the image size.

Input: vd

Output: None

Return Value:-1 ---- 0 failed ---- success

*/
Int v4lgetmbuf (device * vd ){
If (ioctl (vd-> fd, VIDIOCGMBUF, & (vd-> mbuf) <0 ){
Perror ("v4lgetmbuf: VIDIOCGMBUF ");
Return-1;
}
Return 0;
}
/* Function name: v4lgrabinit

Function: Initialize the device and define the size of the obtained image.

Input: vd, width, height

Output: None

Return Value:-1 ---- 0 failed ---- success

*/
Int v4lgrabinit (device * vd, int width, int height ){
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;
}
/* Function name: v4lmmap

Function: Memory ing

Input: vd

Output: None

Return Value:-1 ---- 0 failed ---- success

*/
Int v4lmmap (device * vd ){
If (v4lgetmbuf (vd) <0 ){
Return-1;
}
If (vd-> map = (char *) mmap (0, vd-> mbuf. size, PROT_READ |
PROT_WRITE, MAP_SHARED, vd-> fd, 0) <0 ){
Return-1;
}
Return 0;
}
/* Function name: v4lgrabstart

Function: Get the image

Input: vd, frame

Output: None

Return Value:-1 ---- 0 failed ---- success

*/
Int v4lgrabstart (device * vd, int frame ){
Vd-> mmap. frame = frame;
If (ioctl (vd-> fd, VIDIOCMCAPTURE, & (vd-> mmap) <0 ){
Return-1;
}
Vd-> framestat [frame] = 1;
Return 0;
}
/* Function name: v4lgetaddress

Function: Calculate the first address of the image memory.

Input: vd, frame

Output: None

Return Value:-1 ---- 0 failed ---- success

*/
Unsigned char * v4lgetaddress (device * vd, int frame ){
Vd-> frame = frame;
Return (vd-> map + vd-> mbuf. offsets [vd-> frame]);
}
/* Function name: v4lsync

Function: wait until the message is sent ?? Br/>
Input: vd, frame

Output: None

Return Value:-1 ---- 0 failed ---- success

*/
Int v4lsync (device * vd, int frame ){
If (ioctl (vd-> fd, VIDIOCSYNC, & frame) <0 ){
Return-1;
}
Vd-> framestat [frame] = 0;
Return 0;
}
/* Function name: v4lcapture
Function: get images
Input: vd, frame
Output: None
Return Value:-1 ----- 0 failed ---- success
*/
Int v4lcapture (device * vd, int frame ){
Vd-> mmap. frame = frame;
If (ioctl (vd-> fd, VIDIOCMCAPTURE, & (vd-> mmap) <0 ){
Return-1;
}
Vd-> framestat [frame] = 1;
Return 0;
}
Int main (int argc, char * argv []) {
Device * vd;
Char * buffer;
Int width = 640;
Int height = 480;
Int frame = 0;
/* If (v4lopen ("/dev/video0", vd) <0 ){
Perror ("v4lopen ");
Exit (1 );
} // Open the device */
Vd-> fd = open ("/dev/video0", O_RDWR );

If (v4lgetcapability (vd) <0 ){
Perror ("v4lgetcapability ");
Exit (1 );
} // Get device Properties
If (v4lgetpicture (vd) <0 ){
Perror ("v4lgetpicture ");
Exit (1 );
} // Obtain the image window information
If (v4lgrabinit (vd, width, height) <0 ){
Perror ("v4lgrabinit ");
Exit (1 );
} // Initialize the device and define the size of the obtained Image
If (v4lmmap (vd) <0 ){
Perror ("v4lmmap ");
Exit (1 );
} // Memory ing
If (v4lgrabstart (vd, frame) <0 ){
Perror ("v4lgrabstart ");
Exit (1 );
} // Start to obtain the image
While (1 ){
While (v4lsync (vd, frame )){
Perror ("waite ");
} // Wait for a frame to be uploaded
Buffer = (char *) v4lgetaddress (vd, frame); // obtain the address of this frame
// Buffer provides the first address of the image. You can choose to display or save the image and transfer it ......
// The image size is width * height * 3. It is best to create a new thread or process.
Int fd;
Fd = open ("1.jpeg", O_RDWR );
Write (fd, buffer, 640*480*3 );
Close (fd );
Frame = (frame + 1) % 2; // frame of the next frame
If (v4lcapture (vd, frame) <0 ){
Perror ("v4lcapture ");
Exit (1 );
} // Obtain the next frame
}
If (close (vd-> fd) <0 ){
Perror ("close ");
Exit (1 );
}
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.