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

📄 samplecode.cpp

📁 BCAM 1394 Driver
💻 CPP
📖 第 1 页 / 共 3 页
字号:
      Bcam.Gain.AbsControl = false;
    }
    else
    {
      MessageBox("Absolute Values not available for feature Gain", _T("OnSetGain : Abs Values"), MB_OK | MB_ICONINFORMATION);
    }
    
  } catch( BcamException &e )
  {
    CString Buffer, B;
    Buffer += (B.Format("Exception 0x%X occurred\n", e.Error() ), B);
    Buffer += (B.Format("Message = %s\n", e.Description() ), B);
    Buffer += (B.Format("Context = %s\n", e.Context()), B);
    MessageBox(Buffer, _T("OnSetGain"), MB_OK | MB_ICONEXCLAMATION);
  }
  
  return 0;
}

LRESULT CMainFrame::OnGrabFormat7(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  /**** Simple ***/
  {
    // Get the devicename of the first camera
    std::list<CString> DeviceNames = CBcam::DeviceNames();
    if (DeviceNames.size() == 0) return 0;
    CString DeviceName = *(DeviceNames.begin());
    
    // Create the driver object and open the driver
    CBcam Bcam;
    Bcam.Open( DeviceName );
    
    // Set the Format, Mode and Color
    Bcam.SetVideoMode(DCS_Format7, DCS_Mode0, DCS_IgnoreFrameRate);
    Bcam.FormatSeven[DCS_Mode0].ColorCoding = DCSColor_Mono8;
    
    // Set AOI
    Bcam.FormatSeven[DCS_Mode0].Position = CPoint(0,0);
    Bcam.FormatSeven[DCS_Mode0].Size = CSize(100,100);
    
    // Set BytePerPacket to it's max value
    // Do this after(!) you have set format, mode and AOI because the max value might change
    Bcam.FormatSeven[DCS_Mode0].BytePerPacket = Bcam.FormatSeven[DCS_Mode0].BytePerPacket.Max();
    
    
    // Allocate Resources (max number of buffers, max buffer size)
    Bcam.AllocateResources(1, 100*100);
    
    // Grab the image with 3 sec timeout
    char pBuffer[100*100];
    Bcam.GrabImage(pBuffer, 100*100, 3000);  
    
  }
  
  
  /**** Elaborate ***/
  
  // Use exception handling
  try
  {
    // Get the devicename of the first camera
    ATLASSERT(CBcam::DeviceNames().size() > 0);
    CString DeviceName = *(CBcam::DeviceNames().begin());
    ATLTRACE(_T("Devicename = %s\n"), DeviceName);
    
    // Create the driver object and open the driver
    CBcam Bcam;
    Bcam.Open( DeviceName );
    
    // This is the parameter set for : max image size, Mono8, max. frame rate
    const DCSVideoFormat VideoFormat = DCS_Format7;
    const DCSVideoMode VideoMode   = DCS_Mode0;
    const DCSColorCode ColorCode     = DCSColor_Mono8;
    
    // Get information about this parameter set
    CString ColorCodeName           = BcamUtility::ColorCodeName(ColorCode);
    unsigned short BitsPerPixel     = BcamUtility::BitsPerPixel(ColorCode);
    CSize ImageSize                 = Bcam.FormatSeven[VideoMode].MaxSize();
    
    // Check, if these parameters are supported
    ATLASSERT(Bcam.IsVideoModeSupported(VideoFormat, VideoMode));
    ATLASSERT(Bcam.FormatSeven[VideoMode].ColorCoding.IsSupported(DCSColor_Mono8));
    
    // Set the Format, Mode and Color
    Bcam.SetVideoMode(VideoFormat, VideoMode, DCS_IgnoreFrameRate);
    Bcam.FormatSeven[VideoMode].ColorCoding = ColorCode;
    
    // Set Area Of Interest
    CPoint AoiPosition(0,0);
    CSize AoiSize(ImageSize);
    ATLASSERT( AoiPosition.x % Bcam.FormatSeven[VideoMode].Position.Inc().cx == 0);
    ATLASSERT( AoiPosition.y % Bcam.FormatSeven[VideoMode].Position.Inc().cy == 0);
    ATLASSERT( AoiSize.cx % Bcam.FormatSeven[VideoMode].Size.Inc().cx == 0);
    ATLASSERT( AoiSize.cy % Bcam.FormatSeven[VideoMode].Size.Inc().cy == 0);
    Bcam.FormatSeven[VideoMode].Position = AoiPosition;
    Bcam.FormatSeven[VideoMode].Size = AoiSize;
    
    // Set recommended speed
    unsigned long BytePerPacketMax = Bcam.FormatSeven[VideoMode].BytePerPacket.Max();
    unsigned long BytePerPacket = BytePerPacketMax;
    ATLASSERT( BytePerPacket % Bcam.FormatSeven[VideoMode].BytePerPacket.Inc() == 0);
    Bcam.FormatSeven[VideoMode].BytePerPacket = BytePerPacket;
    
    // Get more information about this parameter set (depending on the parameters already set)
    unsigned long BytePerFrame      = Bcam.FormatSeven[VideoMode].BytePerFrame();
    unsigned long PixelPerFrame     = Bcam.FormatSeven[VideoMode].PixelPerFrame();
    unsigned long PacketsPerFrame   = Bcam.FormatSeven[VideoMode].PacketsPerFrame();
    unsigned long PaddingBytes      = (BytePerPacket*PacketsPerFrame) - (BitsPerPixel*PixelPerFrame/8);
    
    // Tell the user
    CString Buffer, B;
    Buffer += (B.Format("VideoFormat       \t= %lu\n", VideoFormat), B);
    Buffer += (B.Format("VideoMode         \t= %lu\n", VideoMode), B);
    Buffer += (B.Format("ColorCode         \t= %s\n", ColorCodeName ), B);
    Buffer += (B.Format("BitsPerPixel      \t= %u\n", BitsPerPixel ), B);
    Buffer += (B.Format("ImageSize         \t= %lu x %lu\n", ImageSize.cx, ImageSize.cy ), B);
    Buffer += (B.Format("BytePerFrame      \t= %lu\n", BytePerFrame ), B);
    Buffer += (B.Format("PixelPerFrame     \t= %lu\n", PixelPerFrame ), B);
    Buffer += (B.Format("BytePerPacket     \t= %lu\n", BytePerPacket ), B);
    Buffer += (B.Format("PacketsPerFrame   \t= %lu\n", PacketsPerFrame ), B);
    Buffer += (B.Format("PaddingBytes      \t= %lu\n", PaddingBytes ), B);
    MessageBox(Buffer, _T("OnGrabFormat7"), MB_OK | MB_ICONINFORMATION);
    
    // Create a suitable bitmap
    CDibPtr ptrDib;
    ptrDib.Create( AoiSize, 8, CDib::TopDown, CDib::Monochrome );
    
    // Get a pointer to the image buffer
    char *pBuffer = (char*) ptrDib->GetPixels();
    
    // Allocate Resources (max num buffers, MaxImageSize
    Bcam.AllocateResources(1, AoiSize.cx * AoiSize.cy );
    
    // Grab the image
    Bcam.GrabImage(pBuffer, AoiSize.cx * AoiSize.cy, 3000);  // timeout 3 sec.
    
    // Show the image
    m_view.SetBitmap( ptrDib );
    
    
  } catch( BcamException &e )
    {
    CString Buffer, B;
    Buffer += (B.Format("Exception 0x%X occurred\n", e.Error() ), B);
    Buffer += (B.Format("Message = %s\n", e.Description() ), B);
    Buffer += (B.Format("Context = %s\n", e.Context()), B);
    MessageBox(Buffer, _T("OnGrabSingleImage"), MB_OK | MB_ICONEXCLAMATION);
  }
  
  return 0;
}

LRESULT CMainFrame::OnGrabNonBlocking(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  /**** Simple ***/
  {
     // Get the devicename of the first camera
    std::list<CString> DeviceNames = CBcam::DeviceNames();
    if (DeviceNames.size() == 0) return 0;
    CString DeviceName = *(DeviceNames.begin());
   
    // Create the driver object and open the driver
    CBcam Bcam;
    Bcam.Open( DeviceName );
    
    // Parametrize camera for 640 x 480, Mono8, 7.5 fps
    Bcam.SetVideoMode(DCS_Format0, DCS_Mode5, DCS_7_5fps);
    
    // Allocate Resources (max number of buffers, max buffer size)
    // Note that max num buffers = 2 since we're going to enqueue two grab commands
    Bcam.AllocateResources(2, 640*480 );
    
    // Enqueue two grab commands
    char pBuffer1[640*480];
    char pBuffer2[640*480];
    Bcam.GrabImageAsync(pBuffer1, 640*480);  
    Bcam.GrabImageAsync(pBuffer2, 640*480);  
    
    // Do something else...
    
    // Wait for the commands to finish
    FunctionCode_t FunctionCode;
    unsigned long ErrorCode;
    void *pContext;
    Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 3000); // timeout 3 sec.
    ATLTRACE("Buffer1 : Errorcode = 0x%x\n", ErrorCode);
    Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 3000); // timeout 3 sec.
    ATLTRACE("Buffer2 : Errorcode = 0x%x\n", ErrorCode);
    
  }
  
  
  /**** Elaborate ***/
  
  // Use exception handling
  try
  {
    // Get the devicename of the first camera
    ATLASSERT(CBcam::DeviceNames().size() > 0);
    CString DeviceName = *(CBcam::DeviceNames().begin());
    ATLTRACE(_T("Devicename = %s\n"), DeviceName);
    
    // Create the driver object and open the driver
    CBcam Bcam;
    Bcam.Open( DeviceName );
    
    // Parameter set for : 640 x 480, Mono8, 7.5 fps
    const DCSVideoFormat VideoFormat = DCS_Format0;
    const DCSVideoMode VideoMode = DCS_Mode5;
    const DCSVideoFrameRate VideoFrameRate = DCS_7_5fps;
    Bcam.SetVideoMode(VideoFormat, VideoMode, VideoFrameRate);
    
    // Create suitable a bitmap
    CSize ImageSize = BcamUtility::ImageSize(VideoFormat, VideoMode);
    CDibPtr ptrDib;
    ptrDib.Create( ImageSize, 8, CDib::TopDown, CDib::Monochrome );
    
    // Get a pointer to the image buffer
    char *pBuffer = (char*) ptrDib->GetPixels();
    
    // Allocate Resources (max num buffers, MaxImageSize
    Bcam.AllocateResources(1, ImageSize.cx * ImageSize.cy );
    
    // Enqueue a SetGain command (non-blocking)
    unsigned long Gain = Bcam.Gain.Raw();
    Bcam.Gain.Raw.SetAsync(Gain);
    
    // Enqueue a Grab command (non-blocking) AND  a OneShot command
    void *pContext = pBuffer;
    Bcam.GrabImageAsync(pBuffer, ImageSize.cx * ImageSize.cy, pContext);  
    
    // Wait for the three enqueued commands to succeed
    for(int i=0;i<3;++i)
    {
      FunctionCode_t FunctionCode;
      unsigned long ErrorCode;
      void *pContext;
      
      Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 3000); // timeout 3 sec.
      
      ATLASSERT(!ErrorCode);
      switch(FunctionCode)
      {
      case AsyncGainRawSet:
      case AsyncOneShot:
        break;
      case AsyncGrabImage:
        ATLASSERT(pContext == pBuffer);
        break;
      default:
        ATLASSERT(false);
      }
    }
    
    // Show the image
    m_view.SetBitmap( ptrDib );
    
  } catch( BcamException &e )
  {
    CString Buffer, B;
    Buffer += (B.Format("Exception 0x%X occurred\n", e.Error() ), B);
    Buffer += (B.Format("Message = %s\n", e.Description() ), B);
    Buffer += (B.Format("Context = %s\n", e.Context()), B);
    MessageBox(Buffer, _T("OnGrabSingleImage"), MB_OK | MB_ICONEXCLAMATION);
  }
  
  return 0;
}


LRESULT CMainFrame::OnGrabWithHardwareTrigger(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  // Use exception handling
  try
  {
    // Get the devicename of the first camera
    std::list<CString> DeviceNames = CBcam::DeviceNames();
    if (DeviceNames.size() == 0) return 0;
    CString DeviceName = *(DeviceNames.begin());
    
    // Create the driver object and open the driver
    CBcam Bcam;
    Bcam.Open( DeviceName );
    
    // Parametrize camera for 640 x 480, Mono8, 7.5 fps
    Bcam.SetVideoMode(DCS_Format0, DCS_Mode5, DCS_7_5fps);
    
    // Enable hardware trigger
    Bcam.Trigger.OnOff = true;
    if(Bcam.Trigger.IsModeSupported(DCS_TriggerMode0))
      Bcam.Trigger.Mode = DCS_TriggerMode0;
    if(Bcam.Trigger.IsSupported(inqPolarity))
      Bcam.Trigger.Polarity = HighActive;
    
    // Allocate Resources (max number of buffers, max buffer size)
    Bcam.AllocateResources(1, 640*480 );
    
    // Enqueue grab commands
    CDibPtr ptrDib;
    ptrDib.Create( CSize( 640, 480 ), 8, CDib::TopDown, CDib::Monochrome );
    
    // Get a pointer to the image buffer
    char *pBuffer = (char*) ptrDib->GetPixels();
    // Pass the pointer as destination and as context pointer
    Bcam.GrabImageAsync(pBuffer, 640*480, pBuffer );  
    
    // Wait 5 sec. for the hardware trigger to arrive
    Sleep(5000);
    
    // Check if the trigger has arrived
    FunctionCode_t FunctionCode;
    unsigned long ErrorCode;
    void *pContext;
    Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 0); // timeout 0
    ATLASSERT(ErrorCode == 0 && FunctionCode == AsyncOneShot);
    Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 0); // timeout 0
    if(!ErrorCode)
    {
      ATLASSERT(FunctionCode == AsyncGrabImage);
      ATLASSERT( ptrDib->GetPixels() == pContext );
      // Process the Dib 
      m_view.SetBitmap( ptrDib );
      MessageBox("Trigger arrived", _T("OnGrabWithHardwareTrigger"), MB_OK | MB_ICONEXCLAMATION);
    }
    else if ( ErrorCode == WAIT_TIMEOUT )
    {
      // We don't believe in the HW trigger any more
      // so we cancel the grab command

⌨️ 快捷键说明

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