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

📄 mainframe.cpp

📁 BCAM 1394 Driver
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
//  (c) 2002 by Basler Vision Technologies
//  Section:  Vision Components
//  Project:  BCAM
//  $Header: MainFrame.cpp, 8, 07.11.2002 19:38:01, Happe, A.$
//-----------------------------------------------------------------------------
/**
  \file     MainFrame.cpp
  \brief    Implementation of the CMainFrame class.
*/

#include "stdafx.h"

#include <float.h>
#include <math.h>
#include <vfw.h>
#include "resource.h"
#include "MainFrame.h"

//! Edge length of the AOI in part per min(ImageWidth, ImageHeight)
const double AOI_SIZE = 0.20;  // = 20%

//! Start position of the AOI
const CPoint AOI_START_POSITION(40,5);

//! The speed vector moving the AOI
const CPoint AOI_SPEED_VECTOR(21,7);

/*!
Creates the window and initializes all GUI objects.
*/
LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  try
  {
    // create command bar window
    HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
    // attach menu
    m_CmdBar.AttachMenu(GetMenu());
    // load command bar images
    m_CmdBar.LoadImages(IDR_MAINFRAME);
    // remove old menu
    SetMenu(NULL);
    
    HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
    
    CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
    AddSimpleReBarBand(hWndCmdBar);
    AddSimpleReBarBand(hWndToolBar, NULL, TRUE);
    
    // Create Status Bar
    CreateSimpleStatusBar("");
    m_StatusBar.SubclassWindow(m_hWndStatusBar);
    int arrParts[] =
    {
      ID_STATUS_PANE,
        ID_DEFAULT_PANE,
        ID_FPS_PANE
    };
    m_StatusBar.SetPanes(arrParts, sizeof(arrParts) / sizeof(int), false);
    CString StatusPaneText;
    StatusPaneText.LoadString(ID_STATUS_PANE);
    m_StatusBar.SetPaneText(ID_STATUS_PANE, (LPCSTR)StatusPaneText);
    
    // create view window
    m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);
    m_view.ReleaseBitmap();
    
    UIAddToolBar(hWndToolBar);
    UISetCheck(ID_VIEW_TOOLBAR, 1);
    UISetCheck(ID_VIEW_STATUS_BAR, 1);
    
    // register object for message filtering and idle updates
    CMessageLoop* pLoop = _Module.GetMessageLoop();
    ATLASSERT(pLoop != NULL);
    pLoop->AddMessageFilter(this);
    pLoop->AddIdleHandler(this);
    
  }
  CATCH_MSGBOX( "CMainFrame::OnCreate" )  
    
    return 0;
}

void CMainFrame::SubmitGrabCommand(unsigned int BufferIndex)
{
  // Get the current AOI position
  CPoint AoiPosition;
  m_AoiBouncer.GetAoiPosition(m_CycleCounter, AoiPosition);
  
  // Submit a commad to set the AOI poition
  m_Bcam.FormatSeven[DCS_Mode0].Position.SetAsync(AoiPosition);
  
  // Remeber the position
  m_AoiPositions[BufferIndex] = AoiPosition;
  
  // Submit a grab command
  m_Bcam.GrabImageAsync(
    (char*)m_ptrBitmaps[BufferIndex]->GetPixels(), 
    m_ptrBitmaps[BufferIndex]->GetTotalPixelBytes(), 
    (void*)BufferIndex,
    false);  
  
  // Prepare the next cycle
  ++m_CycleCounter;
}


/*!
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.
*/
LRESULT CMainFrame::OnGrabLive(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  
  try
  {
    // Check for a camera
    if(CBcam::DeviceNames().size() == 0)
    {
      MessageBox("No camera present", _T("Error"), MB_OK | MB_ICONEXCLAMATION);
      return 0;
    }
    
    // Get the devicename of the first camera
    CString DeviceName = *(CBcam::DeviceNames().begin());
    ATLTRACE(_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;
    
    // Tell the BitmapView about the imageIsze
    CSize ImageSize = m_Bcam.FormatSeven[VideoMode].MaxSize();
    m_view.SetImageSize(ImageSize);
    
    // Set Area of Interest tp 10% of what is smaller - height or width
    // Beware : Windows Bitmaps have to have a DWORD alligned width :-(;
    unsigned long AoiLenght = (unsigned long)(AOI_SIZE * min(ImageSize.cx, ImageSize.cy)) & ~3;
    CSize AoiSize(AoiLenght, AoiLenght);
    CPoint AoiPosition(0,0);
    m_Bcam.FormatSeven[VideoMode].Position = AoiPosition;
    m_Bcam.FormatSeven[VideoMode].Size = AoiSize;
    
    // Init the bouncer
    m_AoiBouncer.Initialize(
      ImageSize, 
      AoiSize, 
      AOI_START_POSITION,
      AOI_SPEED_VECTOR
      );
    
    // 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)
    {
      SubmitGrabCommand(i);
    }
    
    // Switch on ISO_ENABLE
    m_Bcam.ContinuousShot = true;

  } 
  CATCH_MSGBOX( "CMainFrame::OnGrabLive" )        
    
    return 0;
}

/*!
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
  {
    // Wait for images and process them
    unsigned long RunningIndex = 0;
    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);
          
          // Identify the image
          unsigned int BufferIndex = *(unsigned int*)&pContext;
          
          // Notfy the GDI thread that the buffer with the BufferIndex has been grabbed
          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
        goto Cleanup;
        
        // 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;
  }
  
  
Cleanup:
  
  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.
*/
LRESULT CMainFrame::OnGrabFinished(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
  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);

⌨️ 快捷键说明

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