The ContinuousCapture program is a simple example for a continuous acquisition.
- Program location
- The source file ContinuousCapture.java can be found under:
%INSTALLDIR%\apps\Java\ContinuousCapture\
- Note
- If you have installed the package without example applications, this file will not be available. On Windows® the sample application can be installed or removed from the target system at any time by simply restarting the installation package.
- ContinuousCapture example:
- Opens a Balluff device.
- Snaps images continuously (without display using Linux).
- Console Output
[0]: BF000306 (mvBlueFOX-202C, Family: mvBlueFOX, interface layout: DeviceSpecific)
Please enter the number in front of the listed device followed by [ENTER] to open it: 0
Using device number 0.
Press [ENTER] to end the application
Initialising the device. This might take some time...
Info from BF000306: FramesPerSecond: 28.655660, ErrorCount: 0, CaptureTime_s: 0.104195
Info from BF000306: FramesPerSecond: 28.655636, ErrorCount: 0, CaptureTime_s: 0.104017
Info from BF000306: FramesPerSecond: 28.655659, ErrorCount: 0, CaptureTime_s: 0.104153
Info from BF000306: FramesPerSecond: 28.655636, ErrorCount: 0, CaptureTime_s: 0.104072
Info from BF000306: FramesPerSecond: 28.655660, ErrorCount: 0, CaptureTime_s: 0.104234
- How it works
The continuous acquisition starts a separate thread (CaptureThread.java) that continuously requests images from the device.
First of all the user is prompted to select the device he wants to use for this sample:
DeviceManager devMgr = new DeviceManager();
Device pDev = mvIMPACT.acquire.examples.helper.DeviceAccess.getDeviceFromUserInput( devMgr );
Then after the device has been initialized successfully a continuous acquisition is started in CaptureThread.java .
Accessing the pRequest object can be done after it has been checked using the mvIMPACT.acquire.Request.isOK() function. The image attached to the request can then be processed and/or displayed if the request does not report an error.
requestNr = fi.imageRequestWaitFor( timeout_ms );
pRequest = fi.isRequestNrValid( requestNr ) ? fi.getRequest( requestNr ) : null;
if ( pRequest != null )
{
if ( pRequest.isOK()
{
...
}
}
else
{
System.out.println( "Error: " + pRequest.getRequestResult().readS() );
}
- Source code
import mvIMPACT.acquire.*;
public class CaptureThread extends Thread
{
private static final boolean isWindows_ = System.getProperty( "os.name" ).startsWith( "Windows" );
private Device pDev_;
private boolean terminated_ = false;
public CaptureThread( Device pDev )
{
pDev_ = pDev;
}
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" );
}
Statistics statistics = new Statistics( pDev_ );
FunctionInterface fi = new FunctionInterface( pDev_ );
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 );
Request pRequest = null;
Request pPreviousRequest = null;
int timeout_ms = 500;
int cnt = 0;
int requestNr = acquire.getINVALID_ID();
while( !terminated_ )
{
requestNr = fi.imageRequestWaitFor( timeout_ms );
pRequest = fi.isRequestNrValid( requestNr ) ? fi.getRequest( requestNr ) : null;
if( pRequest != null )
{
if( pRequest.isOK() )
{
++cnt;
if( cnt % 100 == 0 )
{
System.out.println( String.format( "Info from %s: %s: %s, %s: %s, %s: %s", pDev_.getSerial().read(),
statistics.getFramesPerSecond().name(), statistics.getFramesPerSecond().readS(),
statistics.getErrorCount().name(), statistics.getErrorCount().readS(),
statistics.getCaptureTime_s().name(), statistics.getCaptureTime_s().readS() ) );
}
}
else
{
System.out.println( "Error: " + pRequest.getRequestResult().readS() );
}
if( pPreviousRequest != null )
{
pPreviousRequest.unlock();
}
pPreviousRequest = pRequest;
fi.imageRequestSingle();
}
}
mvIMPACT.acquire.examples.helper.DeviceAccess.manuallyStopAcquisitionIfNeeded( pDev_, fi );
if( pRequest != null )
{
pRequest.unlock();
}
fi.imageRequestReset( 0, 0 );
}
public void terminate()
{
terminated_ = true;
}
}
import mvIMPACT.acquire.*;
public class ContinuousCapture
{
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 )
{
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() );
}
}
}