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

📄 mainfrm.cpp

📁 BCAM 1394 Driver
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
//  (c) 2002 by Basler Vision Technologies
//  Section:  Vision Components
//  Project:  BCAM
//  $Header: MainFrm.cpp, 5, 19.09.2002 17:34:51, Happe, A.$
//-----------------------------------------------------------------------------
/**
  \file     MainFrm.cpp
  \brief    Implementation of the CMainFrame class.
*/


#include "stdafx.h"
#include <float.h>
#include "BvcDib.h"
#include "LiveViewMFC.h"

#include "MainFrm.h"
#include "Utilities.h"

using namespace Bvc;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_SETFOCUS()
ON_WM_SYSCOMMAND()
ON_MESSAGE(WM_GRAB_FINISHED, OnGrabFinished)
ON_MESSAGE(WM_GRAB_STOPPED, OnGrabStopped)
ON_MESSAGE(WM_ERROR, OnError)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_GRAB_LIVE, OnGrabLive)
ON_COMMAND(ID_GRAB_STOP, OnGrabStop)
ON_COMMAND(ID_APP_EXIT, OnAppExit)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
ON_UPDATE_COMMAND_UI(ID_FILE_NEW, OnUpdateFileNew)
ON_UPDATE_COMMAND_UI(ID_FILE_OPEN, OnUpdateFileOpen)
ON_UPDATE_COMMAND_UI(ID_GRAB_LIVE, OnUpdateGrabLive)
ON_UPDATE_COMMAND_UI(ID_GRAB_STOP, OnUpdateGrabStop)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//! Describes the fileds in the status bar
static UINT indicators[] =
{
  ID_SEPARATOR,           // status line indicator
  ID_FPS_PANE
};


CMainFrame::CMainFrame() :  m_GrabFinished(false),
  m_LiveGrabbing(false),
  m_DisplayedBufferIndex(-1),
  m_IsBayerImage(false)
{
  
}
  
CMainFrame::~CMainFrame()
{
}

/*!
Creates the window and initializes all GUI objects.
*/
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    return -1;
  // create a view to occupy the client area of the frame
  if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
    CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
  {
    TRACE0("Failed to create view window\n");
    return -1;
  }
  
  if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  {
    TRACE0("Failed to create toolbar\n");
    return -1;      // fail to create
  }
  
  if (!m_wndStatusBar.Create(this) ||
    !m_wndStatusBar.SetIndicators(indicators,
    sizeof(indicators)/sizeof(UINT)))
  {
    TRACE0("Failed to create status bar\n");
    return -1;      // fail to create
  }
  m_wndStatusBar.SetPaneText(ID_FPS_PANE, "");
  
  // TODO: Delete these three lines if you don't want the toolbar to
  //  be dockable
  m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  EnableDocking(CBRS_ALIGN_ANY);
  DockControlBar(&m_wndToolBar);
  
  return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
  if( !CFrameWnd::PreCreateWindow(cs) )
    return FALSE;
  // TODO: Modify the Window class or styles here by modifying
  //  the CREATESTRUCT cs
  
  cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
  cs.lpszClass = AfxRegisterWndClass(0);
  return TRUE;
}

void CMainFrame::OnSetFocus(CWnd* pOldWnd)
{
  // forward focus to the view window
  m_wndView.SetFocus();
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
  // let the view have first crack at the command
  if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
    return TRUE;
  
  // otherwise, do default handling
  return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

/*!
Called it the user wants to start a live grab. 
Opens the driver, initilizes the camera and enqueues #NUM_BUFFERS grab commands.
When the grab commands are finished they turn up in the #GrabThreadProc.
*/
void CMainFrame::OnGrabLive() 
{
  
  try
  {
    // Check for a camera
    if(CBcam::DeviceNames().size() == 0)
    {
      MessageBox("No camera present", _T("Error"), MB_OK | MB_ICONEXCLAMATION);
      return;
    }
    
    // Get the devicename of the first camera
    CString DeviceName = *(CBcam::DeviceNames().begin());
    TRACE(_T("Devicename = %s\n"), DeviceName);
    
    // Open the driver
    m_Bcam.Open( DeviceName );
    
    // Parameter set for the fastest monochrome format (valid assumption anly for Basler cameras)
    const DCSVideoFormat VideoFormat = DCS_Format7;
    const DCSVideoMode VideoMode     = DCS_Mode0;
    const DCSColorCode ColorCode     = DCSColor_Mono8;
    m_Bcam.SetVideoMode(VideoFormat, VideoMode);
    m_Bcam.FormatSeven[VideoMode].ColorCoding = ColorCode;
    
    // Check if the camera sends Bayer8 data instead of Mono8
    m_IsBayerImage = false;
    // Is it a Basler camera? - Check the vendor_id
    CString NodeID = m_Bcam.Info.NodeId();
    if(NodeID.Find("0x003053") == 0)
    {
      // Is it a color camera? - check for the letter 'c' in the modelname
      CString ModelName = m_Bcam.Info.ModelName();
      if(ModelName.Find("c") >= 0)
        m_IsBayerImage = true;
    }
    
    // Set Area Of Interest to maximum
    CSize ImageSize = m_Bcam.FormatSeven[VideoMode].MaxSize();
    ImageSize.cx = ImageSize.cx & ~3; // Beware : Windows Bitmaps have to have a DWORD alligned width :-(
    CPoint AoiPosition(0,0);
    CSize AoiSize(ImageSize);
    m_Bcam.FormatSeven[VideoMode].Position = AoiPosition;
    m_Bcam.FormatSeven[VideoMode].Size = AoiSize;
    
    // Set full speed
    unsigned long BytePerPacketMax = m_Bcam.FormatSeven[VideoMode].BytePerPacket.Max();
    m_Bcam.FormatSeven[VideoMode].BytePerPacket = BytePerPacketMax;
    
    // Create suitable bitmaps
    for(int i=0; i<NUM_BUFFERS; ++i)
      m_ptrBitmaps[i].Create(AoiSize, 8, CDib::TopDown, CDib::Monochrome);
    
    // Allocate Resources 
    m_Bcam.AllocateResources(NUM_BUFFERS, AoiSize.cx * AoiSize.cy );
    
    // Start the grab thread
    if ( ! m_GrabThread.Create(&GrabThreadProc, this, THREAD_PRIORITY_HIGHEST) )
    {
      throw  BcamException(::GetLastError(), "CMainFrame::OnGrabLive : Create Grab Thread");
    }
    
    // Indicate that we're grabbing live now
    m_LiveGrabbing = true;
    
    m_StopWatch.Start();
    
    // Enqueue Grab commands 
    for(i=0; i<NUM_BUFFERS; ++i)
    {
      m_Bcam.GrabImageAsync((char*)m_ptrBitmaps[i]->GetPixels(), AoiSize.cx * AoiSize.cy, (void*)i, USE_ONESHOT);  
    }
    
    // Switch on the camera (in case we're using ContinuousShot)
    if(!USE_ONESHOT)
      m_Bcam.ContinuousShot = true;
    
  } 
  CATCH_MSGBOX( "CMainFrame::OnGrabLive" )        
    
}

/*!
Waits on the completion port for finished commands to show up.
- In case of a a finished grab command a #WM_GRAB_FINISHED message is send to the GUI thread
causing the #OnGrabFinished method to be fired.
- In case a #NotifyQuit message turns up a #WM_GRAB_STOPPED message is send to the GUI thread
causing the #OnGrabStopped method to be fired.
- If an error occurs a #WM_ERROR message is send to the GUI thread
causing the #OnError method to be fired.
*/
DWORD CMainFrame::GrabThreadProc(void* pParameter)
{
  CMainFrame* This = (CMainFrame*) pParameter;  // instance pointer
  
  try
  {
    
    for(;;)
    {
      FunctionCode_t FunctionCode;
      unsigned long ErrorCode;
      void *pContext;
      
      // Wait for things to show up at the driver's completion port
      This->m_Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, INFINITE); 
      
      if(ErrorCode)
        throw BcamException(ErrorCode, "CMainFrame::GrabThreadProc");
      
      switch(FunctionCode)
      {
      case AsyncGrabImage:
        {
          // Stop the watch and restart it again immediately
          float ElapsedTime = (float)This->m_StopWatch.Stop(true);
          
          // Notfy the GDI thread that the buffer with the BufferIndex has been grabbed
          unsigned int BufferIndex = *(unsigned int*)&pContext;
          This->PostMessage(WM_GRAB_FINISHED, *(WPARAM*)&BufferIndex, *(LPARAM*)&ElapsedTime ); 
        }
        break;
        
        // This means the queue is empty now ==> suicide
      case NotifyQuit:
        // Tell the GdiThread to clean up
        This->PostMessage(WM_GRAB_STOPPED, 0, 0 ); 
        
        // Finish thread
        return 0;
        
        // Don't care about other function codes like AsyncSetGain etc.
      default:
        ErrorCode = 1;
        break;
      }
    }
  }
  catch (BcamException&  e )
  {
    // Forward error to GdiThread for display
    BcamException* pE = new BcamException(e);
    This->PostMessage(WM_ERROR, (unsigned int)pE, 0); 
    return e.Error();
  }
  catch (...)
  {
    // Forward error to GdiThread for display
    BcamException* pE = new BcamException(DISP_E_EXCEPTION, "CMainFrame::GrabThreadProc");
    This->PostMessage(WM_ERROR, (unsigned int)pE, 0); 
    return DISP_E_EXCEPTION;
  }
  
  return 0;
}

/*!
Called if the #GrabThreadProc receives a finished grab command. The wParam includes the buffer's index.
Enqueues the buffer's index to the #m_BufferQueue. The BufferQueue will be emplied by #OnIdle.
*/
afx_msg LRESULT CMainFrame::OnGrabFinished(WPARAM wParam, LPARAM lParam)
{
  try
  {
    // store the index of the grabbed buffer in the buffer queue
    m_BufferQueue.push(*(unsigned int*)&wParam);
    
    // Update framerate statistics
    float ElapsedTime = *((float*)&lParam);
    if(ElapsedTime > FLT_EPSILON || ElapsedTime < -FLT_EPSILON)
      m_Fps.Add(1.0 / ElapsedTime);
    
    // Show framerate
    CString Buffer;
    Buffer.Format("%3.1f fps", m_Fps.Avg());
    m_wndStatusBar.SetPaneText(ID_FPS_PANE, Buffer);
  }
  CATCH_MSGBOX( "CMainFrame::OnGrabFinished" )            
    
    return 0;
}

⌨️ 快捷键说明

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