Linux系統下用Frame Buffer來做網路攝影機編程

來源:互聯網
上載者:User

在Linux下開發網路攝影機的程式,主要用的是
video4linux來做的,介面用qt來實現,開始準備用frame
buffer來直接寫屏但是效果不怎麼好,後來就用qt來做了,這樣用起來效果還蠻好的,幀率也可以,可以上到30fps;運用v4l來編程主要掌握其
api,要提高幀率最重要的是用到記憶體映射,其實用qt和frame
buffer的時候都要用到記憶體映射來做,只有這樣才可以達到較高的幀率,不過要注意資源的利用問題.mmap後一定要munmap.對於frame
buffer是很有意思的一個東西,特別是驅動的設計.

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.