// C++ includes.
#include <iostream>
// OpenCV includes.
#include <cv.h>
#include <highgui.h>
void printCamResolution(CvCapture* camera)
{
std::cout << std::endl;
std::cout << "Camera Capture Resolution:" << std::endl;
std::cout << "=================" << std::endl;
std::cout << "Video width: " << cvGetCaptureProperty(
camera, CV_CAP_PROP_FRAME_WIDTH) << std::endl;
std::cout << "Video height: " << cvGetCaptureProperty(
camera, CV_CAP_PROP_FRAME_HEIGHT) << std::endl;
std::cout << std::endl;
}
int main(int argc, char* argv[])
{
// Tell OpenCV to capture data from a camera.
CvCapture* camera = cvCaptureFromCAM(0);
// Print the camera's properties.
printCamResolution(camera);
// Create a window with the given name (displayed at the top of the
// window). Also, we'll make it automatically resize to whatever content
// it displays.
cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE);
// Loop forever until the user quits.
while(1)
{
// This will return the code of the pressed key or -1 if
// nothing was pressed before 10 ms elapsed.
int keyCode = cvWaitKey(10);
if ('c' == (char)keyCode)
{
// The user pressed 'c' (for 'capture').
// Get the latest image from the camera. This
// image should NOT be released manually.
IplImage* image = cvQueryFrame(camera);
// Make the image show up in the named window.
cvShowImage("My Window", image);
// Save the image to a file.
static int imageCount = 0;
char filename[32];
sprintf(filename, "image%d.png", imageCount);
cvSaveImage(filename, image);
++imageCount;
}
else if(keyCode >= 0)
{
// A non-capture key was pressed, so end the loop.
break;
}
}
// Destroy the capture object and the window.
cvReleaseCapture(&camera);
cvDestroyWindow("My Window");
return 0;
}