ºìÁªLinuxÃÅ»§
Linux°ïÖú

Linux²é¿´ÉãÏñÍ·²ÎÊýµÄС³ÌÐò

·¢²¼Ê±¼ä:2007-06-07 00:41:44À´Ô´:ºìÁª×÷Õß:Database
ÔÚlinuxÏÂ×öºÍvideoÏà¹ØµÄÊÂÇé±Ø¶¨ÒªÉæ¼°µ½video for linux²ã£¬¼òд×öv4l£¬Õâ¸ö³ÌÐò¾ÍÊÇ»ùÓÚÕâ¸öAPI²ãдµÄ¡£Ö®ËùÒÔдÕâ¸öС³ÌÐòÖ÷ÒªÊÇÒòΪͨ¹ýlsusbÃüÁîÖ»ÄÜ¿´µ½ÀàËÆvenderID,productIDÖ®ÀàµÄ²ÎÊý£¬¶ø¿´²»µ½ÆäËûµÄÉæ¼°ÉãÏñÍ·±¾ÉíµÄһЩ²ÎÊý¡£³ÌÐòÖÐÖ÷ҪʹÓõ½µÄÊÇioctl()ϵͳµ÷Óã¬ioctl()ÊÇI/O²Ù×÷µÄÔÓ»õÏ䣬ºÜ¶àÊÂÇ鶼ҪÒÀ¿¿ËüÀ´Íê³É¡£³ýÁËÉãÏñÍ·£¬ÊÓƵ²É¼¯¿¨Ö®ÀàµÄÓëvideoÏà¹ØµÄ²Ù×÷ÆäʵҲ¶¼Óë´ËÀàËÆ¡£ÒÔϸø³ö³ÌÐòµÄÔ´´úÂ룬ÒÔ¹©ÒÔºó»áÉæ¼°µ½Õâ·½ÃæµÄÐֵܲο¼¡£³ÌÐò±¾ÉíµÄ¹¦ÄÜÆäʵºÜ¼òµ¥£¬ËùÒÔÒ²²»Ò»Ò»½â˵¡£ÀïÃæÖ»Éæ¼°µ½ÉÙÁ¿*nixÏÂC±à³ÌµÄ֪ʶ£¬ÈôÓÐÒÉÎÊÇë²ÎÔÄÏà¹Ø×ÊÁÏ¡£

/* Program: qc_test.c
* Author: Rockins
* Date: Mar 3,2005
* Compile command: gcc -o qc_test qc_test.c
* IMPORTANT:You can freely distribute this programme under LGPL License.
* If you haven't had a copy of LGPL,you can get it from FSF organization.
*/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define MAXLINE 256

char errMsg[MAXLINE];

char* deviceName = "/dev/video0";
int deviceHandle = 0;
struct video_capability capability;
struct video_channel queryChannel;
struct video_window queryWindow;
struct video_picture queryPicture;

////////////////////////////////////////////////////////////////////////////////////////////
//function:sig_brk(),signal handle,close deviceHandle then exit,if press CTL-C or CTL-\
//param:signo,the signal number
//return:void
////////////////////////////////////////////////////////////////////////////////////////////
static void
sig_brk(int signo)
{
if (signo == SIGINT)
{
fprintf(stderr,"terminated by CTL-C\n");
close(deviceHandle);
exit(-1);
}
if (signo == SIGQUIT)
{
fprintf(stderr,"terminated by CTL-\\\n");
close(deviceHandle);
exit(-1);
}
return ;
}


////////////////////////////////////////////////////////////////////
//function:err_exit(),called when an error occured
//param:char* err_msg
//return:-1
////////////////////////////////////////////////////////////////////
static void
err_exit(char *err_msg)
{
printf(err_msg);
if (errno != 0)
perror("error:");
close(deviceHandle);
exit(-1);
}


/////////////////////////////////////////////////////////////////////////////////////////////
//function:main(),do everything
//return:0 if success,else -1
////////////////////////////////////////////////////////////////////////////////////////////
int
main(int argc,char *argv[])
{

///////////////////////////////////////////////////////////////////////////////////////////
//HERE open the QuickCam device
///////////////////////////////////////////////////////////////////////////////////////////
deviceHandle = open (deviceName, O_RDWR);
if (deviceHandle == -1)
{ //cannot open the "/dev/video0"
sprintf(errMsg,"cannot open %s:",deviceName);
err_exit(errMsg);
}
printf("open %s success.\n",deviceName);

/////////////////////////////////////////////////////////////////////////////////////////////
//HERE register signal handle for CTL-C and CTL-\
/////////////////////////////////////////////////////////////////////////////////////////////
if (signal(SIGINT,sig_brk) == SIG_ERR)
{
sprintf(errMsg,"cannot register signal handle for SIGINT\n");
err_exit(errMsg);
}

if (signal(SIGQUIT,sig_brk) == SIG_ERR)
{
sprintf(errMsg,"cannot register signal handle for SIGQUIT\n");
err_exit(errMsg);
}

///////////////////////////////////////////////////////////////////////////////////////////
//HERE query QuickCam for its capability
//////////////////////////////////////////////////////////////////////////////////////////
if (ioctl(deviceHandle,VIDIOCGCAP,&capability) == -1)
{ //query capability failed
sprintf(errMsg,"query capability failed.\n");
err_exit(errMsg);
}

printf("query capability success.\n");
printf("\tname = %s.\n",capability.name);
if (capability.type & VID_TYPE_CAPTURE)
printf("\ttype = VID_TYPE_CAPTURE.\n");
if (capability.type & VID_TYPE_TUNER)
printf("\ttype = VID_TYPE_TUNER.\n");
if (capability.type & VID_TYPE_SCALES)
printf("\ttype = VID_TYPE_SCALES.\n");
if (capability.type & VID_TYPE_MONOCHROME)
printf("\ttype = VID_TYPE_MONOCHROME.\n");
if (capability.type & VID_TYPE_SUBCAPTURE)
printf("\ttype = VID_TYPE_SUBCAPTURE.\n");
printf("\tchannels = %d\n",capability.channels);
if (capability.audios != 0)
printf("\taudios = %d\n",capability.audios);
printf("\tmaxwidth = %d,maxheight = %d\n",capability.maxwidth,capability.maxheight);
printf("\tminwidth = %d,minheight = %d\n",capability.minwidth,capability.minheight);

//////////////////////////////////////////////////////////////////////////////////////////////
//HERE query QuickCam for available channels
//////////////////////////////////////////////////////////////////////////////////////////////
int i = 0;
while (i < capability.channels)
{
queryChannel.channel = i++;
if (ioctl(deviceHandle,VIDIOCGCHAN,&queryChannel) == -1)
{ //query channel failed
printf("query channel %d failed.\n",queryChannel.channel);
continue;
}

//then,query channel success
printf("channel %d is available.\n",queryChannel.channel);
printf("\tchannel name = %s.\n",queryChannel.name);
if (queryChannel.tuners != 0)
printf("\tchannel tuners = %d.\n",queryChannel.tuners);
if (queryChannel.flags & VIDEO_VC_TUNER)
printf("\tVIDEO_VC_TUNER = channel has tuners.\n");
if (queryChannel.flags & VIDEO_VC_AUDIO)
printf("\tVIDEO_VC_AUDIO = channel has audio.\n");

if (queryChannel.type & VIDEO_TYPE_TV)
printf("\tVIDEO_TYPE_TV = channel is a TV input.\n");
if (queryChannel.type & VIDEO_TYPE_CAMERA)
printf("\tVIDEO_TYPE_CAMERA = channel is a camera.\n");

printf("\tchannel norm = %d.\n",queryChannel.norm);
}

//////////////////////////////////////////////////////////////////////////////////////////////
//HERE query QuickCam for window property,i.e, x,y,width and height
//////////////////////////////////////////////////////////////////////////////////////////////
if (ioctl(deviceHandle,VIDIOCGWIN,&queryWindow) == -1)
{ //query window failed
sprintf(errMsg,"query window failed.\n");
err_exit(errMsg);
}

printf("query window success.\n");
printf("\tx = %d,y = %d.\n",queryWindow.x,queryWindow.y);
printf("\twidth = %d,height = %d.\n",queryWindow.width,queryWindow.height);

/////////////////////////////////////////////////////////////////////////////////////////////
//HERE query QuickCam for picture property,i.e,brightness,contrast,depth and palette
/////////////////////////////////////////////////////////////////////////////////////////////
char* palette_mode;
if (ioctl(deviceHandle,VIDIOCGPICT,&queryPicture) == -1)
{ //query picture failed
sprintf(errMsg,"query picture failed.\n");
err_exit(errMsg);
}

//then,query picture success
printf("query picture success.\n");
printf("\tbrightness = %d.\n",queryPicture.brightness);
printf("\tcontrast = %d.\n",queryPicture.contrast);
if (queryPicture.depth == 15)
palette_mode = "VIDEO_PALETTE_RGB555";
else if (queryPicture.depth == 16)
palette_mode = "VIDEO_PALETTE_RGB565";
else if (queryPicture.depth == 24)
palette_mode = "VIDEO_PALETTE_RGB24";
else if (queryPicture.depth == 32)
palette_mode = "VIDEO_PALETTE_RGB32";
printf("\tdepth = %d,palette = %s\n",queryPicture.depth,palette_mode);

///////////////////////////////////////////////////////////////////////////////////////
//HERE close deviceHandle,then main() return
///////////////////////////////////////////////////////////////////////////////////////
close(deviceHandle);
return 0;
}


ÒÔÉÏ´úÂëÔÚÔËÐÐlinuxÄں˵ÄPCƽ̨ºÍǶÈëʽ¿ª·¢°åÉϲúÉúµÄ½á¹ûÈçÏ£¨Ä¿±êΪÂÞ¼¼QuickCam ExpressÉãÏñÍ·£©£º

open /dev/video0 success.
query capability success.
name = Logitech QuickCam USB.
type = VID_TYPE_CAPTURE.
type = VID_TYPE_SUBCAPTURE.
channels = 1
maxwidth = 352,maxheight = 292
minwidth = 32,minheight = 32
channel 0 is available.
channel name = Camera.
VIDEO_TYPE_CAMERA = channel is a camera.
channel norm = 0.
query window success.
x = 0,y = 0.
width = 352,height = 292.
query picture success.
brightness = 32768.
contrast = 32768.
depth = 24,palette = VIDEO_PALETTE_RGB24
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. 85.103.11.* ÓÚ 2007-07-06 00:44:37·¢±í:

    http://2154d68f7a82c90ebfbf1213efba29aa-t.lwgmrw.org 2154d68f7a82c90ebfbf1213efba29aa http://2154d68f7a82c90ebfbf1213efba29aa-b1.lwgmrw.org 2154d68f7a82c90ebfbf1213efba29aa http://2154d68f7a82c90ebfbf1213efba29aa-b3.lwgmrw.org 7323937625928ec2c2b389a5c949efe8