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

📄 mainframe.cpp

📁 BCAM 1394 Driver
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        // Get the buffer's index from the queue
        unsigned int BufferIndex = m_BufferQueue.front();
        m_BufferQueue.pop();
        
        // If more than one buffer is in the queue display only the most recent
        if(0 < m_BufferQueue.size() )
        { 
          // Enqueue a new Grab command 
          m_Bcam.GrabImageAsync(
            (char*)m_ptrBitmaps[BufferIndex]->GetPixels(), 
            m_ptrBitmaps[BufferIndex]->GetTotalPixelBytes(), 
            (void*)BufferIndex,
            USE_ONESHOT);  
        }
        else 
        {
          // If the image is monochrome the most recent buffer is hold
          // by BitmapView; if the image is Bayer the SetBitmap function
          // will do a RGB copy so the buffer can be enqueued again
          // immediately
          if(!m_IsBayerImage)
          {
            // Enqueue the buffer currently being displayed
            if(m_DisplayedBufferIndex != -1)
            {
              // Enqueue a new Grab command 
              m_Bcam.GrabImageAsync(
                (char*)m_ptrBitmaps[m_DisplayedBufferIndex]->GetPixels(), 
                m_ptrBitmaps[m_DisplayedBufferIndex]->GetTotalPixelBytes(), 
                (void*)BufferIndex,
                USE_ONESHOT);  
            }
            
            // Remeber which buffer is now displayed
            m_DisplayedBufferIndex = BufferIndex;
          }
          
          // Update View
          m_view.SetBitmap(m_ptrBitmaps[BufferIndex], m_IsBayerImage);
          
          // see comment above
          if(m_IsBayerImage)
          {
            // Enqueue a new Grab command 
            m_Bcam.GrabImageAsync(
              (char*)m_ptrBitmaps[BufferIndex]->GetPixels(), 
              m_ptrBitmaps[BufferIndex]->GetTotalPixelBytes(), 
              (void*)BufferIndex,
              USE_ONESHOT);  
          }
          
        }
      }
    }
    
    // Update the user interface
    UpdateUI();
  }
  CATCH_MSGBOX( "CMainFrame::OnIdle" ) 
    
    return FALSE;
}

/*!
Called if the user wants the live grab to stop. Enqueues a #NotifyQuit message to the grab
queue. Sets the #m_LiveGrabbing flag to false to prevent new grab commands from being enqueued.
When all commands currently being in the queue are finished the NotifyQuit message will turn up 
in the #GrabThreadProc.
*/
LRESULT CMainFrame::OnGrabStop(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  try
  {
    // This will prevent new images from being enqueued
    m_LiveGrabbing = false;
    
    // Enqueue a "clean-up" message which will show up after all images being currently
    // in the queue have been received
    m_Bcam.Notify(NotifyQuit, NULL);
  }
  CATCH_MSGBOX( "CMainFrame::OnGrabStop" )        
    
    return 0;
}

/*!
Called if the #GrabThreadProc receives a #NotifyQuit message. This means that the command queue is
empty now and everything can be cleaned up.
*/
LRESULT CMainFrame::OnGrabStopped(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
  try
  {
    // Clean up queue
    while(0 < m_BufferQueue.size())
      m_BufferQueue.pop();
    
    // forget about the displayed buffer
    m_DisplayedBufferIndex = -1;
    
    // switch off camera (in case we're using ContinuousShot)
    if(!USE_ONESHOT)
      m_Bcam.ContinuousShot = false;
    
    // shutdown thread
    m_GrabThread.Release();  // this waits for the thread to die

    // Cancel pending I/O requests.
    // This is optional since closing the driver will cancel pending requests automatically
    m_Bcam.Cancel();

    // Release bandwidth, isoch channels and isoch transfer contexts 
    // This is optional since closing the driver will trigger this automatically.
    // Warning! Calling FreeResources is not allowed, if there are still pending I/O requests, 
    // so call Cancel() first
    m_Bcam.FreeResources();
    
    // Close driver 
    m_Bcam.Close();
    
    // We could take away the Bitmap from the view
    //but since it's reference counted we leavi ti there for display
    //m_view.SetBitmap(NULL);
    
    // Delete bitmaps
    for(int i=0; i<NUM_BUFFERS; ++i)
      m_ptrBitmaps[i].Release();
    
    // Reset frame rate display
    m_Fps.Reset();
    m_StatusBar.SetPaneText(ID_FPS_PANE, "");
    
  }
  CATCH_MSGBOX( "CMainFrame::OnGrabStopped" )     
    
    return 0;
}


/*!
Called if an error occurred in the #GrabThreadProc. Shows an error message box.
*/
LRESULT CMainFrame::OnError(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  // Show the error message coming from another thread
  BcamException* pE = (BcamException*)wParam;
  CString Buffer, B;
  Buffer += (B.Format("Exception 0x%X occurred\n", pE->Error() ), B);
  Buffer += (B.Format("Message = %s\n", pE->Description() ), B);
  Buffer += (B.Format("Context = %s\n", pE->Context()), B);
  MessageBox(Buffer, _T("CMainFrame::OnError"), MB_OK | MB_ICONEXCLAMATION);
  
  delete pE;
  
  return 0;
}


/*!
Makes sure the application terminates cleanly if closed via the system menu
*/
LRESULT CMainFrame::OnSysCommand(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
  if(wParam == SC_CLOSE)
  {
    if(m_LiveGrabbing)
    {
      
      // Enqueue a "clean-up" message which will show up after all images being currently
      // in the queue have been received
      m_Bcam.Notify(NotifyQuit, NULL);
      
      // Wait until the GrabThread died
      ::WaitForSingleObject((HANDLE)m_GrabThread,INFINITE);
      
      // The rest will clean up itselfe via the destructors
    }
    
  }
  
  bHandled = false;
  
  return 0;
}

LRESULT CMainFrame::OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  if((bool)m_view.m_ptrBitmap)
    m_view.ReleaseBitmap();
  
  return 0;
}


LRESULT CMainFrame::OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  CFileDialog dlg(TRUE, _T("bmp"), NULL, 0, _T("Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0"), m_hWnd);
  if(dlg.DoModal() == IDOK)
  {
    try
    {
      CDibPtr ptrBitmap;
      ptrBitmap.LoadBMP(dlg.m_szFileName);
      m_view.SetBitmap(ptrBitmap);
    }
    CATCH_MSGBOX( "CMainFrame::OnFileOpen" )        
  }
  
  return 0;
}

LRESULT CMainFrame::OnFileSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  if(!m_view.m_ptrBitmap)
    return 0;
  
  CFileDialog dlg(FALSE, _T("bmp"), _T("Temp.bmp"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0"), m_hWnd);
  if(dlg.DoModal() == IDOK)
  {
    try
    {
      m_view.m_ptrBitmap->StoreBMP(dlg.m_szFileName);
    }
    CATCH_MSGBOX( "CMainFrame::OnFileSave" )        
  }
  
  return 0;
}

LRESULT CMainFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  try
  {
    if(m_LiveGrabbing)
    {
      
      // Enqueue a "clean-up" message which will show up after all images being currently
      // in the queue have been received
      m_Bcam.Notify(NotifyQuit, NULL);
      
      // Wait until the GrabThread died
      ::WaitForSingleObject((HANDLE)m_GrabThread,INFINITE);
      
      // The rest will clean up itselfe via the destructors
    }
  }
  CATCH_MSGBOX( "CMainFrame::OnFileExit" )
    
    PostMessage(WM_CLOSE);
  
  return 0;
}

LRESULT CMainFrame::OnEditCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  try
  {
    if(!m_LiveGrabbing && (bool)m_view.m_ptrBitmap)
    {
      m_view.m_ptrBitmap->CopyToClipboard();
    }
  }
  CATCH_MSGBOX( "CMainFrame::OnEditCopy" )
    
    return 0;
}

void CMainFrame::UpdateUI()
{
  // update UI elements
  UIEnable(ID_FILE_NEW, !m_LiveGrabbing);
  UIEnable(ID_FILE_OPEN, !m_LiveGrabbing);
  UIEnable(ID_FILE_SAVE, !m_LiveGrabbing && (bool)m_view.m_ptrBitmap);
  UIEnable(ID_GRAB_LIVE, !m_LiveGrabbing);
  UIEnable(ID_GRAB_STOP, m_LiveGrabbing);
  UIEnable(ID_EDIT_COPY, !m_LiveGrabbing && (bool)m_view.m_ptrBitmap);
  
  // These functions are not yet implemented so they have to be disabled always
  UIEnable(ID_FILE_PRINT, false);
  UIEnable(ID_FILE_PRINT_PREVIEW, false);
  UIEnable(ID_FILE_PRINT_SETUP, false);
  
  UIUpdateToolBar();
}

⌨️ 快捷键说明

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