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

📄 cameramanager.cpp

📁 BCAM 1394 Driver
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//  (c) 2002 by Basler Vision Technologies
//  Section:  Vision Components
//  Project:  BCAM
//  $Header: CameraManager.cpp, 23, 04.07.2006 11:03:27, Happe, A.$
//-----------------------------------------------------------------------------
/**
  \file     CameraManager.cpp
 *
 * \brief Implementation of the CCameraManager class.
 *
 */
//-----------------------------------------------------------------------------


#include "stdafx.h"
#include "CameraManager.h"
#include "mainfrm.h"
#include "ChildFrm.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCameraManager::CCameraManager(CMainFrame& MainFrame)
  : m_MainFrame(MainFrame),
    m_pCurrentDevice(NULL),
    m_SelectedDeviceName("")
{
}

CCameraManager::~CCameraManager()
{
  for ( DevName2Camera_t::iterator it = m_DevName2Camera.begin(); it != m_DevName2Camera.end(); ++ it )
  {
    try
    {
      delete it->second;
    }
    catch ( BcamException & )
    { /* NOP */ }
  }

}

//------------------------------------------------------------------------------
// CCamera* CCameraManager::GetDevice(CString DeviceName)
//------------------------------------------------------------------------------
/**
 * Returns the camera object for a given device name 
 * \param     DeviceName
 * \return the camera object. NULL is returned, if no object has been found  
 */
//------------------------------------------------------------------------------
CCamera* CCameraManager::GetDevice(CString DeviceName)
{
  DeviceName.MakeUpper();
  DevName2Camera_t::iterator it = m_DevName2Camera.find(DeviceName);
  
  return it == m_DevName2Camera.end() ? NULL : it->second;
}

//------------------------------------------------------------------------------
// void CCameraManager::CurrentDeviceChanged(CString* pDeviceName)
//------------------------------------------------------------------------------
/**
 * The bus viewer informs us that the user has selected an other device. 
 * If the pointer to the device name is NULL, currently no device is active (perhaps all
 *  devices have been removed)
 *
 * \param     pDeviceName pointer to the name of the devive. If it is NULL, currently no device
 *            is selected
 * \return    0 new device could be opened or no device is selected, error code otherwise
 */
//------------------------------------------------------------------------------
DWORD CCameraManager::CurrentDeviceChanged(CString* pDeviceName)
{
  CCamera* pOldDevice = m_pCurrentDevice;
  DWORD result = 0;

  m_SelectedDeviceName = pDeviceName == NULL ? "" : *pDeviceName;

  if ( pDeviceName == NULL )
  {
    // Actually in the bus view no BCAM object is selected. If there is no MDI associated
    // with m_pCurrentDevice, m_pCurrentDevice is set to NULL. Otherwise m_pCurrentDevice 
    // remains unchanged, i.e. m_pCurrentDevice corresponds to the MDI window.
    if ( GetCurrentChild() == NULL )
    {
      // no child window exists, set current device to NULL
      m_pCurrentDevice = NULL;
    }
    else
    {
      // current device remains unchanged
      return 0;
    }
  }
  else 
  {
    CCamera * pDevice = GetDevice(*pDeviceName);
    if ( pDevice == m_pCurrentDevice )
      return 0;  // nothing has changed
    m_pCurrentDevice = pDevice;
  }

  // open the new device
  try
  {
    if ( m_pCurrentDevice != NULL )
      m_pCurrentDevice->Activate();
    // close the previously selected camera, if it is not grabbing
    if ( pOldDevice != NULL &&  pOldDevice->IsOpen() && ! pOldDevice->IsGrabActive() )
    {
      try
      {
        pOldDevice->Deactivate();
      }
      CATCH_REPORT();
    }
  }
  catch(BcamException& e)
  {
    // activating the new device failed, the old device remains the current device
    m_pCurrentDevice = pOldDevice;
    // Error when opening the device (e.g. the device is allocated by another application )
    if ( e.Error() == ERROR_BUSY )
    {
      // Device is already used by an other application.
      // Create a more meaningful error message than "the requested resource is in use".
      ReportError(BcamException(E_DEVICE_ALREADY_OPENED, e.Context()));
      result = E_DEVICE_ALREADY_OPENED;
    }
    else
    {
      ReportError(e);
      result = e.Error();
    }
  }
  catch ( ... ) 
  { 
    BcamException e(DISP_E_EXCEPTION); 
    ReportError(e); 
    result = e.Error();
  } 


  // inform the main frame
  m_MainFrame.CurrentDeviceChanged(m_pCurrentDevice);

  // activate associated MDI child, if any exists
  if ( m_pCurrentDevice != NULL )
  {
    Device2ChildFrame_t::iterator it = m_Device2ChildFrame.find(m_pCurrentDevice);
    if ( it != m_Device2ChildFrame.end() )
    {
      // a MDI child exists, activate it
     CChildFrame* pChild = it->second;
     pChild->SetFocus();
    }
  }
  return result;
}

//------------------------------------------------------------------------------
// void CCameraManager::MDIChildClosed(CChildFrame* pChild)
//------------------------------------------------------------------------------
/**
 * A MDI child window informs us that it has been closed.
 *
 * \param     pChild
 * \return   void
 */
//------------------------------------------------------------------------------
void CCameraManager::MDIChildClosed(CChildFrame* pChild)
{
  
  try
  {
    Device2ChildFrame_t::iterator it;
	for (it = m_Device2ChildFrame.begin(); it != m_Device2ChildFrame.end(); ++it )
    {
      if ( it->second == pChild )
      {
        CCamera* pCamera = it->first;
        // cancel an active grab
        if ( pCamera->IsGrabActive() )
        {
          try
          {
            pCamera->GrabCancel();
            // If the camera is no longer selected, close it to allow other applications to access it
            if ( pCamera != m_pCurrentDevice && pCamera->IsOpen() )
            {
              pCamera->Deactivate();
            }
          }
          CATCH_REPORT();
        }

        m_Device2ChildFrame.erase(it);
        pCamera->SetMDIChild(NULL);


        break;
      }
    }
    assert ( it != m_Device2ChildFrame.end() );

    // If the user has selected a non BCAM node in the BusView window and no other 
    // child frames exist, there is no current device. In this case call the CurrentDeviceChanged()
    // method to update the CameraManager's status
    if ( m_Device2ChildFrame.size() == 0 && m_SelectedDeviceName == "" )
      CurrentDeviceChanged(NULL);

  }
  CATCH_REPORT();
}
 
//------------------------------------------------------------------------------
// void CCameraManager::MDIChildActivated(CChildFrame* pChild)
//------------------------------------------------------------------------------
/**
 * A MDI child informs us that it has been activated by the user.
 * \param     pChild Pointer to the child which has been activated
 */
//------------------------------------------------------------------------------
void CCameraManager::MDIChildActivated(CChildFrame* pChild)
{
  for ( Device2ChildFrame_t::iterator it = m_Device2ChildFrame.begin(); it != m_Device2ChildFrame.end(); ++it )
  {
    if ( it->second == pChild )
    {
      CCamera* pCamera = it->first;
      CurrentDeviceChanged(&pCamera->m_FriendlyDeviceName);
      break;
    }
  }

}

//------------------------------------------------------------------------------
// void CCameraManager::AddDevice(CString DeviceName, HWND hWndNotify)
// 
// 
//------------------------------------------------------------------------------
/**
 * The bus view informs us about a new device found on the bus. 
 * Create a camera object for the new device. If the registry contains settings 
 * for the device, restore them.
 *
 * \param     DeviceName name of the device
 * \param     hWndNotify handle of window which receives the pnp notifications
 */
//------------------------------------------------------------------------------
void CCameraManager::AddDevice(CString DeviceName, HWND hWndNotify)
{
  CCamera* pCamera = new CCamera(DeviceName, hWndNotify, m_MainFrame);

  if ( pCamera == NULL )
    throw BcamException(E_OUTOFMEMORY, "CCameraManager::AddDevice()");

  pCamera->SetOnRemoveRequestCallback(OnRemoveRequest, this);
  pCamera->SetOnRemoveCompleteCallback(OnRemoveComplete, this);

  m_DevName2Camera[DeviceName] = pCamera;

}

//------------------------------------------------------------------------------
// void CCameraManager::RemoveDevice(CString DeviceName)
// Author: 
//------------------------------------------------------------------------------
/**
 * Tell the camera manager to remove a device. This method is called for devices
 * which were not opened when they have been removed
 *
 * \param     DeviceName
 * \return    void
 *
 */
//------------------------------------------------------------------------------
void CCameraManager::RemoveDevice(CString DeviceName)
{
  DeviceName.MakeUpper();
  CCamera* pCamera = GetDevice(DeviceName);
  if ( pCamera != NULL )
  {
    OnRemoveRequest(*pCamera, this);
    OnRemoveComplete(*pCamera, this);
  }
}
//------------------------------------------------------------------------------
// void CCameraManager::OnRemoveRequest(CBcam& Bcam, void* pv)
//------------------------------------------------------------------------------
/**
 * Called is to be removed or has been removed. The file handle of the device is
 * still valid.
 *
 * \param     Bcam The Bcam object which has been removed
 * \param     pv context information, in our case the instance pointer
 */
//------------------------------------------------------------------------------
void CCameraManager::OnRemoveRequest(CBcam& Bcam, void* pv)
{
  CCameraManager* This = (CCameraManager*) pv;
  
  DevName2Camera_t::iterator it = This->m_DevName2Camera.find(Bcam.Info.DeviceName());
  if ( it == This->m_DevName2Camera.end() )
  {
    // it seems that the device is ( no longer ) displayed in the tree view. On potential reason : 
    // The try to add the device has failed due to an error of the camera. 
    return;
  }

  // Destroy the threads related to grabbing
  it->second->DestroyThreads();


  // close an open MDI child. This will also stop an active grab
  Device2ChildFrame_t::iterator it2 = This->m_Device2ChildFrame.find(it->second);
  if ( it2 != This->m_Device2ChildFrame.end() )
  {
    // a MDI child exists, close it
    CChildFrame* pChild = it2->second;

    ReleaseCapture();  // A tracking rubberband will now stop tracking

    SendMessage(*pChild, WM_CLOSE, 0, 0);  // close the child window
  }

  
}

//------------------------------------------------------------------------------
// void CCameraManager::OnRemoveComplete(CBcam& Bcam, void* pv)
//------------------------------------------------------------------------------
/**
 * After a remove request initiated by the removal of one device we retrieve a 
 * remove complete notification. Now the cleanup for the device is done and we are
 * allowed to remove it from our map.
 *
 * \param     Bcam
 * \param     pv
 * \return void
 *
 */
//------------------------------------------------------------------------------
void CCameraManager::OnRemoveComplete(CBcam& Bcam, void* pv)
{
  CCameraManager* This = (CCameraManager*) pv;
  
  DevName2Camera_t::iterator it = This->m_DevName2Camera.find(Bcam.Info.DeviceName());
  if ( it == This->m_DevName2Camera.end() )
  {
    // it seems that the device is ( no longer ) displayed in the tree view. On potential reason : 
    // The try to add the device has failed due to an error of the camera. 
    return;
  }
  if ( it->second == This->m_pCurrentDevice )
  {
    // The current device has been removed 
    This->CurrentDeviceChanged(NULL);
  }
  try
  {
    // delete device
    delete it->second;
  }
  catch ( BcamException& e)
  {
    This->ReportError(e);
  }
  // remove the map entry
  This->m_DevName2Camera.erase(it);
}

//------------------------------------------------------------------------------
// void CCameraManager::AddMDIChild()
//------------------------------------------------------------------------------
/**
 * Opens a new window associated with the current device. 
 * If the registry key for the current device contains information about the window
 * layout and zoom factor, restore these information
 */
//------------------------------------------------------------------------------
void CCameraManager::AddMDIChild()
{
  assert ( m_pCurrentDevice != NULL );
  assert ( m_Device2ChildFrame.find(m_pCurrentDevice) == m_Device2ChildFrame.end() );  // only one window per device is allowed
  
  CChildFrame* pChild = new CChildFrame(*this, m_MainFrame);

⌨️ 快捷键说明

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