MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/PlanktonTracker/";

    ui->delayLineEdit->setEnabled(false);
    ui->numPhotoLineEdit->setEnabled(false);
    ui->startButton->setEnabled(false);
    ui->stopButton->setEnabled(false);
    ui->trackButton->setEnabled(false);

    Pylon::PylonAutoInitTerm autoInitTerm;

    QActionGroup *cameraDevicesGroup = new QActionGroup(this);
    cameraDevicesGroup->setExclusive(true);

    try {

        CTlFactory& tlFactory = CTlFactory::GetInstance();

        // Get all attached devices.
        DeviceInfoList_t devices;
        if (tlFactory.EnumerateDevices(devices) == 0) {
            QString status = QString("No camera present.");
            ui->statusBar->showMessage(status);
        }

        // Create an array of instant cameras for the found devices and avoid exceeding a maximum number of devices.
        CInstantCameraArray cameras(min(devices.size(), c_maxCamerasToSee));

        for (size_t i = 0; i < cameras.GetSize(); ++i) {

            cameras[i].Attach(tlFactory.CreateDevice(devices[i]));

            QAction *cameraDeviceAction = new QAction((QString)cameras[i].GetDeviceInfo().GetModelName(), cameraDevicesGroup);
            cameraDeviceAction->setCheckable(true);
            cameraDeviceAction->setData(QVariant::fromValue(cameras[i].GetDeviceInfo()));
            if (i == 0) {
                cameraDeviceAction->setChecked(true);
                // Set as default camera and enable some UI elements
                if (setCamera(cameras[i].GetDeviceInfo())) {
                    ui->delayLineEdit->setEnabled(true);
                    ui->numPhotoLineEdit->setEnabled(true);
                    ui->startButton->setEnabled(true);
                }
            }
            ui->menuCamera->addAction(cameraDeviceAction);

        }

    } catch (GenICam::GenericException &e) {
        QString status = QString("An exception occurred: %1").arg((QString)e.GetDescription());
        ui->statusBar->showMessage(status);
    }

    connect(cameraDevicesGroup, SIGNAL(triggered(QAction*)), SLOT(updateCameraDevice(QAction*)));

}
Beispiel #2
0
GigaCamera::GigaCamera()
{
    //初始化相机运行时环境
    Pylon::PylonAutoInitTerm autoInitTerm;
    try
    {
        //find usable cameras
        CTlFactory& tlFactory = CTlFactory::GetInstance();
        IGigETransportLayer *pTL = dynamic_cast<IGigETransportLayer*>(tlFactory.CreateTl(BaslerGigEDeviceClass));
        if(pTL == NULL)
        {
            throw RUNTIME_EXCEPTION;
        }
        DeviceInfoList_t allDeviceInfos;
        if(pTL->EnumerateDevices(allDeviceInfos) == 0)
        {
            throw RUNTIME_EXCEPTION;
        }
        //将找到的相机放入可用相机数组
        usableDeviceInfos.push_back(allDeviceInfos[0]);
        const String_t subnet(static_cast<const CBaslerGigEDeviceInfo&>(allDeviceInfos[0]).GetSubnetAddress());
        for(size_t i=1;i<allDeviceInfos.size()&&usableDeviceInfos.size()<c_maxCamerasToUse;i++)
        {
            const CBaslerGigEDeviceInfo& gigeinfo=static_cast<const CBaslerGigEDeviceInfo&>(allDeviceInfos[i]);
            if(subnet==gigeinfo.GetSubnetAddress())
            {
                //将找到的可用相机的子网信息放入usableDeviceInfos中
                usableDeviceInfos.push_back(gigeinfo);
            }
            else
            {
                //error info

            }
        }

        cameras(usableDeviceInfos.size());

        for(size_t i=0;i<cameras.GetSize();i++)
        {
            //利用得到的可用相机设别实例化相机,放在cameras中
            cameras[i].Attach(tlFactory.CreateDevice(usableDeviceInfos[i]));
            //cameras[i].RegisterConfiguration(new CActionTriggerConfiguration(DeviceKey,));
            //相机上下文信息,即给相机编号,与获取到的图像信息对应
            cameras[i].SetCameraContext(i);

        }
    }
    catch(GenICam::GenericException &e)
    {
        //异常信息
        qDebug()<<e.GetDescription();

    }

}
	void CCameraBasler::initCamera(const char *strCameraName)
	{
#ifndef NDEBUG
		std::cout << "DEBUG: [Basler] Init Camera." << std::endl;
#endif

		try
		{
			bool Found = false;

			CTlFactory& TlFactory = CTlFactory::GetInstance();
			DeviceInfoList_t lstDevices;
			TlFactory.EnumerateDevices( lstDevices );
			if ( ! lstDevices.empty() )
			{
				DeviceInfoList_t::const_iterator it;
				for ( it = lstDevices.begin(); it != lstDevices.end(); ++it )
				{
					if ( it->GetFullName().find(strCameraName) != std::string::npos )
					{
						// Found
						Found = true;
						break;
					}
				}
				if (Found)
				{ 
					m_Camera = new CInstantCamera(CTlFactory::GetInstance().CreateDevice(it->GetFullName()));
					m_Camera->MaxNumBuffer = 15;

					// Open the camera
					if (!m_Camera->IsOpen())
						m_Camera->Open();

					return;
				}
				else
				{
					m_Camera = NULL;
					return;
				} 
			}
			return;
		}
		catch (...)
		{
			// Error handling
			std::cerr << "ERROR: [Basler] An exception occurred opening camera." << std::endl;
			m_Camera = NULL;
			return;
		}

		m_cameraState = CVCCameraInterface::INITIALIZED;  
	}
int main(int argc, char* argv[])
{
    // The exit code of the sample application.
    int exitCode = 0;

    // Automagically call PylonInitialize and PylonTerminate to ensure the pylon runtime system
    // is initialized during the lifetime of this object.
    Pylon::PylonAutoInitTerm autoInitTerm;

    try
    {
        // Declare a local counter used for waiting.
        int loopCount = 0;

        // Get the transport layer factory.
        CTlFactory& tlFactory = CTlFactory::GetInstance();

        // Create an instant camera object with the camera device found first.
        CInstantCamera camera( tlFactory.CreateFirstDevice());

        // Print the camera information.
        cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
        cout << "Friendly Name: " << camera.GetDeviceInfo().GetFriendlyName() << endl;
        cout << "Full Name    : " << camera.GetDeviceInfo().GetFullName() << endl;
        cout << "SerialNumber : " << camera.GetDeviceInfo().GetSerialNumber() << endl;
        cout << endl;

        // For demonstration purposes only, register another configuration event handler that handles device removal.
        camera.RegisterConfiguration( new CSampleConfigurationEventHandler, RegistrationMode_Append, Cleanup_Delete);

        // For demonstration purposes only, add a sample configuration event handler to print out information
        // about camera use.
        camera.RegisterConfiguration( new CConfigurationEventPrinter, RegistrationMode_Append, Cleanup_Delete);

        // Open the camera. Camera device removal is only detected while the camera is open.
        camera.Open();

        // Now, try to detect that the camera has been removed:

        // Ask the user to disconnect a device
        loopCount = c_loopCounterInitialValue;
        cout << endl << "Please disconnect the device (timeout " << loopCount / 4 << "s) " << endl;

        /////////////////////////////////////////////////// don't single step beyond this line  (see comments above)

        // Before testing the callbacks, we manually set the heartbeat timeout to a short value when using GigE cameras.
        // Since for debug versions the heartbeat timeout has been set to 5 minutes, it would take up to 5 minutes
        // until detection of the device removal.
        CHearbeatHelper heartbeatHelper(camera);
        heartbeatHelper.SetValue(1000);  // 1000 ms timeout

        try
        {
            // Get a camera parameter using generic parameter access.
            GenApi::CIntegerPtr width(camera.GetNodeMap().GetNode("Width"));

            // The following loop accesses the camera. It could also be a loop that is
            // grabbing images. The device removal is handled in the exception handler.
            while ( loopCount > 0)
            {
                // Print a "." every few seconds to tell the user we're waiting for the callback.
                if (--loopCount % 4 == 0)
                {
                    cout << ".";
                    cout.flush();
                }
                WaitObject::Sleep(250);

                // Change the width value in the camera depending on the loop counter.
                // Any access to the camera like setting parameters or grabbing images
                // will fail throwing an exception if the camera has been disconnected.
                width->SetValue( width->GetMax() - (width->GetInc() * (loopCount % 2)));
            }

        }
        catch (GenICam::GenericException &e)
        {
            if ( camera.IsCameraDeviceRemoved())
            {
                // The camera device has been removed. This caused the exception.
                cout << endl;
                cout << "The camera has been removed from the PC." << endl;
                cout << "The camera device removal triggered an exception:" << endl
                    << e.GetDescription() << endl;
            }
            else
            {
                // An unexpected error has occurred.
                // In this example it is handled by exiting the program.
                throw;
            }
        }

        if ( !camera.IsCameraDeviceRemoved())
            cout << endl << "Timeout expired" << endl;

        /////////////////////////////////////////////////// Safe to use single stepping (see comments above).

        // Now try to find the detached camera after it has been attached again:

        // Create a device info object for remembering the camera properties.
        CDeviceInfo info;

        // Remember the camera properties that allow detecting the same camera again.
        info.SetDeviceClass( camera.GetDeviceInfo().GetDeviceClass());
        info.SetSerialNumber( camera.GetDeviceInfo().GetSerialNumber());

        // Destroy the Pylon Device representing the detached camera device.
        // It cannot be used anymore.
        camera.DestroyDevice();

        // Ask the user to connect the same device.
        loopCount = c_loopCounterInitialValue;
        cout << endl << "Please connect the same device to the PC again (timeout " << loopCount / 4 << "s) " << endl;

        // Create a filter containing the CDeviceInfo object info which describes the properties of the device we are looking for.
        DeviceInfoList_t filter;
        filter.push_back( info);

        for ( ; loopCount > 0; --loopCount)
        {
            // Print a . every few seconds to tell the user we're waiting for the camera to be attached
            if ( loopCount % 4 == 0)
            {
                cout << ".";
                cout.flush();
            }

            // Try to find the camera we are looking for.
            DeviceInfoList_t devices;
            if ( tlFactory.EnumerateDevices(devices, filter) > 0 )
            {
                // Print two new lines, just for improving printed output.
                cout << endl << endl;

                // The camera has been found. Create and attach it to the Instant Camera object.
                camera.Attach( tlFactory.CreateDevice( devices[0]));
                //Exit waiting
                break;
            }

            WaitObject::Sleep(250);
        }

        // If the camera has been found.
        if ( camera.IsPylonDeviceAttached())
        {
            // Print the camera information.
            cout << endl;
            cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
            cout << "Friendly Name: " << camera.GetDeviceInfo().GetFriendlyName() << endl;
            cout << "Full Name    : " << camera.GetDeviceInfo().GetFullName() << endl;
            cout << "SerialNumber : " << camera.GetDeviceInfo().GetSerialNumber() << endl;
            cout << endl;

            // All configuration objects and other event handler objects are still registered.
            // The configuration objects will parameterize the camera device and the instant
            // camera will be ready for operation again.

            // Open the camera.
            camera.Open();

            // Now the Instant Camera object can be used as before.
        }
        else // Timeout
        {
            cout << endl << "Timeout expired." << endl;
        }
    }
    catch (GenICam::GenericException &e)
    {
        // Error handling.
        cerr << "An exception occurred." << endl
        << e.GetDescription() << endl;
        exitCode = 1;
    }

    // Comment the following two lines to disable waiting on exit.
    cerr << endl << "Press Enter to exit." << endl;
    while( cin.get() != '\n');

    return exitCode;
}
Beispiel #5
0
int main(int argc, char* argv[])
{

    int exitCode = 0;
    Pylon::PylonAutoInitTerm autoInitTerm;
	
	CDeviceInfo info;
	info.SetDeviceClass(Camera_t::DeviceClass());
	CTlFactory& tlFactory = CTlFactory::GetInstance();
	DeviceInfoList_t devices;
	
	if (tlFactory.EnumerateDevices(devices) == 0)
	{
		throw RUNTIME_EXCEPTION("No camera present.");
	}

	int cam_num = devices.size();

	cameras = new CBaslerUsbInstantCameraArray(min(devices.size(), c_maxCamerasToUse));
	// Create and attach all Pylon Devices.
	for (size_t i = 0; i < cameras->GetSize(); ++i)
	{
		cameras->operator[](i).Attach(tlFactory.CreateDevice(devices[i]));
		cameras->operator[](i).RegisterConfiguration(new CSoftwareTriggerConfiguration, RegistrationMode_Append, Cleanup_Delete);
		cameras->operator[](i).RegisterImageEventHandler(new CSampleImageEventHandler, RegistrationMode_Append, Cleanup_Delete);
		cameras->operator[](i).Open();


		

		_ImageBuffers[i] = new MyBufferFactory();
		cameras->operator[](i).SetBufferFactory(_ImageBuffers[i], Cleanup_None);

		CDeviceInfo & diRef = devices[i];

		if (diRef.GetModelName().find(GenICam::gcstring("acA2500-14uc")) != GenICam::gcstring::_npos())
		{
			_IsCameraBW[i] = false;
			cameras->operator[](i).PixelFormat.SetValue(PixelFormat_BayerGB12);
		}
		else if (diRef.GetModelName().find(GenICam::gcstring("acA2500-14um")) != GenICam::gcstring::_npos())
		{
			_IsCameraBW[i] = true;
			cameras->operator[](i).PixelFormat.SetValue(PixelFormat_Mono12);
		}

		//cameras->operator[](i).PixelFormat.SetValue(PixelFormat_Mono12);

		//cameras->operator[](i).Gain.SetValue(0);
	
		if (_IsCameraBW[i])
			cameras->operator[](i).ExposureTime.SetValue(Exposure);
		else 
			cameras->operator[](i).ExposureTime.SetValue(Exposure*ColorExposureMultiplier);
		cout << "Using device " << cameras->operator[](i).GetDeviceInfo().GetModelName() << endl;

		if (GenApi::IsWritable(cameras->operator[](i).ChunkModeActive))
		{
			cameras->operator[](i).ChunkModeActive.SetValue(true);
		}
		else
		{
			throw RUNTIME_EXCEPTION("The camera doesn't support chunk features");
		}

		cameras->operator[](i).ChunkSelector.SetValue(ChunkSelector_Timestamp);
		cameras->operator[](i).ChunkEnable.SetValue(true);
	}

    try
    {    
		char key;
	
		ProcessMessage(Action);
		do
		{
			cin.get(key);
			Action = ParseKey(key);
			ProcessMessage(Action);

		} while (Action != Quit);

    }
    catch (GenICam::GenericException &e)
    {
        // Error handling.
        cerr << "An exception occurred." << endl
        << e.GetDescription() << endl;
        exitCode = 1;
    }

    // Comment the following two lines to disable waiting on exit.
    cerr << endl << "Press Enter to exit." << endl;
    while( cin.get() != '\n');

    return exitCode;
}