The related API functions that call the camera are provided under Windows, and the function definitions are included in the Vfw.h header file.
http://msdn.microsoft.com/zh-cn/dd757677 This address provides a complete tutorial.
Here is a simple example
Header file:
#ifndef Pvcamerasnapshoter_h#definePvcamerasnapshoter_h#include<QtGui/QWidget>#include<QtGui/QPixmap>#include<QtCore/QByteArray>#include<Windows.h>#include<Vfw.h>#pragmaComment (lib, "Vfw32.lib")classPvcamerasnapshoter: Publicqwidget{Q_object Public: Pvcamerasnapshoter (Qwidget*parent =0); ~pvcamerasnapshoter (void); BOOLOpencamera (); //Initialize camera voidClosecamera (); Qpixmap Getcamerasnapshot (); //Keep a frame of picture data
Private: HWND Camerascreenhwnd;};#endif //Pvcamerasnapshoter_h
Implementation file:
#include <tchar.h>#include<QtGui/QBitmap>#include<QtCore/QByteArray>#include"PvCameraSnapshoter.h"#include".. /COMMON/PVCMUSERPATH.H"LRESULT PASCAL Snapshotframecbproc (HWND hwnd, LPVIDEOHDR LPVHDR); //callback functions are used to process the received image data
pvcamerasnapshoter::p vcamerasnapshoter (Qwidget*parent): Qwidget (parent) {}pvcamerasnapshoter::~pvcamerasnapshoter (void){}BOOLPvcamerasnapshoter::opencamera () {LPTSTR lpszName=Newtchar[ -]; LPTSTR Lpszver=Newtchar[ -]; Capgetdriverdescription (0, LpszName, -, Lpszver, -); Camerascreenhwnd= Capcreatecapturewindow (LpszName, Ws_child | Ws_visible,0,0, Width (), height (), winId (),0); //Create a window to display the webcam video if(Camerascreenhwnd = =Invalid_handle_value) { return false; } if(Capdriverconnect (Camerascreenhwnd,0)) //with linked camera {Cappreview (Camerascreenhwnd,true); //Set up entertainment mode so that the window associated with the camera can automatically display the video data cappreviewrate (Camerascreenhwnd, -); //60 frames per second Bitmapinfo Bitmapinfo; memset (&bitmapinfo,0,sizeof(Bitmapinfo)); Bitmapinfo.bmiHeader.biSize=sizeof(Bitmapinfoheader); Bitmapinfo.bmiHeader.biWidth= the; Bitmapinfo.bmiHeader.biHeight= -; Bitmapinfo.bmiHeader.biPlanes=1; Bitmapinfo.bmiHeader.biBitCount= +; Bitmapinfo.bmiHeader.biCompression=Bi_rgb; Capsetvideoformat (Camerascreenhwnd,&bitmapinfo,sizeof(Bitmapinfo)); //Set the video format of the camera, but this function doesn't seem to work Capsetuserdata (Camerascreenhwnd, This); //Add user data, capsetcallbackonframe (camerascreenhwnd,snapshotframecbproc) may be used in the callback function; //Set callback function, camera video data every frame goes through this function return true; } return false;}voidPvcamerasnapshoter::closecamera () {capdriverdisconnect (Camerascreenhwnd);} Qpixmap Pvcamerasnapshoter::getcamerasnapshot () {Qpixmap camerasnapshot; if(Camerascreenhwnd = =Invalid_handle_value) { returnCamerasnapshot; } if(Capgrabframenostop (Camerascreenhwnd)) //Grab a frame of image data, which does not stop preview mode and overlap {pvcmuserpath Userp, compared to Capgrabframe Ath QString Strsnapshotfile= UserPath. Getusersubpath ("Camerasnapshot"). FilePath ("snapshot.bmp"); Constwchar_t* Wsnapshotpath = reinterpret_cast<ConstWchar_t*>(Strsnapshotfile.utf16 ()); if(!Capfilesavedib (Camerascreenhwnd,wsnapshotpath)) //Keep camera snapshots {Qdebug ("Save Camerasnapshot Fail"); returnCamerasnapshot; } camerasnapshot.load (Strsnapshotfile); } returnCamerasnapshot;} LRESULT PASCAL Snapshotframecbproc (HWND hwnd, LPVIDEOHDR LPVHDR) { //Processing video data herereturn(LRESULT) TRUE;}#include"Moc_pvcamerasnapshoter.cpp"