move toPylon C++ Programmer's GuideBetter Viewing
Getting Started
The pylon Programming Guide is a quick guide on how to program using the Basler pylon C++ API. It can be used in conjunction with thepylon sample codetogether to help beginners get started. Also.API renfenceA description of the Basler pylon C++ interface is provided. The description of the interface can also be found in the pylon header file. When using Microsoft Visual Studio, right-click on the desired method or class and select "Go to Declaration" from the context menu to access the relevant documentation.
For information on programming the Basler blaze camera, please refer to pylon's supplemental package for the blaze (available on the Basler website). This includes a C++ API and a .NET API, as well as sample code.
Common Settings for Building Applications with pylon (Linux)
This section shows the most common Linux build setups for building applications using pylon and the GNU toolchain. For more information, check out theAdvanced TopicsPart.
In order to centralize all the parameters that are responsible for building pylon based applications, we created thepylon-config
Tool. It works in a similar way to thepkg-config
You can call thepylon-config --help
to get the list of supported parameters.
In a typical GNU Make-based project, you can add the following line to the Makefile:
PYLON_ROOT ?= /opt/pylon
CPPFLAGS += $(shell $(PYLON_ROOT)/bin/pylon-config --cflags)
LDFLAGS += $(shell $(PYLON_ROOT)/bin/pylon-config --libs-rpath)
LDLIBS += $(shell $(PYLON_ROOT)/bin/pylon-config --libs)
If necessary, you can now use the environment variable<PYLON_ROOT>
Override the default installation path. Example:
PYLON_ROOT=/path/to/your/pylon/install make
Initialization/Uninitialization of the pylon Runtime Library
The pylon runtime system must be initialized before using the pylon API. A pylon based application must call thePylonInitialize()
. Before the application exits, it must call thePylonTerminate()
method to free up resources allocated by the pylon runtime system.
Pylon::PylonAutoInitTerm
The Convenience class helps to perform the above operations.Pylon::PylonAutoInitTerm
constructor call of thePylonInitialize()
The destructor callPylonTerminate()
. This ensures that thePylon::PylonAutoInitTerm
The pylon runtime system is initialized during the lifetime of the type object.
Examples are shown below:
#include <pylon/>
using namespace Pylon;
int main(int argc, char* argv[])
{
Pylon::PylonAutoInitTerm autoInitTerm; // PylonInitialize() will be called now
// Use pylon
// ..
} // autoInitTerm's destructor calls PylonTerminate() now
Advanced TopicsContains additional information for MFC users
Error Handling
In the event of an error, methods in the pylon class may throw C++ exceptions. pylon C++ API throws exceptions of typeGenericExceptionor its subclasses. You should protect pylon calls with exception handlers that catch GenericException. Example:
try
{
(640);
}
catch (const GenericException & e)
{
cerr << "Failed to set AOI width. Cause:"
<< () << endl;
}
Creating a pylon Device
In pylon, the physical camera device is defined by thepylon DevicesRepresentation. The following example shows how to create a pylon device:
CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
Create the first camera device found, e.g. for a vision system that uses only one camera.Advanced TopicsShows how to handle multiple camera devices and how to find a specific camera device.
The Instant Camera Classes
Instant camera (Instant Camera
) class makes it possible to grab images with just a few lines of code, minimizing programming effort. The Instant Camera class internally uses a pylon device (ptlon Device
). The pylon device needs to be created and attached to the instant camera object to operate.
Example.
// Create an instant camera object with the camera device found first.
CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice() );
// Print the model name of the camera.
cout << "Using device " << ().GetModelName() << endl;
// Start the grabbing of c_countOfImagesToGrab images.
// The camera device is parameterized with a default configuration which
// sets up free-running continuous acquisition.
( c_countOfImagesToGrab );
// This smart pointer will receive the grab result data.
CGrabResultPtr ptrGrabResult;
// () is called automatically by the RetrieveResult() method
// when c_countOfImagesToGrab images have been retrieved.
while (())
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
( 5000, ptrGrabResult, TimeoutHandling_ThrowException );
// Image grabbed successfully?
if (ptrGrabResult->GrabSucceeded())
{
// Access the image data.
cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
const uint8_t* pImageBuffer = (uint8_t*) ptrGrabResult->GetBuffer();
cout << "Gray value of first pixel: " << (uint32_t) pImageBuffer[0] << endl << endl;
}
else
{
cout << "Error: " << std::hex << ptrGrabResult->GetErrorCode() << std::dec << " " << ptrGrabResult->GetErrorDescription() << endl;
}
}
The above code can be found in the sample code in theGrab SampleFound in.
The Main Features of an Instant Camera Class
The Instant Camera class provides easy access to camera devices while being highly customizable. The following list shows the main features of the Instant Camera class:
- It is used as a single access point to access camera functions.
- It works "out of the box" without any parameters to set. The camera uses the default configuration of the device. The default configuration can be overridden.
- It manages the life cycle of pylon devices.
- It automatically turns pylon devices on and off.
- It handles the creation, reuse and destruction of buffers.
- It provides a crawl loop thread if needed.
- It detects the removal of camera equipment.
- It supports advanced camera features such as block mode and event reporting (camera events).
- It can be extended by derivation.
- This can be extended by registering additional event handler objects.
Types of Instant Camera Classes
Before you start programming, you need to determine which instant camera class to use. The following table shows the available instant camera classes:
Name of Class | Usable for Device Type | Device-specific |
---|---|---|
Pylon::CInstantCamera (recommended) | All cameras | No |
Pylon::CBaslerUniversalInstantCamera (recommended for newbies) | All cameras | No |
CInstantCamera
cap (a poem)CBaslerUniversalInstantCamera
Allows you to operate all types of camera equipment.
CBaslerUniversalInstantCamera
class isCInstantCamera
A specialization of the class which extends it by a parameter class. The parameter class provides a member for each camera parameter. The additional parameter class provides IDE auto-completion (e.g., IntelliSense in Visual Studio), which is very helpful when developing applications. Although this adds a little runtime overhead, this overhead is so small that it is negligible.
For further information, please go toPylon C++ Programmer's Guide