Linux applet for viewing camera parameters

Source: Internet
Author: User
For details about how to view camera parameters in Linux, refer to Linux general technology-Linux programming and kernel information. In linux, video-related tasks must involve the video for linux layer, which is abbreviated as v4l. This program is written based on this API layer. The main reason for writing this applet is that through the lsusb command, only parameters such as venderID and productID can be seen, but other parameters related to the camera itself cannot be seen. The ioctl () system call is mainly used in the program, and ioctl () is a grocery box for I/O operations. Many things need to be done by it. In addition to cameras and video capture cards, video-related operations are similar. The source code of the program is provided below for future reference. The functions of the program are actually very simple, so I will not explain them one by one. It only involves a small amount of knowledge about C Programming under * nix. If you have any questions, please refer to the relevant materials.

/* 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;
}


The above code is generated on the PC platform running Linux kernel and Embedded Development Board as follows (the target is the Logitech QuickCam Express camera ):

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
Related Article

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.