Use of the camera under Windows

Source: Internet
Author: User

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"

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.