本帖最后由 无聊 于 2012-5-24 09:04 编辑
亚瑟王 发表于 2012-5-18 18:54
分辨率呢?
void VideoCapture::setupCamera( QSize size )
{
// Clear important variables.
frames = 0;
currentFrame = 0;
width = 640;
height = 480;
caps.minwidth = width;
caps.minheight = height;
caps.maxwidth = width;
caps.maxheight = height;
// Open the video device.
fd = open( VIDEO_DEVICE, O_RDWR );
if ( fd == -1 ) {
qDebug( "%s: %s", VIDEO_DEVICE, strerror( errno ) );
return;
}
// Get the device's current capabilities.
memset( &caps, 0, sizeof( caps ) );
if ( ioctl( fd, VIDIOCGCAP, &caps ) < 0 ) {
qDebug( "%s: could not retrieve the video capabilities",
VIDEO_DEVICE );
close( fd );
fd = -1;
return;
}
// Change the channel to the first-connected camera, skipping TV inputs.
// If there are multiple cameras, this may need to be modified.
int chan;
struct video_channel chanInfo;
qDebug( "available video capture inputs:" );
for ( chan = 0; chan < caps.channels; ++chan ) {
chanInfo.channel = chan;
if ( ioctl( fd, VIDIOCGCHAN, &chanInfo ) >= 0 ) {
if ( chanInfo.type == VIDEO_TYPE_CAMERA )
qDebug( " %s (camera)", chanInfo.name );
else if ( chanInfo.type == VIDEO_TYPE_TV )
qDebug( " %s (tv)", chanInfo.name );
else
qDebug( " %s (unknown)", chanInfo.name );
}
}
for ( chan = 0; chan < caps.channels; ++chan ) {
chanInfo.channel = chan;
if ( ioctl( fd, VIDIOCGCHAN, &chanInfo ) >= 0 ) {
if ( chanInfo.type == VIDEO_TYPE_CAMERA ) {
qDebug( "selecting camera on input %s", chanInfo.name );
if ( ioctl( fd, VIDIOCSCHAN, &chan ) < 0 ) {
qDebug( "%s: could not set the channel", VIDEO_DEVICE );
}
break;
}
}
}
// Set the desired picture mode to RGB32.
struct video_picture pict;
memset( &pict, 0, sizeof( pict ) );
ioctl( fd, VIDIOCGPICT, &pict );
pict.palette = VIDEO_PALETTE_RGB32;
if ( ioctl( fd, VIDIOCSPICT, &pict ) < 0 ) {
qDebug( "%s: could not set the picture mode", VIDEO_DEVICE );
close( fd );
fd = -1;
return;
}
// Determine the capture size to use. Zero indicates "preview mode".
if ( size.width() == 0 ) {
size = QSize( caps.minwidth, caps.minheight );
}
// Get the current capture window.
struct video_window wind;
memset( &wind, 0, sizeof( wind ) );
ioctl( fd, VIDIOCGWIN, &wind );
// Adjust the capture size to match the camera's aspect ratio.
if ( caps.maxwidth > 0 && caps.maxheight > 0 ) {
if ( size.width() > size.height() ) {
size = QSize( size.height() * caps.maxwidth / caps.maxheight,
size.height() );
} else {
size = QSize( size.width(),
size.width() * caps.maxheight / caps.maxwidth );
}
}
// Set the new capture window.
wind.x = 0;
wind.y = 0;
wind.width = size.width();
wind.height = size.height();
if ( ioctl( fd, VIDIOCSWIN, &wind ) < 0 ) {
qDebug( "%s: could not set the capture window", VIDEO_DEVICE );
}
// Re-read the capture window, to see what it was adjusted to.
ioctl( fd, VIDIOCGWIN, &wind );
width = wind.width;
height = wind.height;
// Enable mmap-based access to the camera.
memset( &mbuf, 0, sizeof( mbuf ) );
if ( ioctl( fd, VIDIOCGMBUF, &mbuf ) < 0 ) {
qDebug( "%s: mmap-based camera access is not available", VIDEO_DEVICE );
close( fd );
fd = -1;
return;
}
// Mmap the designated memory region.
frames = (unsigned char *)mmap( 0, mbuf.size, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0 );
if ( !frames || frames == (unsigned char *)(long)(-1) ) {
qDebug( "%s: could not mmap the device", VIDEO_DEVICE );
close( fd );
fd = -1;
return;
}
// Start capturing of the first frame.
struct video_mmap capture;
currentFrame = 0;
capture.frame = currentFrame;
capture.width = width;
capture.height = height;
capture.format = VIDEO_PALETTE_RGB32;
ioctl( fd, VIDIOCMCAPTURE, &capture );
}
应该是这段源码的缘故吧?
|