Stannum chemistry Asked:2024-12-24 22:20:41 +0000 UTC2024-12-24 22:20:41 +0000 UTC 2024-12-24 22:20:41 +0000 UTC 如何用 C++ 从网络摄像头拍摄照片并保存[关闭] 772 您需要从网络摄像头拍摄照片并保存 c++ 1 个回答 Voted Best Answer anbarafors 2024-12-25T01:35:22Z2024-12-25T01:35:22Z #pragma comment(lib,"vfw32.lib") #include <windows.h> #include <vfw.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { char svName[256]; char svComment[512]; char svBuff[1024]; int nDriverIndex = 0; for (int i=0; i<5; ++i) { if (capGetDriverDescription(nDriverIndex, svName,255,svComment,512)) { wsprintf(svBuff, "%d: %s %s", nDriverIndex,svName,svComment); ++nDriverIndex; } } if (nDriverIndex) wsprintf(svBuff,"%d devices listed",nDriverIndex); else { MessageBox(HWND_DESKTOP, "Missing video device.","Error", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND | MB_SYSTEMMODAL); ExitProcess(0); } --nDriverIndex; WNDCLASSEX wcex={sizeof(WNDCLASSEX),0,DefWindowProc,0,0,hInstance,NULL,NULL,NULL,NULL,"wndname",NULL,}; ATOM Atom=RegisterClassEx(&wcex); if(!Atom) { Atom=RegisterClass((LPWNDCLASS)&wcex.style); if (!Atom) { MessageBox(HWND_DESKTOP, "Failed register class.","Error", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND | MB_SYSTEMMODAL); ExitProcess(0); } } HWND hWnd=CreateWindowEx(0,MAKEINTATOM(Atom),"appname",0,0,0,0,0,HWND_DESKTOP,NULL,hInstance,NULL); if (!hWnd) { MessageBox(HWND_DESKTOP, "Failed register class.","Error", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND | MB_SYSTEMMODAL); ExitProcess(0); } HWND hwndCap=capCreateCaptureWindow("wnd_cap_frame", WS_CHILD,0,0,160,120,hWnd,1); if (!hwndCap) { MessageBox(HWND_DESKTOP, "Failed create cap window.","Error", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND | MB_SYSTEMMODAL); ExitProcess(0); } if (!capDriverConnect(hwndCap,nDriverIndex)) { DestroyWindow(hwndCap); wsprintf(svBuff, "Failed connect to driver #%d", nDriverIndex); MessageBox(HWND_DESKTOP, svBuff,"Error", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND | MB_SYSTEMMODAL); ExitProcess(0); } CAPDRIVERCAPS pcdc; capDriverGetCaps(hwndCap,&pcdc,sizeof(CAPDRIVERCAPS)); if (!pcdc.fCaptureInitialized) { DestroyWindow(hwndCap); wsprintf(svBuff,"Failed init driver #%d",nDriverIndex); MessageBox(HWND_DESKTOP, svBuff,"Error", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND | MB_SYSTEMMODAL); ExitProcess(0); } DWORD dwSize=capGetVideoFormatSize(hwndCap); BITMAPINFO* pbiOrig=(BITMAPINFO*)malloc(dwSize); if (!pbiOrig) { DestroyWindow(hwndCap); MessageBox(HWND_DESKTOP, "Failed allocate memory.","Error", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND | MB_SYSTEMMODAL); ExitProcess(0); } BITMAPINFO* pbiInfo=(BITMAPINFO*)malloc(dwSize); if (!pbiInfo) { DestroyWindow(hwndCap); MessageBox(HWND_DESKTOP, "Failed allocate memory #2","Error", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND | MB_SYSTEMMODAL); ExitProcess(0); } capGetVideoFormat(hwndCap,pbiOrig,dwSize); memcpy(pbiInfo,pbiOrig,dwSize); pbiInfo->bmiHeader.biWidth = 640; pbiInfo->bmiHeader.biHeight = 480; pbiInfo->bmiHeader.biBitCount = 16; pbiInfo->bmiHeader.biSizeImage = 0; pbiInfo->bmiHeader.biCompression = BI_RGB; pbiInfo->bmiHeader.biClrUsed = 0; pbiInfo->bmiHeader.biClrImportant = 0; pbiInfo->bmiHeader.biPlanes = 1; pbiInfo->bmiColors->rgbBlue = 0; pbiInfo->bmiColors->rgbGreen = 0; pbiInfo->bmiColors->rgbRed = 0; pbiInfo->bmiColors->rgbReserved = 0; /* Set video format. */ capSetVideoFormat(hwndCap,pbiInfo,dwSize); capGrabFrameNoStop(hwndCap); capFileSaveDIB(hwndCap,"Cap_001.bmp"); /* Restore original format. */ capSetVideoFormat(hwndCap,pbiOrig,dwSize); capDriverDisconnect(hwndCap); DestroyWindow(hwndCap); MessageBox(HWND_DESKTOP, "Capture frame - OK!","Info", MB_OK| MB_SETFOREGROUND | MB_SYSTEMMODAL); return 0; }
1 个回答