⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vfwimageprocessor.cpp

📁 一个VFW的视频捕捉系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
      m_ErrorID = DV_ERR_NONSPECIFIC;
   }

   return Ret;
}


/*******************************************************************************
   Function   : GetPreviousError
   Arguments  : ErrorID (output) - ID of Error
                ErrorString (output) - Description of error.
                ResetError (input) - TRUE Reset error condition.
   Return     : none
   Description: Gets the last Error ID and Error Description.
*******************************************************************************/
VOID CVFWImageProcessor::GetPreviousError(INT *ErrorID, CString *ErrorString, BOOL ResetError)
{
   if (ErrorID)
      *ErrorID = m_ErrorID;

   if (ErrorString)
      *ErrorString = m_ErrorText;

   if (ResetError)
   {
      m_ErrorID = 0;
      m_ErrorText.Empty();
   }
}


/*******************************************************************************
   Function   : EnablePreviewVideo
   Arguments  : Parent (input) - Parent window that will display video.
                x (input) - X Location in parent where video will be shown.
                y (input) - Y location in parent where video will be shown.
                PreviewRate (input) - Rate of preview in FPS.
   Return     : TRUE Success, FALSE Failed.
   Description: Enables preview video mode.
*******************************************************************************/
BOOL CVFWImageProcessor::EnablePreviewVideo(HWND Parent, INT x, INT y, INT PreviewRate)
{
   // Reset any error conditions.
   return EnablePreviewVideo(Parent,
                             x,y,
                             m_BitmapInfoHeader.biWidth,
                             m_BitmapInfoHeader.biHeight,
                             PreviewRate);
}


/*******************************************************************************
   Function   : EnablePreviewVideo
   Arguments  : Parent (input) - Parent window that will display video.
                x (input) - X Location in parent where video will be shown.
                y (input) - Y location in parent where video will be shown.
                Width (input) - Width of preview window.
                Height (input) - Height of preview window.
                PreviewRate (input) - Rate of preview in FPS.
   Return     : TRUE Success, FALSE Failed.
   Description: Enables preview video mode.
*******************************************************************************/
BOOL CVFWImageProcessor::EnablePreviewVideo(HWND Parent, INT x, INT y, INT Width, INT Height, INT PreviewRate)
{
   // Reset any error conditions.
   GetPreviousError(NULL,NULL,TRUE);

   SetParent(m_hWndVideo,Parent);
   SetWindowLong(m_hWndVideo,GWL_STYLE,WS_CHILD);

   SetWindowPos(m_hWndVideo,NULL,x,y,
                Width,
                Height,
                SWP_NOZORDER);
   ShowWindow(m_hWndVideo,SW_SHOW);
   capPreviewRate(m_hWndVideo, PreviewRate);

   return capPreview(m_hWndVideo,TRUE);
}




/*******************************************************************************
   Function   : DisablePreviewVideo
   Arguments  : none
   Return     : TRUE Success, FALSE Failed.
   Description: Disables preview video.
*******************************************************************************/
BOOL CVFWImageProcessor::DisablePreviewVideo()
{
   // Reset any error conditions.
   GetPreviousError(NULL,NULL,TRUE);

   BOOL Ret = capPreview(m_hWndVideo,FALSE);

   SetWindowPos(m_hWndVideo,NULL,0,0,0,0,SWP_NOZORDER);
   SetParent(m_hWndVideo,NULL);
   SetWindowLong(m_hWndVideo,GWL_STYLE,WS_POPUP);

   return Ret;
}


/*******************************************************************************
   Function   : DriverGetCaps
   Arguments  : Caps (output)
   Return     : See capDriverGetCaps()
   Description: Wrapper function for capDriverGetCaps().
*******************************************************************************/
BOOL CVFWImageProcessor::DriverGetCaps(CAPDRIVERCAPS *Caps)
{
   // Reset any error conditions.
  GetPreviousError(NULL,NULL,TRUE);

  return capDriverGetCaps(m_hWndVideo,Caps,sizeof(*Caps));
}


/*******************************************************************************
   Function   : DlgVideoSource
   Arguments  : none
   Return     : See capDlgVideoSource()
   Description: Wrapper function for capDlgVideoSource().
*******************************************************************************/
BOOL CVFWImageProcessor::DlgVideoSource()
{
   // Reset any error conditions.
   GetPreviousError(NULL,NULL,TRUE);

   return PostThreadMessage(m_CaptureThread->m_nThreadID,UM_VID_SOURCE,0,0);

// return capDlgVideoSource(m_hWndVideo);
}


/*******************************************************************************
   Function   : DlgVideoFormat
   Arguments  : none
   Return     : See capDlgVideoFormat()
   Description: Wrapper function for capDlgVideoFormat()
*******************************************************************************/
BOOL CVFWImageProcessor::DlgVideoFormat()
{
   // Reset any error conditions.
   GetPreviousError(NULL,NULL,TRUE);

   return PostThreadMessage(m_CaptureThread->m_nThreadID,UM_VID_FORMAT,0,0);

//   return capDlgVideoFormat(m_hWndVideo);
}


/*******************************************************************************
   Function   : DlgVideoDisplay
   Arguments  : none
   Return     : See capDlgVideoDisplay()
   Description: Wrapper function for capDlgVideoDisplay()
*******************************************************************************/
BOOL CVFWImageProcessor::DlgVideoDisplay()
{
   // Reset any error conditions.
   GetPreviousError(NULL,NULL,TRUE);

   return PostThreadMessage(m_CaptureThread->m_nThreadID,UM_VID_DISPLAY,0,0);

// return capDlgVideoDisplay(m_hWndVideo);
}


/*******************************************************************************
   Function   : CancelCapture
   Arguments  : none
   Return     : none
   Description: Cancels current AVI capture.
*******************************************************************************/
VOID CVFWImageProcessor::CancelCapture()
{
   m_CancelCapture.SetEvent();
   capCaptureAbort(m_hWndVideo);
}


/*******************************************************************************
   Function   : AllocDIBImage
   Arguments  : ppImageData (output)     - Return pointer to allocated
                                           memory.  If passed as NULL,
                                           not used.
                AllocatedSize (output)   - Size of allocated block.
                                           If passed as NULL, not used.
   Return     : none
   Description: Alllocates image buffer for DIB capture.
*******************************************************************************/
BOOL CVFWImageProcessor::AllocDIBImage(PBITMAPINFO *ppImageData,
                                       ULONG *AllocatedSize)
{
   BOOL Ret = TRUE;
   DWORD Size = 0;

   // Reset any error conditions.
   GetPreviousError(NULL,NULL,TRUE);

   Size = CalcBitmapInfoSize(m_BitmapInfoHeader) + CalcBitmapSize(m_BitmapInfoHeader);

   if (Size > 0)
   {
      if (ppImageData)
      {
         *ppImageData = (BITMAPINFO *) new BYTE[Size];
         (**ppImageData).bmiHeader = m_BitmapInfoHeader;
      }
   }
   else
   {
      Ret = FALSE;
   }

   if (AllocatedSize)
   {
      *AllocatedSize = Size;
   }

   return Ret;
}



/*******************************************************************************
   Function   : GetBitmapInfoHeader()
   Arguments  : none
   Return     : BitmapInfo of capture device.
   Description: See return.
*******************************************************************************/
BITMAPINFOHEADER CVFWImageProcessor::GetBitmapInfoHeader()
{
   return m_BitmapInfoHeader;
}



/*******************************************************************************
   Function   : CalcBitmapSize()
   Arguments  : bmiHeader (input) - BITMAPINFOHEADER from which to calculate
                                    bitmap size.
   Return     : Size of Bitmap.
   Description: Calculates the size of a bitmap based upon the contents of
                the BITMAPINFOHEADER passed in.
*******************************************************************************/
ULONG CVFWImageProcessor::CalcBitmapSize(const BITMAPINFOHEADER &bmiHeader)

{
   ULONG Size = 0;

   if (bmiHeader.biSizeImage == 0)
   {
      Size = bmiHeader.biWidth *
             bmiHeader.biHeight *
             bmiHeader.biBitCount / 8;
   }
   else
   {
      Size = bmiHeader.biSizeImage;
   }

   return Size;
}


/*******************************************************************************
   Function   : CalcBitmapInfoSize()
   Arguments  : bmiHeader (input) - BITMAPINFOHEADER from which to calculate
                                    bitmap size.
   Return     : Size of Bitmap Info Header.
   Description: Calculates the size of a bitmap info header based upon the
                contents of the BITMAPINFOHEADER passed in.  This function
                can be used to determine the offset from the BITMAPINFOHEADER
                to the actual bitmap data.
*******************************************************************************/
ULONG CVFWImageProcessor::CalcBitmapInfoSize(const BITMAPINFOHEADER &bmiHeader)

{
   UINT bmiSize = (bmiHeader.biSize != 0) ? bmiHeader.biSize : sizeof(BITMAPINFOHEADER);
   return bmiSize + bmiHeader.biClrUsed * sizeof (RGBQUAD);
}


//
// Internal callback functions.
//
static LRESULT CALLBACK ErrorCallbackProc(HWND hWnd, int nErrID, LPSTR lpErrorText)
{
   CVFWImageProcessor *VFWObj = (CVFWImageProcessor *) capGetUserData(hWnd);

   if (VFWObj)
   {
      VFWObj->m_ErrorID = nErrID;
      VFWObj->m_ErrorText = lpErrorText;
   }

   return (LRESULT) TRUE;
}


static LRESULT CALLBACK ControlCallbackProc(HWND hWnd, int nState)
{
   CVFWImageProcessor *VFWObj = (CVFWImageProcessor *) capGetUserData(hWnd);
   LRESULT Ret = TRUE;

   switch(nState)
   {
      case CONTROLCALLBACK_PREROLL:
         if (VFWObj)
         {
            VFWObj->m_CancelCapture.ResetEvent();
         }

         Ret = TRUE;
         break;

      case CONTROLCALLBACK_CAPTURING:
         // if m_CancelCapture is posted, then we cancel AVI capture by returning FALSE.
         if (VFWObj)
         {
            Ret = (::WaitForSingleObject(VFWObj->m_CancelCapture,0) != WAIT_OBJECT_0);

            if (!Ret)
               TRACE("Callback Canceled Capture\n");
         }

         break;
   }

   return Ret;
}


static LRESULT CALLBACK StatusCallbackProc(HWND hWnd, int nID, LPCSTR lpsz)

{
   CVFWImageProcessor *VFWObj = (CVFWImageProcessor *) capGetUserData(hWnd);

   switch(nID)
   {
      case IDS_CAP_BEGIN:
         break;

      case IDS_CAP_END:
         break;
   }

   return (LRESULT) TRUE;
}


static LRESULT CALLBACK FrameCallbackProc(HWND hWnd, LPVIDEOHDR lpVHdr)

{
   CVFWImageProcessor *VFWObj = (CVFWImageProcessor *) capGetUserData(hWnd);
   LRESULT Ret = TRUE;

   if (VFWObj)
   {
      if (!VFWObj->m_hWndVideo)
      {
         Ret = FALSE;
      }
      else
      {
         CSingleLock ImageLockObj(&VFWObj->m_ImageProtect, TRUE);

         if (VFWObj->m_TransferBitmapInfo)
         {
            ULONG Size;

            VFWObj->m_TransferBitmapInfo->bmiHeader = VFWObj->m_BitmapInfoHeader;

            Size =  min(VFWObj->m_TransferBitmapInfoSize - VFWObj->CalcBitmapInfoSize(VFWObj->m_TransferBitmapInfo->bmiHeader),
                        lpVHdr->dwBytesUsed);

            memcpy(((CHAR *) VFWObj->m_TransferBitmapInfo) + VFWObj->CalcBitmapInfoSize(VFWObj->m_TransferBitmapInfo->bmiHeader),
                   lpVHdr->lpData,
                   Size);
         }

         ImageLockObj.Unlock();

         VFWObj->m_ImageReady.SetEvent();
      }
   }
   else
   {
      Ret = FALSE;
   }

   return Ret;
}


static LRESULT CALLBACK StreamCallbackProc(HWND hWnd, LPVIDEOHDR lpVHdr)

{
   CVFWImageProcessor *VFWObj = (CVFWImageProcessor *) capGetUserData(hWnd);
   LRESULT Ret = TRUE;

   return Ret;
}



static UINT CaptureThreadFunc(VOID *pCVFWImageProcessor)
{
   CVFWImageProcessor *pImageProc = (CVFWImageProcessor *) pCVFWImageProcessor;
   UINT Ret = 0;
   HWND VideoWindow;
   MSG Msg;

   VideoWindow = capCreateCaptureWindow(NULL,WS_POPUP,
                                        0,0,
                                        1,1,0,0);

   pImageProc->m_hWndVideo = VideoWindow;

   pImageProc->m_ImageReady.SetEvent();

   // Process window messages for VFW window until a WM_QUIT is posted.
   while(GetMessage(&Msg,NULL,0,0))
   {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);

      switch(Msg.message)
      {
         case UM_VID_SOURCE:
            capDlgVideoSource(pImageProc->m_hWndVideo);
            break;

         case UM_VID_FORMAT:
            capDlgVideoFormat(pImageProc->m_hWndVideo);
            break;

         case UM_VID_DISPLAY:
            capDlgVideoDisplay(pImageProc->m_hWndVideo);
            break;
      }
   }

   Ret = Msg.wParam;

// Ret = AfxGetThread()->Run();

   if (VideoWindow)
   {
      DestroyWindow(VideoWindow);
   }

   return Ret;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -