IDS peak AFL v1.6.1
Loading...
Searching...
No Matches
C#

Prerequisites

For Windows, there also exists a .NET version of the library.

To use it in a C# project, add a reference to the ids_peak_afl_dotnet.dll to the project.

Before starting your application, make sure that the DLL-Loader can find all required libraries. This means that ids_peak_afl_dotnet.dll, ids_peak_afl_dotnet_interface.dll, ids_peak_afl.dll and all other libraries required by ids_peak_afl.dll are located next to your application binary or that the directory where they are located is added to the DLL-Loader's search directories.

The recommended way is to add a post-build step to the .csproj file, which copies the DLLs into the application's binary directory. An example of how to achieve this can be seen for the IDS peak genericAPI and IDS peak IPL in the provided C# samples.

Alternatively, you could add their directories to the DLL-Loader's search directories by appending each directory to the environment variable Path:

  • PowerShell
    $env:Path="$env:Path;<Installation-Directory-Of-ids_peak_afl.dll>"
    $env:Path="$env:Path;<Installation-Directory-Of-ids_peak_afl_dotnet.dll>"
    # Then start your application
  • CMD
    set Path=%Path%;<Installation-Directory-Of-ids_peak_afl.dll>
    set Path=%Path%;<Installation-Directory-Of-ids_peak_afl_dotnet.dll>
    REM Then start your application

Usage

Using the .NET binding is very similar to how you would use the C++ API of the IDS peak AFL.

There are two main concepts:

  1. Manager
  2. Controller

A Manager can apply multiple auto features based on an image, which is achieved by registering one or more Controllers on the manager.

// Auto feature manager, which can have multiple controllers
var manager = new peak.afl.Manager(remoteNodemap);
// Create autofocus controller on the manager directly
var focusController = manager.CreateController(
peak_afl_controllerType.PEAK_AFL_CONTROLLER_TYPE_AUTOFOCUS)
var brightnessController = manager.CreateController(
peak_afl_controllerType.PEAK_AFL_CONTROLLER_TYPE_BRIGHTNESS)
peak_afl_controllerType
controller types
Definition peak_afl.h:125

The auto features of each individual controller get applied by passing an image to the Manager's Process function.

// Process the image in the autofeature manager, which will
// apply all the actions of associated controllers.
manager.Process(image)
Warning
Some controllers are dependent on the time stamp information that is available on the IDS peak IPL Image. If you're creating such an image yourself, instead of e.g., using the provided BufferToImage function, you have to take care to pass the time stamp to the image's constructor.

Callbacks

Registering Callbacks in the .NET binding works differently from the way of the C++ API. Instead of calling e.g., RegisterFinishedCallback directly, there are base classes available that need to be inherited from, these include e.g.:

  • FinishedCallback
  • ProcessDataCallback
  • ComponentExposureFinishedCallback
  • ComponentGainFinishedCallback

To create a custom callback the callback method needs to be overridden. Attaching that callback to a Controller is done by passing the Controller instance to the constructor of the Callback class.

An example usage could look like as follows:

using peak.afl;
// NOTE: Not available for all controller types e.g.,
// not for `PEAK_AFL_CONTROLLER_TYPE_BRIGHTNESS`
class ProcessDataCallback : peak.afl.ProcessDataCallback
{
public ProcessDataCallback(Controller controller) : base(controller) {}
public override void callback(int focusValue, int sharpnessValue)
{
Console.WriteLine($"ProcessDataCallback: Focus: {focusValue} Sharpness: {sharpnessValue}");
}
}
class FinishedCallback : peak.afl.FinishedCallback
{
public FinishedCallback(Controller controller) : base(controller) {}
public override void callback()
{
Console.WriteLine("ControllerFinishedCallback!");
}
}
class App
{
static void Main(string[] args)
{
// ... Library and camera setup omitted ...
// Autofeature manager, which can have multiple controllers
var manager = new peak.afl.Manager(remoteNodemap);
// Create autofocus controller
var controller = manager.CreateController(
peak_afl_controllerType.PEAK_AFL_CONTROLLER_TYPE_AUTOFOCUS);
// Only focus once
controller.SetMode(
peak_afl_controller_automode.PEAK_AFL_CONTROLLER_AUTOMODE_ONCE);
// Register callbacks
// NOTE: these have to be assigned, otherwise they get destructed
// and the callback gets removed
var processCallback = new ProcessDataCallback(controller);
var focusFinishedCallback = new FinishedCallback(controller);
for (var i = 0; i < 100; i++)
{
try
{
// Wait for finished/filled buffer event
var buffer = dataStream.WaitForFinishedBuffer(1000);
// NOTE: The resulting `image` still uses the underlying
// memory of `buffer`, so we can't queue the buffer
// until we're done processing the image.
var img = ids_peak_ipl_extension.BufferToImage(buffer);
// Process the image in the autofeature manager, which will
// Apply all the actions of associated controllers
manager.Process(img);
img.Dispose();
// Put the buffer back in the pool, so it can be filled again
dataStream.QueueBuffer(buffer);
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
// ... Cleanup etc. omitted ...
}
}
Definition peak_afl.hpp:489
peak_afl_controller_automode
controller automode
Definition peak_afl.h:144

Output:

ProcessDataCallback: Focus: 632 Sharpness: 33214
ProcessDataCallback: Focus: 390 Sharpness: 32619
ProcessDataCallback: Focus: 781 Sharpness: 32978
ProcessDataCallback: Focus: 539 Sharpness: 33527
ProcessDataCallback: Focus: 482 Sharpness: 34531
ProcessDataCallback: Focus: 446 Sharpness: 33545
ProcessDataCallback: Focus: 503 Sharpness: 33354
ProcessDataCallback: Focus: 467 Sharpness: 33902
ProcessDataCallback: Focus: 489 Sharpness: 32037
ProcessDataCallback: Focus: 475 Sharpness: 32500
ProcessDataCallback: Focus: 483 Sharpness: 34209
ControllerFinishedCallback!

Component callbacks

For Controllers of type e.g, brightness, there exist additional callbacks that can be registered for the individual components of the controller:

using peak.afl;
// NOTE: Not available for all controller types e.g.,
// not for `PEAK_AFL_CONTROLLER_TYPE_AUTOFOCUS`
class ComponentExposureFinishedCallback : peak.afl.ComponentExposureFinishedCallback
{
public ComponentExposureFinishedCallback(Controller controller) : base(controller)
{
}
public override void callback()
{
Console.WriteLine("ControllerComponentExposureFinishedCallback!");
}
}
class ComponentGainFinishedCallback : peak.afl.ComponentGainFinishedCallback
{
public ComponentGainFinishedCallback(Controller controller) : base(controller)
{
}
public override void callback()
{
Console.WriteLine("ControllerComponentGainFinishedCallback!");
}
}
class FinishedCallback : peak.afl.FinishedCallback
{
public FinishedCallback(Controller controller) : base(controller) {}
public override void callback()
{
Console.WriteLine("ControllerFinishedCallback!");
}
}
class App
{
static void Main(string[] args)
{
// ... Library and camera setup omitted ...
// Autofeature manager, which can have multiple controllers
var manager = new peak.afl.Manager(remoteNodemap);
// Create autofocus controller
var controller = manager.CreateController(
peak_afl_controllerType.PEAK_AFL_CONTROLLER_TYPE_BRIGHTNESS);
controller.BrightnessComponentSetMode(
peak_afl_controller_brightness_component.PEAK_AFL_CONTROLLER_BRIGHTNESS_COMPONENT_EXPOSURE,
peak_afl_controller_automode.PEAK_AFL_CONTROLLER_AUTOMODE_ONCE
);
controller.BrightnessComponentSetMode(
peak_afl_controller_brightness_component.PEAK_AFL_CONTROLLER_BRIGHTNESS_COMPONENT_GAIN,
peak_afl_controller_automode.PEAK_AFL_CONTROLLER_AUTOMODE_ONCE
);
// Register callbacks
// NOTE: these have to be assigned, otherwise they get destructed
// and the callback gets removed
var brightnessFinishedCallback = new FinishedCallback(controller);
var exposureFinishedCallback = new ComponentExposureFinishedCallback(controller);
var gainFinishedCallback = new ComponentGainFinishedCallback(controller);
for (var i = 0; i < 100; i++)
{
try
{
// Wait for finished/filled buffer event
var buffer = dataStream.WaitForFinishedBuffer(1000);
// NOTE: The resulting `image` still uses the underlying
// memory of `buffer`, so we can't queue the buffer
// until we're done processing the image.
var img = ids_peak_ipl_extension.BufferToImage(buffer);
// Process the image in the autofeature manager, which will
// Apply all the actions of associated controllers
manager.Process(img);
img.Dispose();
// Put the buffer back in the pool, so it can be filled again
dataStream.QueueBuffer(buffer);
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
// ... Cleanup etc. omitted ...
}
}
peak_afl_controller_brightness_component
controller brightness component
Definition peak_afl.h:245

Output:

ControllerComponentExposureFinishedCallback!
ControllerComponentGainFinishedCallback!
ControllerFinishedCallback!
Note
The ProcessDataCallback is not supported for some controller types, including the brightness controller used in the example above!