VideoCapture.cpp
资源名称:VideoMove.rar [点击查看]
上传用户:czshopping
上传日期:2022-05-22
资源大小:5430k
文件大小:14k
源码类别:
视频捕捉/采集
开发平台:
Visual C++
- // VideoCapture.cpp: implementation of the CVideoCapture class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "VideoCapture.h"
- #include "resource.h"
- #include "detect.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //#include "FaceImageCtrl.h"
- //#include "FaceImageCtrl_i.c"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CVideoCapture::CVideoCapture()
- {
- m_hParentWnd=NULL;
- m_iMaxCapFrame=1;
- m_pBuffer=NULL;
- m_iConnectedDriverID=-1;
- m_hCapWnd=NULL;
- m_iHeight=240;
- m_iWidth=320;
- m_bHasOverlay=FALSE;
- m_bInit=FALSE;
- m_bCapture=FALSE; //正在扑捉
- m_PreViewRate=30; //预揽帧数
- m_bStorageData=FALSE;//要求保存扑捉
- lpVideo=this;
- m_dwBufferSize=0;
- nOverlayColor = 0;
- nOverlayShape = 0;
- }
- CVideoCapture::~CVideoCapture()
- {
- }
- BOOL CVideoCapture::EnableVideo(long nDriver)
- {
- if(!ConnectDev(nDriver))return FALSE;
- return TRUE;
- }
- BOOL CVideoCapture::EnableAnyVideo()
- {
- if (m_MemDC.GetSafeHdc()==NULL)
- {
- m_MemDC.CreateCompatibleDC(m_pDC);
- }
- if(!ConnectDev(-1))
- return FALSE;
- return TRUE;
- }
- void CVideoCapture::DisableVideo()
- {
- DisConnectDev();
- }
- //构建扑捉窗
- BOOL CVideoCapture::CaptureInit(HWND hwndParent)
- {
- int nCapWndID=0;
- if (hwndParent==NULL) return FALSE;
- if (!InitVideoBuffer())
- return FALSE;
- m_hParentWnd=hwndParent;
- m_hCapWnd= capCreateCaptureWindow(
- (LPSTR)"Face Video Capture Window", WS_CHILD|WS_VISIBLE,
- 0, 0,m_iWidth,m_iHeight,
- hwndParent, nCapWndID );
- if (hwndParent == NULL)
- {
- m_hParentWnd=NULL;
- return FALSE;
- }
- m_pDC=CWnd::FromHandle(m_hParentWnd)->GetDC();
- if (m_MemDC.GetSafeHdc()==NULL)
- {
- m_MemDC.CreateCompatibleDC(m_pDC);
- }
- m_Bitmap.CreateCompatibleBitmap(m_pDC,m_iWidth,m_iHeight);
- m_pRotateBuffer=new BYTE[m_iHeight*m_iWidth*4];
- m_MoveTrace.Initialize(m_iWidth, m_iHeight,3,3);
- return TRUE;
- }
- void CVideoCapture::CaptureEnd()
- {
- CWnd::FromHandle(m_hParentWnd)->ReleaseDC(m_pDC);
- m_Bitmap.DeleteObject();
- if (m_MemDC.GetSafeHdc()!=NULL)
- {
- m_MemDC.DeleteDC();
- }
- delete m_pRotateBuffer;
- m_MoveTrace.Uninitialize();
- DisConnectDev();
- //::DestroyWindow(m_hCapWnd);
- if (m_pBuffer)
- delete (m_pBuffer);
- m_pBuffer=NULL;
- m_hParentWnd=NULL;
- m_iMaxCapFrame=1;
- m_pBuffer=NULL;
- m_hCapWnd=NULL;
- m_iHeight=0;
- m_iWidth=0;
- m_bHasOverlay=FALSE;
- m_bInit=FALSE;
- m_bCapture=FALSE; //正在扑捉
- m_bStorageData=FALSE;//要求保存扑捉
- }
- //获取当前设置的设备号
- long CVideoCapture::GetCurrentDriver()
- {
- return m_iConnectedDriverID;
- }
- //取指定设备号对应名称
- BSTR CVideoCapture::GetDriverName(long nDriver)
- {
- char szDeviceName[160];
- if (!capGetDriverDescription(nDriver,szDeviceName,
- sizeof(szDeviceName), NULL,0) )
- szDeviceName[0]=0;
- m_bstr = szDeviceName;
- return m_bstr;
- }
- //返回可用设备数
- long CVideoCapture::GetVideoDriverCount()
- {
- int wDriverIndex;
- char szDeviceName[160];
- for (wDriverIndex=0; wDriverIndex < 10; wDriverIndex++)
- {
- szDeviceName[0]=0;
- if (capGetDriverDescription(wDriverIndex,szDeviceName,
- sizeof(szDeviceName), NULL,0) )
- {
- if (szDeviceName[0]==0)
- break;
- }
- else
- break;
- }
- return wDriverIndex;
- }
- //连接扑捉设备
- BOOL CVideoCapture::ConnectDev(long DriverID)
- {
- CAPDRIVERCAPS CapDrvCaps;
- if (m_hCapWnd==NULL)
- return FALSE;
- if (m_iConnectedDriverID!=DriverID)
- {
- DisConnectDev();
- }
- m_iConnectedDriverID=-1;
- //auto scan valid driver
- if(-1 == DriverID)
- {
- for (int i=0;i<GetVideoDriverCount();i++)
- {
- if(capDriverConnect(m_hCapWnd, i))
- if (capDriverGetCaps(m_hCapWnd, &CapDrvCaps, sizeof(CAPDRIVERCAPS)) )
- if (CapDrvCaps.fCaptureInitialized)
- break;
- }
- if (i>=GetVideoDriverCount())
- {
- return FALSE;
- }
- else
- m_iConnectedDriverID=i;
- }
- else
- {
- if(!capDriverConnect(m_hCapWnd, DriverID))
- {
- return FALSE;
- }
- if (!capDriverGetCaps(m_hCapWnd, &CapDrvCaps, sizeof(CAPDRIVERCAPS)) )
- return FALSE;
- else
- {
- if (!CapDrvCaps.fCaptureInitialized)
- {
- return FALSE;
- }
- else
- m_iConnectedDriverID=DriverID;
- }
- }
- if (!SetVideoFormat())
- {
- DisConnectDev();
- return FALSE;
- }
- //set call back function
- if (!InitVideoBuffer())
- {
- DisConnectDev();
- return FALSE;
- }
- if (!capSetCallbackOnFrame(m_hCapWnd,FrameCallbackProc))
- {
- DisConnectDev();
- return FALSE;
- }
- m_bHasOverlay=CapDrvCaps.fHasOverlay;
- m_bInit = TRUE;
- MoveWindow(320,0,320,240);
- SetPreviewRate();
- return TRUE;
- }
- BOOL CVideoCapture::SetVideoFormat(int Width,int Height)
- {
- ///////////////Set Video format(frame width and height)/////////////
- DWORD dwSize;
- LPBITMAPINFO lpbi; //位图信息描述
- dwSize = capGetVideoFormatSize(m_hCapWnd);
- lpbi = (LPBITMAPINFO)malloc(dwSize);
- if (lpbi==NULL)
- return FALSE;
- capGetVideoFormat(m_hCapWnd,lpbi,dwSize);
- lpbi->bmiHeader.biBitCount = 24;
- lpbi->bmiHeader.biWidth = Width;
- lpbi->bmiHeader.biHeight = Height;
- lpbi->bmiHeader.biCompression = BI_RGB;
- lpbi->bmiHeader.biSizeImage =0;
- if(!capSetVideoFormat( m_hCapWnd, lpbi, dwSize))
- {
- //return FALSE;
- }
- capGetVideoFormat(m_hCapWnd,lpbi,dwSize);
- m_iWidth=lpbi->bmiHeader.biWidth;
- m_iHeight=lpbi->bmiHeader.biHeight;
- if ((lpbi->bmiHeader.biBitCount>24)||(lpbi->bmiHeader.biBitCount==12))
- {
- //Commented by Yuan 2002/10/21
- //free(lpbi);
- //return FALSE;
- }
- m_dwBufferSize=lpbi->bmiHeader.biSizeImage ;
- m_bitCount=lpbi->bmiHeader.biBitCount;
- free(lpbi);
- return TRUE;
- }
- void CVideoCapture::DisConnectDev()
- {
- if (m_hCapWnd==NULL)
- return ;
- //if (m_bCapture)
- // capCaptureStop(m_hCapWnd);
- capSetCallbackOnFrame(m_hCapWnd,NULL);
- capDriverDisconnect(m_hCapWnd);
- }
- BOOL CVideoCapture::Preview(BOOL onoff)
- {
- if(!m_bInit)
- {
- return FALSE;
- }
- capPreview(m_hCapWnd,onoff);
- return TRUE;
- }
- BOOL CVideoCapture::SetPreviewRate(int rate)
- {
- if((!m_bInit)||(rate<0))
- {
- return FALSE;
- }
- if(rate == 0) rate = 5;
- if (!capPreviewRate(m_hCapWnd,(WORD) ((1000.0 / rate) + 0.5)) )
- return FALSE;
- m_PreViewRate=rate;
- capPreview(m_hCapWnd,true);
- return TRUE;
- }
- BOOL CVideoCapture::SetScrollPos(LPPOINT pos)
- {
- if(!m_bInit)return FALSE;
- return capSetScrollPos(m_hCapWnd,pos);
- }
- BOOL CVideoCapture::StretchVideo(BOOL bStretch)
- {
- if(!m_bInit)return FALSE;
- //return capPreviewScale(m_hParentWnd,bStretch);
- return capPreviewScale(m_hCapWnd,bStretch);
- }
- BOOL CVideoCapture::SetOverlay(bool bOverlay)
- {
- if(!m_bInit)return FALSE;
- if (!m_bHasOverlay)return FALSE;
- return capOverlay(m_hCapWnd,bOverlay);
- }
- void CVideoCapture::SetOverlayColor(int nColor)
- {
- nOverlayColor = nColor;
- }
- void CVideoCapture::SetOverlayShape(int nShape)
- {
- nOverlayShape = nShape;
- }
- void CVideoCapture::OverlayDraw(RECT rect)
- {
- HDC dc;
- HPEN penColor;
- HPEN penOld;
- HBRUSH brush;
- HBRUSH brushOld;
- LOGBRUSH log;
- if (m_hCapWnd!=NULL)
- {
- dc=::GetDC(m_hCapWnd);
- log.lbStyle =BS_NULL;
- log.lbColor =0;
- log.lbHatch =0;
- brush=CreateBrushIndirect(&log);
- switch(nOverlayColor)
- {
- case 0:
- penColor = ::CreatePen(PS_SOLID,2,RGB(255,0,0));
- break;
- case 1:
- penColor = ::CreatePen(PS_SOLID,2,RGB(0,255,0));
- break;
- case 2:
- penColor = ::CreatePen(PS_SOLID,2,RGB(0,0,255));
- break;
- default:
- break;
- }
- penOld=(HPEN)::SelectObject(dc,penColor);
- brushOld=(HBRUSH)::SelectObject(dc,brush);
- ::SetBkMode(dc,TRANSPARENT);
- switch(nOverlayShape)
- {
- case 0:
- ::Ellipse(dc,rect.left ,rect.top ,rect.right ,rect.bottom );
- break;
- case 1:
- ::Ellipse(dc,rect.left ,rect.top ,rect.right ,rect.top + rect.right-rect.left);
- break;
- case 2:
- ::Rectangle(dc,rect.left,rect.top,rect.right,rect.bottom);
- break;
- default:
- break;
- }
- if (penOld!=NULL)
- ::SelectObject(dc,penOld);
- if (brushOld!=NULL)
- ::SelectObject(dc,brushOld);
- ::DeleteObject(penColor);
- ::DeleteObject(brush);
- ::ReleaseDC(m_hCapWnd,dc);
- }
- }
- void CVideoCapture::ShowVideoFormatDialog()
- {
- if(!m_bInit)return ;
- capDlgVideoFormat(m_hCapWnd);
- GetVideoFormat();
- }
- void CVideoCapture::ShowVideoSourceDialog()
- {
- if(!m_bInit)return ;
- capDlgVideoSource(m_hCapWnd);
- GetVideoFormat();
- }
- BOOL CVideoCapture::GrabVideo(unsigned char *pImageData)
- {
- BOOL Result;
- if(!m_bInit)return FALSE;
- m_bStorageData=TRUE;
- Result=capGrabFrameNoStop(m_hCapWnd);
- m_bStorageData=FALSE;
- if (Result)
- GetImage(pImageData);
- return Result;
- }
- void CVideoCapture::SaveImage(LPVIDEOHDR lpVHdr)
- {
- memcpy(m_pBuffer, lpVHdr->lpData, m_dwBufferSize);
- }
- //取帧数据
- void CVideoCapture::GetImage(unsigned char *pImageData)
- {
- memcpy(pImageData,m_pBuffer, m_dwBufferSize);
- }
- //初始化帧缓冲
- BOOL CVideoCapture::InitVideoBuffer()
- {
- DWORD Size;
- if (m_pBuffer)
- delete (m_pBuffer);
- Size=3*m_iWidth*m_iHeight;
- m_pBuffer=new unsigned char[Size];
- return (m_pBuffer!=NULL);
- }
- void CVideoCapture::MoveWindow(int x,int y,int nWidth,int nHeight)
- {
- //m_iWidth = nWidth;
- //m_iHeight = nHeight;
- ::MoveWindow(m_hCapWnd,x,y,nWidth,nHeight,TRUE);
- }
- void CVideoCapture::GetVideoFormat()
- {
- DWORD dwSize;
- LPBITMAPINFO lpbi; //位图信息描述
- dwSize = capGetVideoFormatSize(m_hCapWnd);
- lpbi = (LPBITMAPINFO)malloc(dwSize);
- capGetVideoFormat(m_hCapWnd,lpbi,dwSize);
- //Modified by Yuanzujun 2002/10/21
- if ( (lpbi->bmiHeader.biWidth != 320) || (lpbi->bmiHeader.biHeight!=240) )
- {
- AfxMessageBox("图像格式目前版本只支持320*240!请重新设置!");
- free(lpbi);
- return;
- }
- if ( lpbi->bmiHeader.biBitCount != 24 )
- {
- AfxMessageBox("图像深度目前版本只支持'RGB 24'!n请重新设置!");
- free(lpbi);
- return;
- }
- if((m_iWidth!=lpbi->bmiHeader.biWidth) || (m_iHeight!=lpbi->bmiHeader.biHeight))
- {
- m_iWidth=lpbi->bmiHeader.biWidth;
- m_iHeight=lpbi->bmiHeader.biHeight;
- InitVideoBuffer();
- }
- m_dwBufferSize=lpbi->bmiHeader.biSizeImage;
- m_bitCount=lpbi->bmiHeader.biBitCount;
- free(lpbi);
- }
- int CVideoCapture::GetVideoWidth()
- {
- return m_iWidth;
- }
- int CVideoCapture::GetVideoHeight()
- {
- return m_iHeight;
- }
- BOOL CVideoCapture::WriteAsBMP(char* szFileName,unsigned char* pImgData,int nWidth,int nHeight)
- {
- char aFileName[255];
- int nStrLen;
- if(strstr(szFileName,".")==NULL)
- {
- strcpy(aFileName,szFileName);
- strcat(aFileName,".bmp");
- }
- else
- {
- nStrLen=strlen(szFileName)-strlen(strstr(szFileName,"."));
- strncpy(aFileName,szFileName,nStrLen);
- aFileName[nStrLen]=' ';
- strcat(aFileName,".bmp");
- }
- BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
- /* Fill in file type (first 2 bytes must be "BM" for a bitmap) */
- bmfHdr.bfType = 0x4d42; // "BM"
- // Calculate the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)
- int nImgWidth;
- if (nWidth%4==0)
- {
- nImgWidth=nWidth*3;
- }
- else
- {
- nImgWidth=nWidth*3+nWidth%4;
- }
- bmfHdr.bfSize =(DWORD)( nImgWidth*nHeight + sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) );
- bmfHdr.bfReserved1 = 0;
- bmfHdr.bfReserved2 = 0;
- bmfHdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
- BITMAPINFOHEADER BMIH; // Pointer to DIB info structure
- BMIH.biSize=sizeof(BITMAPINFOHEADER);
- BMIH.biHeight=nHeight;
- BMIH.biWidth=nWidth;
- BMIH.biPlanes=1;
- BMIH.biBitCount=24;
- BMIH.biCompression=BI_RGB;
- BMIH.biSizeImage=nImgWidth*nHeight;
- BMIH.biXPelsPerMeter=0xec4;
- BMIH.biYPelsPerMeter =0xec4;
- BMIH.biClrUsed =0;
- BMIH.biClrImportant=0;
- FILE *pFile;
- pFile=fopen(aFileName,"wb");
- if (pFile==NULL)
- {
- return FALSE;
- }
- BYTE aTemp[12]={0};
- try {
- // Write the file header
- fwrite((BYTE*)&bmfHdr,1,sizeof(BITMAPFILEHEADER),pFile);
- // Write the DIB header
- fwrite((BYTE*)&BMIH,1,sizeof(BITMAPINFOHEADER),pFile);
- // Write the DIB bits
- int i;
- for (i=0;i<nHeight;i++)
- {
- fwrite(pImgData+i*nWidth*3,1,nWidth*3,pFile);
- fwrite(aTemp,1,nWidth%4,pFile);
- }
- }
- catch(...)
- {
- fclose(pFile);
- return FALSE;
- }
- fclose(pFile);
- return TRUE;
- }
- LRESULT FAR PASCAL FrameCallbackProc(HWND hWnd, LPVIDEOHDR lpVHdr)
- {
- if (lpVideo == NULL)
- return (LRESULT) TRUE;
- int x, y, nX, nY;
- nX = 1;
- nY = 1;
- const int nMosaicWidth=4;
- const int nMosaicHeight=4;
- CBitmap *pOldBitmap;
- pOldBitmap=lpVideo->m_MemDC.SelectObject(&lpVideo->m_Bitmap);
- lpVideo->m_MoveTrace.GetDIBBit(lpVHdr->lpData);
- lpVideo->m_MoveTrace.Extract(nX, nY);
- lpVideo->m_MoveTrace.Grey(); //m_Grey1
- lpVideo->m_MoveTrace.Sobel(); //m_Grey1
- int nThreshold=lpVideo->m_MoveTrace.GetThreshold(30);// ThresholdDIB((LPSTR)lpVideo->m_MoveTrace.m_pGrey1,320,240);
- lpVideo->m_MoveTrace.FrameMinus(); //m_pGrey[x]=abs(m_pGrey1[x] - m_pGrey2[x]);
- lpVideo->m_MoveTrace.Mosaic(nMosaicWidth, nMosaicHeight); //m_pGrey --> m_pMosaic
- lpVideo->m_MoveTrace.Dither(nThreshold); //m_pMosaic
- for( y=0;y<lpVideo->m_iHeight;y+=1)
- {
- for( x=0;x<lpVideo->m_iWidth;x+=1)
- {
- int nPos = y/nY*lpVideo->m_iWidth/nX+x/nX;
- int nTemp = lpVideo->m_MoveTrace.m_pMosaic[nPos];
- // int nTemp = lpVideo->m_MoveTrace.m_pGrey[nPos];
- // int nTemp = lpVideo->m_MoveTrace.m_pGrey1[nPos];
- lpVideo->m_MemDC.SetPixel(x,lpVideo->m_iHeight-1-y,RGB(nTemp,nTemp,nTemp));
- }
- }
- lpVideo->m_MoveTrace.SaveFrame(); //m_pGrey2[x] = m_pGrey1[x];
- CRect rect;
- bool bRet1=lpVideo->m_MoveTrace.Rectangle(rect,100,5);//m_pMosaic
- if (bRet1)
- {
- lpVideo->m_MemDC.Draw3dRect(&rect, RGB(0, 255, 0), RGB(0, 255, 0));
- lpVideo->m_MemDC.Draw3dRect(&lpVideo->m_MoveTrace.m_NextRect, RGB(0, 0, 255), RGB(0, 0, 255));
- lpVideo->m_MemDC.Draw3dRect(&lpVideo->m_MoveTrace.m_CurrentRect, RGB(255, 0, 0), RGB(255, 0, 0));
- }
- // SetDIBitsToDevice(lpVideo->m_pDC->m_hDC,0,0,m_iWidth,m_iHeight,0,0,)
- BOOL bRet=lpVideo->m_pDC->BitBlt(
- 0,0,
- lpVideo->m_iWidth,
- lpVideo->m_iHeight,
- &lpVideo->m_MemDC,
- 0,0,
- SRCCOPY);
- lpVideo->m_MemDC.SelectObject(pOldBitmap);
- //保存图片
- if (lpVideo->m_bStorageData)
- {
- lpVideo->SaveImage(lpVHdr);
- CTime Time=CTime::GetCurrentTime();
- CString strTime = Time.Format("%H%M%S");
- lpVideo->WriteAsBMP(strTime.GetBuffer(strTime.GetLength()),lpVHdr->lpData,320,240);
- lpVideo->m_bStorageData = FALSE;
- }
- return (LRESULT) TRUE;
- }