Impact Acquire SDK Java
GenICamCallbackOnEvent.java

The GenICamCallbackOnEvent program is a simple example which illustrates how GenICam™ events can be used to inform an application about a certain event via a callback.

How it works:
  1. Open the device by calling
    pDev.open();
  2. Enable GenICam™ events.
  3. Attach a custom callback to the ExposureEndTimestamp property that gets called whenever a property is modified.
  4. Start the image acquisition in order to cause the callbacks to get triggered.

The full explanation regarding the callback usage can be found at the chapter Callbacks Triggered By GenICam™ Events .

CaptureThread.java
import mvIMPACT.acquire.*;
public class CaptureThread extends Thread
{
//=============================================================================
//=== private static member variables =========================================
//=============================================================================
private static final boolean isWindows_ = System.getProperty( "os.name" ).startsWith( "Windows" );
//=============================================================================
//=== private member variables ================================================
//=============================================================================
private Device pDev_;
//private ImageDisplayWindow window_; // Windows only but there is no such thing as #ifdef in Java!
private boolean terminated_ = false;
//=============================================================================
//=== public constructor ======================================================
//=============================================================================
//-----------------------------------------------------------------------------
public CaptureThread( Device pDev )
//-----------------------------------------------------------------------------
{
pDev_ = pDev;
//window_ = new ImageDisplayWindow( "mvIMPACT_acquire sample, Device " + pDev.getSerial().read() );
}
//=============================================================================
//=== public member functions =================================================
//=============================================================================
//-----------------------------------------------------------------------------
public void run()
//-----------------------------------------------------------------------------
{
if( isWindows_ )
{
System.out.println( "\n\nSince you are running on a Windows platform you could use the display module belonging to this SDK. To try this simply remove the comments wherever the 'window_' variable is used. As Java does not support something like '#ifdef' we did not come up with anything smarter. Suggestions welcome!!!\n\n" );
}
EventControl ec = new EventControl( pDev_ );
ec.getEventSelector().writeS( "ExposureEnd" );
ec.getEventNotification().writeS( "On" );
// create an interface to the device found
FunctionInterface fi = new FunctionInterface( pDev_ );
// Send all requests to the capture queue. There can be more than 1 queue for some devices, but for this sample
// we will work with the default capture queue. If a device supports more than one capture or result
// queue, this will be stated in the manual. If nothing is mentioned about it, the device supports one
// queue only. This loop will send all requests currently available to the driver. To modify the number of requests
// use the property mvIMPACT.acquire.SystemSettings.requestCount at runtime or the property
// mvIMPACT.acquire.Device.defaultRequestCount BEFORE opening the device.
int result = TDMR_ERROR.DMR_NO_ERROR;
while ( ( result = fi.imageRequestSingle() ) == TDMR_ERROR.DMR_NO_ERROR ) { };
if( result != TDMR_ERROR.DEV_NO_FREE_REQUEST_AVAILABLE )
{
System.out.println( String.format( "'FunctionInterface.imageRequestSingle' returned with an unexpected result: %d(%s)", result, ImpactAcquireException.getErrorCodeAsString( result ) ) );
}
mvIMPACT.acquire.examples.helper.DeviceAccess.manuallyStartAcquisitionIfNeeded( pDev_, fi );
// run thread loop
Request pRequest = null;
// we always have to keep at least 2 images as the display module might want to repaint the image, thus we
// cannot free it unless we have a assigned the display to a new buffer.
Request pPreviousRequest = null;
int timeout_ms = 500;
int cnt = 0;
int requestNr = acquire.getINVALID_ID();
EventCallback eventCallback = new EventCallback( "", ec );
// register a callback to eventExposureEndTimestamp
eventCallback.registerComponent( ec.getEventExposureEndTimestamp() );
while ( !terminated_ )
{
// wait for results from the default capture queue
requestNr = fi.imageRequestWaitFor( timeout_ms );
pRequest = fi.isRequestNrValid( requestNr ) ? fi.getRequest( requestNr ) : null;
if( pRequest != null )
{
if( pRequest.isOK() )
{
//window_.GetImageDisplay().SetImage( pRequest );
//window_.GetImageDisplay().Update();
}
else
{
System.out.println( "Error: " + pRequest.getRequestResult().readS() );
}
if( pPreviousRequest != null )
{
// this image has been displayed thus the buffer is no longer needed...
pPreviousRequest.unlock();
}
pPreviousRequest = pRequest;
// send a new image request into the capture queue
fi.imageRequestSingle();
}
//else
//{
// Please note that slow systems or interface technologies in combination with high resolution sensors
// might need more time to transmit an image than the timeout value which has been passed to imageRequestWaitFor().
// If this is the case simply wait multiple times OR increase the timeout(not recommended as usually not necessary
// and potentially makes the capture thread less responsive) and rebuild this application.
// Once the device is configured for triggered image acquisition and the timeout elapsed before
// the device has been triggered this might happen as well.
// The return code would be -2119(DEV_WAIT_FOR_REQUEST_FAILED) in that case, the documentation will provide
// additional information under TDMR_ERROR in the interface reference.
// If waiting with an infinite timeout(-1) it will be necessary to call 'imageRequestReset' from another thread
// to force 'imageRequestWaitFor' to return when no data is coming from the device/can be captured.
// System.out.println("imageRequestWaitFor failed (%d, %s), timeout value too small?", requestNr, ImpactAcquireException.getErrorCodeAsString(requestNr));
//}
}
mvIMPACT.acquire.examples.helper.DeviceAccess.manuallyStopAcquisitionIfNeeded( pDev_, fi );
eventCallback.unregisterComponent( ec.getEventExposureEndTimestamp() );
// stop the display from showing freed memory
//window_.GetImageDisplay().RemoveImage();
// In this sample all the next lines are redundant as the device driver will be
// closed now, but in a real world application a thread like this might be started
// several times an then it becomes crucial to clean up correctly.
// free the last potentially locked request
if( pRequest != null )
{
pRequest.unlock();
}
// clear all queues
fi.imageRequestReset( 0, 0 );
}
//-----------------------------------------------------------------------------
public void terminate()
//-----------------------------------------------------------------------------
{
terminated_ = true;
}
}
EventCallback.java
import mvIMPACT.acquire.*;
//-----------------------------------------------------------------------------
class EventCallback extends ComponentCallback
//-----------------------------------------------------------------------------
{
//=============================================================================
//=== Private member variables ================================================
//=============================================================================
private Object pUserData_;
private String name_;
//=============================================================================
//=== Public constructors =====================================================
//=============================================================================
public EventCallback( final String name, Object pUserData )
{
super();
pUserData_ = pUserData;
name_ = name;
}
//=============================================================================
//=== Public member functions =================================================
//=============================================================================
@Override
public void execute( Component c, SWIGTYPE_p_void pUserData )
{
// re-generating the object/data previously attached to the callback object. This could now be used to call a certain member function e.g. to update a class instance about this event!
EventControl ec = ( EventControl )getUserData();
if( c.isProp() )
{
Property p = new Property( c.hObj() );
System.out.println( "Component " + c.name() + " has changed. Its current value: " + p.readS() + "us. FrameID is: " + ec.getEventExposureEndFrameID().readS() );
}
}
public Object getUserData()
{
return pUserData_;
}
}
import mvIMPACT.acquire.*;
//-----------------------------------------------------------------------------
public class GenICamCallbackOnEvent
//-----------------------------------------------------------------------------
{
//=============================================================================
//=== Static member functions =================================================
//=============================================================================
static
{
try
{
System.loadLibrary( "mvIMPACT_Acquire.java" );
}
catch( UnsatisfiedLinkError e )
{
System.err.println( "Native code library failed to load. Make sure the 'mvIMPACT_Acquire.java' library can be found in the systems search path.\n" + e );
System.exit( 1 );
}
}
//-----------------------------------------------------------------------------
public static void main( String[] args )
//-----------------------------------------------------------------------------
{
DeviceManager devMgr = new DeviceManager();
Device pDev = mvIMPACT.acquire.examples.helper.DeviceAccess.getDeviceFromUserInput( devMgr );
if( pDev == null )
{
System.out.print( "Unable to continue! " );
mvIMPACT.acquire.examples.helper.DeviceAccess.waitForENTER();
System.exit( 1 );
}
System.out.println( "Initialising the device. This might take some time..." );
try
{
pDev.open();
}
catch( ImpactAcquireException e )
{
// this e.g. might happen if the same device is already opened in another process...
System.out.println( "An error occurred while opening device " + pDev.getSerial().read() +
"(error code: " + e.getMessage() + ")." );
mvIMPACT.acquire.examples.helper.DeviceAccess.waitForENTER();
System.exit( 1 );
}
CaptureThread captureThread = new CaptureThread( pDev );
captureThread.start();
mvIMPACT.acquire.examples.helper.DeviceAccess.waitForENTER();
captureThread.terminate();
try
{
captureThread.join();
}
catch( Exception e )
{
System.out.println( e.getMessage() );
}
}
}