#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"ml.lib")
#pragma comment(lib,"cvaux.lib")
#pragma comment(lib,"cvcam.lib") 

#include "cv.h"
#include "highgui.h"
#include <stdio.h>

inline void cvDoubleMatPrint( const CvMat* mat );
inline void cvDoubleMatSet(  CvMat* mat,double v );
inline void cvDoubleMatSet( CvMat* mat);
inline void cvScalarPrint(const CvScalar& s);

int main( int argc, char** argv )
{
// if( argc == 2 && (pImg = cvLoadImage( argv[1], CV_LOAD_IMAGE_UNCHANGED)) != 0 )
//	  return 0;
CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
//CvCapture* capture = cvCaptureFromAVI("infile.avi");
IplImage* img = 0; 
//CVAPI(int) cvGrabFrame( CvCapture* capture );

cvNamedWindow("image",CV_WINDOW_AUTOSIZE);


CvVideoWriter *writer = 0;
int isColor = 1;
int fps     = 25;  // or 30
int frameW  = 640; // 744 for firewire cameras
int frameH  = 480; // 480 for firewire cameras
writer=cvCreateVideoWriter("out.avi",CV_FOURCC('X','V','I','D'),
                           fps,cvSize(frameW,frameH),isColor);
char key=0;

while(key!=27){
  cvGrabFrame(capture);          // capture a frame
  img=cvRetrieveFrame(capture);  // retrieve the captured frame
  // img = cvQueryFrame(capture);
  cvShowImage("image", img);
  cvWriteFrame(writer,img);      // add the frame to the file
key=cvWaitKey(30);           // wait 20 ms
//注意 cvWaitKey 参数应该不小于 20 ms,否则画面的显示可能出错.
}
 

//由视频流捕捉器得到的图像是由捕捉器分配和释放内存的,不需要单独对图像进行释放内存的操作
//cvReleaseImage(&img);
cvDestroyWindow("image");
cvReleaseVideoWriter(&writer);
cvReleaseCapture(&capture);

 return 0;
}