程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> vc教程 >> 攝像頭控制SDK源碼

攝像頭控制SDK源碼

編輯:vc教程

#include
#include
#include
#pragma comment(lib,"vfw32.lib")

HWND ghWndCap ; //捕獲窗的句柄
CAPDRIVERCAPS gCapDriverCaps ; //視頻驅動器的能力
CAPSTATUS gCapStatus ; //捕獲窗的狀態
char szCaptureFile[] = "MYCAP.AVI";
char gachBuffer[20];

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

LRESULT CALLBACK StatusCallbackProc(HWND hWnd,int nID,LPSTR lpStatusText)
{
if(!ghWndCap)return FALSE;//獲得捕獲窗的狀態
capGetStatus(ghWndCap,&gCapStatus,sizeof(CAPSTATUS));//更新捕獲窗的大小
SetWindowPos(ghWndCap,NULL,0,0,gCapStatus.uiImageWidth,gCapStatus.uiImageHeight,SWP_NOZORDER|SWP_NOMOVE);
if(nID==0){//清除舊的狀態信息
SetWindowText(ghWndCap,(LPSTR)"hello");
return (LRESULT)TRUE;
}//顯示狀態ID和狀態文本
wsprintf(gachBuffer,"Status# %d: %s",nID,lpStatusText);
SetWindowText(ghWndCap,(LPSTR)gachBuffer);
return (LRESULT)TRUE;
}
LRESULT CALLBACK ErrorCallbackProc(HWND hWnd,int nErrID,LPSTR lpErrorText)
{
if(!ghWndCap)return FALSE;
if(nErrID==0)return TRUE;//清除舊的錯誤
wsprintf(gachBuffer,"Error# %d",nErrID);//顯示錯誤標識和文本
MessageBox(hWnd, lpErrorText, gachBuffer,MB_OK | MB_ICONEXCLAMATION);
return (LRESULT) TRUE;
}

LRESULT CALLBACK FrameCallbackProc(HWND hWnd,LPVIDEOHDR lpVHdr)
{
FILE *fp;
fp=fopen("caram.dat","w");
if(!ghWndCap)return FALSE;//假設fp為一打開的.dat文件指針
fwrite(lpVHdr->lpData,1,lpVHdr->dwBufferLength,fp);
return (LRESULT)TRUE;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires WindowsNT!"),szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT("The Hello Program"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_CREATE:
{
ghWndCap=capCreateCaptureWindow((LPSTR)"Capture Window",WS_CHILD|WS_VISIBLE,0,0,300,240,(HWND)hwnd,(int)0);
capSetCallbackOnError(ghWndCap,(FARPROC)ErrorCallbackProc);
capSetCallbackOnStatus(ghWndCap,(FARPROC)StatusCallbackProc);
capSetCallbackOnFrame(ghWndCap,(FARPROC)FrameCallbackProc);
capDriverConnect(ghWndCap,0); // 將捕獲窗同驅動器連接
//獲得驅動器的能力,相關的信息放在結構變量gCapDriverCaps中
capDriverGetCaps(ghWndCap,&gCapDriverCaps,sizeof(CAPDRIVERCAPS));
capPreviewRate(ghWndCap, 66); // 設置PrevIEw模式的顯示速率
capPreview(ghWndCap, TRUE); //啟動PrevIEw模式
if(gCapDriverCaps.fHasOverlay) //檢查驅動器是否有疊加能力
capOverlay(ghWndCap,TRUE); //啟動Overlay模式
if(gCapDriverCaps.fHasDlgVideoSource)capDlgVideoSource(ghWndCap); //Video source 對話框
if(gCapDriverCaps.fHasDlgVideoFormat)capDlgVideoFormat(ghWndCap); // Video format 對話框
if(gCapDriverCaps.fHasDlgVideoDisplay)capDlgVideoDisplay(ghWndCap); // Video display 對話框
capFileSetCaptureFile( ghWndCap, szCaptureFile); //指定捕獲文件名
capFileAlloc(ghWndCap, (1024L * 1024L * 5)); //為捕獲文件分配存儲空間
capCaptureSequence(ghWndCap); //開始捕獲視頻序列
capGrabFrame(ghWndCap); //捕獲單幀圖像

}

return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClIEntRect(hwnd,&rect);
DrawText(hdc,TEXT("Hello,Windows98!"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
{
capSetCallbackOnStatus(ghWndCap,NULL);
capSetCallbackOnError(ghWndCap,NULL);
capSetCallbackOnFrame(ghWndCap,NULL);
capCaptureAbort(ghWndCap);//停止捕獲
capDriverDisconnect(ghWndCap); //將捕獲窗同驅動器斷開
PostQuitMessage(0);
}
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved