利用windows capture Api捕获的视频流是RGB数据
1
2 /*
3
4 capture.h
5 caputre用户捕获pc camera的 RGB视频源
6 做到capture,encoder,mgr 独立,不会有依赖性,以便之后的复用
7 */
8
9 #ifndef _DESKCAM_CAPTURE_H
10 #define _DESKCAM_CAPTURE_H
11
12
13 #include <windows.h>
14 #include "vfw.h"
15 #pragma comment(lib, "winmm.lib")
16 #pragma comment(lib, "vfw32.lib")
17 #include <nv.h>
18
19
20 struct CaptureInfo{
21 NVString sid;
22 unsigned int index; //
23 unsigned int type; //类别
24 unsigned short width;
25 unsigned short height;
26 void (*after_capture)(void * data,unsigned int size,void * user);
27 void * user;
28 };
29
30 class PCDeskCamera:public NVObject{
31 public:
32 PCDeskCamera(){ _bmpInfo = NULL;}
33 ~PCDeskCamera(){ if(_bmpInfo) {delete _bmpInfo;_bmpInfo=NULL;} }
34
35 bool Open();
36 void Close();
37 CaptureInfo& GetCaptureInfo(){ return _ctx;}
38 private:
39 static LRESULT CALLBACK StreamCapture(HWND hwnd,LPVIDEOHDR hdr);
40 bool GetBitmapInfo(HWND cap);
41 HWND _hwnd;
42 CaptureInfo _ctx;
43 HWND _capture_wnd;
44 LPBITMAPINFO _bmpInfo;
45 };
46 #endif
47
1
2 #include "capture.h"
3
4 /*
5 视频捕获,必须提供消息处理机制:
6 while(GetMessage()){
7 TranslateMessage();
8 DispatchMessage();
9 }
10 而且回调函数必须声明为 static LRESULT CALLBACK,否则产生异常
11 */
12
13 LRESULT PCDeskCamera::StreamCapture(HWND hwnd,LPVIDEOHDR hdr){
14 PCDeskCamera * cam;
15 //int i;
16 //i=100;
17 // MessageBeep(100);
18 // return 0;
19
20 cam = (PCDeskCamera*)GetWindowLong(hwnd,GWL_USERDATA);
21 if( cam== NULL){
22 return 0;
23 }
24 //--
25 cam->GetCaptureInfo().after_capture(hdr->lpData,hdr->dwBytesUsed,
26 cam->GetCaptureInfo().user);
27 return 1;
28 }
29
30
31 bool PCDeskCamera::GetBitmapInfo(HWND cap){
32 int vfs=capGetVideoFormatSize(cap);
33 if(!vfs) return false;
34 if(_bmpInfo) {delete _bmpInfo;_bmpInfo=NULL;}
35 _bmpInfo =(BITMAPINFO*)( new char[vfs]);
36 LPBITMAPINFOHEADER bmpIH=( LPBITMAPINFOHEADER )_bmpInfo;
37 bmpIH->biSize= sizeof(BITMAPINFOHEADER);
38 BOOL ret=capGetVideoFormat(cap, _bmpInfo, (WORD)vfs);
39 _ctx.width = bmpIH->biWidth;
40 _ctx.height = bmpIH->biHeight;
41 return true;
42 }
43
44 bool PCDeskCamera::Open(){
45 int ret;
46 CAPTUREPARMS cpp;
47 _capture_wnd = capCreateCaptureWindow("AviCap_Basic1", WS_CHILD|WS_VISIBLE,
48 0, 0, 200,200,
49 GetDesktopWindow(),
50 0xff00);
51 if(_capture_wnd == NULL){
52 return false;
53 }
54 SetWindowLong(_capture_wnd,GWL_USERDATA,(LONG)this);
55 capPreviewRate(_capture_wnd,50);
56 ret =capDriverConnect(_capture_wnd, _ctx.index);
57 if( ret == FALSE){
58 return false;
59 }
60
61 if(!GetBitmapInfo(_capture_wnd)) {
62 return false;
63 }
64 ret = capSetCallbackOnVideoStream(_capture_wnd,PCDeskCamera::StreamCapture);
65 capCaptureGetSetup(_capture_wnd, &cpp, sizeof(CAPTUREPARMS));
66 cpp.fYield = true ;
67 cpp.fAbortLeftMouse = false;
68 cpp.fAbortRightMouse = false;
69 cpp.fCaptureAudio = false;
70 ret = capCaptureSetSetup (_capture_wnd, &cpp, sizeof(CAPTUREPARMS));
71 capCaptureSequenceNoFile(_capture_wnd);
72 //capCaptureSequence(_capture_wnd);
73 ShowWindow(_capture_wnd,SW_HIDE);
74
75
76 return true;
77 }
78
79 void PCDeskCamera::Close(){
80 // capCaptureAbort(_capture_wnd);
81 capSetCallbackOnVideoStream(0 ,NULL);
82 capDriverDisconnect(_capture_wnd);
83 capCaptureStop(_capture_wnd);
84 SetWindowLong(_capture_wnd,GWL_USERDATA,(LONG)NULL);
85 ::CloseWindow(_capture_wnd);
86 DestroyWindow(_capture_wnd);
87 }
88
89
90