Code: Select all
#include <winsock2.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <optitrack.h>
#include <iostream>
using namespace std;
#define BUFFER_FRAME_SIZE 640*480
bool TimeToClose = false;
INPCameraCollection * cameraCollection;
INPCamera *camera;
INPCameraFrame * frame;
INPCameraFrame * tempframe;
INPVector *pVector;
BYTE *Buffer;
long *pVal;
static unsigned char bufFrameCurrent[BUFFER_FRAME_SIZE];
#define HRCHECK(message) if (FAILED(hr)) { fprintf(stderr, "%s\n", message); return 0; }
int main(int argc, char* argv[])
{
	HRESULT hr;
	LONG count;
	LONG misc;
	LONG waittime;
	LONG pVal;
	long width, height;
	VARIANT vx, vy, vz;
	VARIANT vyaw, vpitch, vroll;
	VARIANT voption;
	VARIANT_BOOL corrupt;
	int running = 1;
	int toerrors = 0;
	int windowWidth = 0;
	int windowHeight = 0;
	int frameCounter = 0;
	// probably not needed, but doesn't hurt:
	VariantInit(&vx);
	VariantInit(&vy);
	VariantInit(&vz);
	VariantInit(&vyaw);
	VariantInit(&vpitch);
	VariantInit(&vroll);
	VariantInit(&voption);
	CoInitialize(NULL);  // Startup Microsoft crap
	// create CameraCollection and Vector objects:
	hr = CoCreateInstance(__uuidof(NPCameraCollection), NULL, CLSCTX_ALL,
			__uuidof(INPCameraCollection), (void **) &cameraCollection);
	HRCHECK("CoCreateInstance for CameraCollection");
	hr = CoCreateInstance(__uuidof(NPVector), NULL, CLSCTX_ALL,
			__uuidof(INPVector), (void **) &pVector);
	HRCHECK("CoCreateInstance for Vector");
	// find all the cameras:
	hr = cameraCollection->Enum();
	HRCHECK("CameraCollection->Enum");
	hr = cameraCollection->get_Count(&count);
	HRCHECK("CameraCollection->get_Count");
	cout << "There are " << count << endl;
	if (count < 1) 
	{
		fprintf(stderr, "camera count < 1\n");
		return 0;
	}
  // get one:
  hr = cameraCollection->Item(0, &camera);
  HRCHECK("CameraCollection->Item");
  camera->get_SerialNumber(&misc);
  camera->get_Width(&width);
  camera->get_Height(&height);
  windowWidth = width;
  windowHeight = height;
  printf("serial number = %ld\n", misc);
 
#if 0
  voption.vt = VT_BOOL;
  voption.boolVal = VARIANT_TRUE;
  //hr = pCamera->SetOption(NP_OPTION_SEND_EMPTY_FRAMES, voption);
  HRCHECK("Camera->SetOption");
#endif
	waittime = 100; // value in milliseconds; 0 = don't wait; INFINITE = wait till data
	//pVector->Reset();
	//HRCHECK("Vector->Reset");
	running = 6000; // run ~1 minute for waittime=10 ms
 
	IplImage* curFrame = cvCreateImage(cvSize(355, 290), IPL_DEPTH_8U, 1);
	//IplImage* cvFrame = cvCreateImage(cvSize(355, 290), IPL_DEPTH_8U, 1);
	//IplImage* curFrame;
	static unsigned char frameBuffer[355*290];
	unsigned char *data = (unsigned char *)(curFrame->imageData);
	cvNamedWindow( "Window", CV_WINDOW_AUTOSIZE);
	//cvResizeWindow("Window", 600, 290);
	if(count > 0)
	{
		cameraCollection->Item(0, &camera);
		{
			  hr = camera->Open();
			  // turn on illumination LEDs
			  hr = camera->SetLED(NP_LED_ONE, VARIANT_TRUE);
			  hr = camera->Start();
			  //camera->SetOption(NP_OPTION_EXPOSURE, (VARIANT) 5);
			  
				  while(!TimeToClose)
				  {
						Sleep(5);
						MSG msg;
						if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) ) 
						{
						   if(GetMessage( &msg, NULL, 0, 0 ) )
						   {
							 TranslateMessage(&msg);
							 DispatchMessage(&msg);
						   }
						}
						camera->GetFrame(0, &tempframe);
						frame = tempframe;
						while (tempframe!= NULL)
						{
							tempframe->get_Id(&pVal);
							cout << "fame numer: " << pVal << endl;
						    frame = tempframe;
						    //tempframe->Free();
						    tempframe->Release();
						    camera->GetFrame(0, &tempframe);
						}
 // while(running--) 
  //{
						if(frame != 0)
						{
							frameCounter++;
							camera->GetFrameImage(frame, windowWidth, windowHeight, windowWidth, 8, (byte *) bufFrameCurrent);
							frame->Free();
							frame->Release();
						}
					}		  
		}
	}
	 
#if 0
    if (FAILED(hr)) {
		cout << "WTF" << endl;
      // fprintf(stderr,"Camera->GetFrame %x ", hr);
      switch(hr) {
        case S_OK:
	  fprintf(stderr, "OK\n"); break;
        case E_POINTER:
	  fprintf(stderr, "E_POINTER\n"); break;
        case NP_E_DEVICE_DISCONNECTED:
	  fprintf(stderr, "NP_E_DEVICE_DISCONNECTED\n"); break;
        case NP_E_DEVICE_NOT_SUPPORTED:
	  fprintf(stderr, "NP_E_DEVICE_NOT_SUPPORTED\n"); break;
        //case HRESULT_FROM_WIN32(ERROR_TIMEOUT):
	  /*fprintf(stderr, "HRESULT_FROM_WIN32(ERROR_TIMEOUT)\n");*/
	  //toerrors++;
	  break;
        default:
	  fprintf(stderr, "default\n"); break;
      }
      continue;
    }
#endif
  // shut everything down
  camera->SetLED(NP_LED_ONE, VARIANT_FALSE);
  camera->Stop();
  camera->Close();
  CoUninitialize();
  return 0;
}