Linux camera Programming

Source: Internet
Author: User
Linux camera programming-Linux general technology-Linux programming and kernel information. The following is a detailed description. In linux, the camera program is developed mainly using video4linux, and qt is used for the interface. We started to prepare frame buffer for Direct screen writing, but the effect was not very good, later, I used qt to do it. In this way, the effect is quite good, and the frame rate can also be up to 30fps. I used v4l to program and master its api, the most important thing to improve the frame rate is to use memory ing. In fact, memory ing is used for both qt and frame buffer. Only in this way can a high frame rate be achieved, however, pay attention to resource utilization issues. munmap is required after mmap. frame buffer is a very interesting thing, especially the driver design.
CODE: 1. open device:
Video_dev = open ("\ dev \ video0", O_RDWR ));
2. get the information of the video device
Struct video_capability video_cap;
Memset (& video_cap, 0, sizeof (video_cap ));
If (ioctl (video_dev, VIDIOCGCAP, & video_cap) =-1)
{
Perror ("cann' t get the information of the video device ");
Close (video_dev );
Exit (1 );
}
3. get the information of the channels
Struct video_channel video_chan;
Memset (& video_chan, 0, sizeof (video_chan ));
For (channel = 0; channel <video_cap.channels; channel ++)
{
Video_chan.channel = channel;
If (ioctl (video_dev, VIDIOCGCHAN, & video_chan) =-1)
{
Perror ("cann' t get the information of the channels ");
Close (video_dev );
Exit (3 );
}
If (video_chan.type = VIDEO_TYPE_ TV)
{
# Ifdef DEBUG
Printf ("NO. % d channel is % s, type is TV! \ N ", channel, video_chan.name );
# Endif
}
If (video_chan.type = VIDEO_TYPE_CAMERA)
{
If (ioctl (video_dev, VIDIOCSCHAN, & video_chan) =-1)
{
Perror ("cann' t set the camera channel! ");
Close (video_dev );
Exit (4 );
}
}
}
4. get the capture attributes
Struct video_picture video_pic;
Memset (& video_pic, 0, sizeof (video_pic ));
If (ioctl (video_dev, VIDIOCGPICT, & video_pic) =-1)
{
Perror ("cann' t get the picture attributes of the device! ");
Close (video_dev );
Exit (5 );
}
5. set capture attributes
Video_pic.palette = VIDEO_PALETTE _ *****; // different camera has different palette
Video_pic.brightness = 65535;
Video_pic.colour = 0;
If (ioctl (video_dev, VIDIOCSPICT, & video_pic) =-1)
{
Perror ("cann' t set the picture attributs! ");
Close (video_dev );
Exit (6 );
}
6. mmap the device
Struct video_mbuf mbuf;
Unsigned char * frames;
Memset (& mbuf, 0, sizeof (mbuf ));
If (ioctl (video_dev, VIDIOCGMBUF, & mbuf) =-1)
{
Perror ("cann' t get the buffer information of the video device! ");
Close (video_dev );
Exit (7 );
}
Frames = (unsigned char *): mmap (0, mbuf. size, PROT_READ | PROT_WRITE, MAP_SHARED, video_dev, 0 );
If (! Frames | frames = (unsigned char *) (long) (-1 ))
{
Perror ("The device cann' t be memory mapped! ");
Close (video_dev );
Exit (8 );
}
7. capture
If (ioctl (video_dev, VIDIOCMCAPTURE, & mmap) =-1)
{
Perror ("Capture failed! ");
Close (video_dev );
Exit (9 );
}

Else
{
// Display or save the picture file
}

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.