Code sample Home

Connect / disconnect camera device
// Mode:
//   0 - disconnect
//   1 - connect by dialog
//   2 - connect last device
//-----------------------------------------------
void DemoCameraConnect (int Mode)
{
  int    i, n;
  BOOL   bConnected, bRet;
  WCHAR  szName[256];

  bConnected = lcPropGetBool( 0, LC_PROP_G_CAMERA_ON );
  if (Mode == 0){
    if (bConnected){
      // disconnect camera
      bRet = lcCameraDisconnect();
    }
  }else{
    if (bConnected == false){
      if (Mode == 1){
        // select camera by dialog
        bRet = lcCameraConnect( L"<Dialog>" );
      }else{
        if (Mode == 2){
          // get a number of available cameras
          n = lcPropGetInt( 0, LC_PROP_G_CAMERA_COUNT );
          // get names of each camera
          for (i=0; i<n; ++i){
            // set camera index
            lcPropPutInt( 0, LC_PROP_G_CAMERA_I, i );
            // get name by current index
            wcscpy( szName, lcPropGetStr( 0, LC_PROP_G_CAMERA_NAME ));
          }
          // connect last camera
          if (szName[0]){
            bRet = lcCameraConnect( szName );
          }
        }
      }
    }
  }
  bConnected = lcPropGetBool( 0, LC_PROP_G_CAMERA_ON );
  if (bConnected){
    // set update time (msec.)
    lcPropPutInt( 0, LC_PROP_G_CAMERA_TIME, 100 );
    ::MessageBox( 0, L"Camera is connected", L"Demo", MB_OK );
  }else{
    ::MessageBox( 0, L"Camera is disconnected", L"Demo", MB_OK );
  }
}
Add Camera view entity into a drawing
//-----------------------------------------------
void DemoCameraView (HANDLE hLcWnd)
{
  double Wcam, Hcam, Ratio;
  double Lef, Bot, Rig, Top, Wview, Hview, Gap;
  BOOL   bConnected;
  HANDLE hBlock;

  bConnected = lcPropGetBool( 0, LC_PROP_G_CAMERA_ON );
  if (bConnected){
    // get camera image size
    Wcam = lcPropGetInt( 0, LC_PROP_G_CAMERA_WIDTH );
    Hcam = lcPropGetInt( 0, LC_PROP_G_CAMERA_HEIGHT );
    Ratio = Wcam / Hcam;
    // get a block, linked with CAD window
    hBlock = lcPropGetHandle( hLcWnd, LC_PROP_WND_BLOCK );
    // add "camera view" object
    Lef = 0.0;
    Bot = 0.0;
    Wview = 100.0;
    Hview = Wview / Ratio;
    lcBlockAddCamview( hBlock, Lef, Bot, Wview, Hview );
    Rig = Lef + Wview;
    Top = Bot + Hview;
    Gap = Wview / 4.0;
    lcWndZoomRect( hLcWnd, Lef-Gap, Bot-Gap, Rig+Gap, Top+Gap );
  }else{
    ::MessageBox( 0, L"Camera is disconnected", L"Camera View", MB_OK );
    lcWndSetFocus( hLcWnd );
  }
}
Get snapshot image from a camera
//-----------------------------------------------
void DemoCameraShot (HANDLE hLcWnd)
{
  int Time;

  // get camera update time
  Time = lcPropGetInt( 0, LC_PROP_G_CAMERA_TIME );
  if (Time > 0){
    // do not update camera by timer
    lcPropPutInt( 0, LC_PROP_G_CAMERA_TIME, 0 );
  }
  // update camera view
  lcCameraShot();
  lcWndRedraw( hLcWnd );
}
Access data of camera image and save the image into a file
//-----------------------------------------------
void DemoCameraMem (HANDLE hLcWnd)
{
  BYTE* pBuf;
  int   nBytesPerRow, W, H;
  BITMAPFILEHEADER bfh;
  BITMAPINFOHEADER bih;
  FILE* df;
  WCHAR szFileName[256];
  HWND  hWnd;

  // generate filename
  swprintf( szFileName, L"d:/Image_%d.bmp", ::GetTickCount() );

  pBuf = (BYTE*)lcPropGetHandle( 0, LC_PROP_G_CAMERA_BITS );
  nBytesPerRow = lcPropGetInt( 0, LC_PROP_G_CAMERA_BPROW );
  W = lcPropGetInt( 0, LC_PROP_G_CAMERA_WIDTH );
  H = lcPropGetInt( 0, LC_PROP_G_CAMERA_HEIGHT );

  // Set BITMAPINFOHEADER
  memset( &bih, 0, sizeof(BITMAPINFOHEADER) );
  bih.biSize          = 40;       // header size, 40
  bih.biWidth         = W;        // image width, pixel
  bih.biHeight        = -H;       // image height, pixel
  bih.biPlanes        = 1;       
  bih.biCompression   = BI_RGB;
  bih.biXPelsPerMeter = 5905;     // preferable resolution, pixel/meter
  bih.biYPelsPerMeter = 5905;     // ---- on Y
  bih.biBitCount      = 24;       // bits per pixel
  bih.biSizeImage     = nBytesPerRow * H;   // size of image data (pBuf), bytes
  // sat data for file header
  memset( &bfh, 0, sizeof(bfh) );
  bfh.bfType = 0x4D42;   // BM
  // file size
  bfh.bfSize = 14 + bih.biSize + bih.biSizeImage;
  // offset to bitmap bits
  bfh.bfOffBits = 14 + bih.biSize;  // sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
  // write the image file (BMP)
  df = _wfopen( szFileName, L"wb" );
  if (df){
    // write file header
    fwrite( &bfh, 14, 1, df );
    // write bitmap info header
    fwrite( &bih, 40, 1, df );
    // write bitmap bits
    fwrite( pBuf, 1, bih.biSizeImage, df );
    // finish
    fclose( df );
    hWnd = (HWND)lcPropGetHandle( hLcWnd, LC_PROP_WND_HWND );
    ::MessageBox( hWnd, szFileName, L"SAVED", MB_OK );
    lcWndSetFocus( hLcWnd );
  }
}
Used functions:

lcCameraConnect
lcCameraDisconnect
lcCameraShot
lcBlockAddCamview

lcWndZoomRect
lcWndSetFocus
lcWndRedraw

lcPropGetBool
lcPropGetInt
lcPropGetHandle
lcPropPutInt