mvIMPACT Acquire SDK C++
SingleCapture.cpp

The SingleCapture program is a simple example, which shows, how to snap a single image.

Program location
The source file SingleCapture.cpp can be found under:
%INSTALLDIR%\apps\SingleCapture\
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.
SingleCapture example:
  1. Opens a MATRIX VISION device.
  2. Snaps an image (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.
Image captured( RGBx888Packed 640x480 )
Press [ENTER] to end the application
How it works

The work behind an image acquisition is rather simple. If a program requests an image, the image data will be written in the image buffer. After this the image buffer can either be displayed or processed and then must be passed back to the driver.

But now step by step through the source:

First of all a device handle must be obtained. In this sample the user is prompted to select the device he wants to use:

DeviceManager devMgr;
Device* pDev = getDeviceFromUserInput( devMgr );

The function getDeviceFromUserInput() is included via

#include <apps/Common/exampleHelper.h>

Afterwards a function interface has to be created for the device:

FunctionInterface fi( pDev );

Then you have to send the request for an image to the driver request queue and afterwards wait for the result:

const int iMaxWaitTime_ms = -1; // USB 1.1 on an embedded system needs a large timeout for the first image.
// wait for results from the default capture queue.
int requestNr = fi.imageRequestWaitFor( iMaxWaitTime_ms );

With the request number you can gain access to the image buffer:

// check if the image has been captured without any problems.
if( !fi.isRequestNrValid( requestNr ) )
{
// If the error code is -2119(DEV_WAIT_FOR_REQUEST_FAILED), the documentation will provide
// additional information under TDMR_ERROR in the interface reference
cout << "imageRequestWaitFor failed (" << requestNr << ", " << ImpactAcquireException::getErrorCodeAsString( requestNr ) << ")"
<< ", timeout value too small?" << endl;
return 0;
}
const Request* pRequest = fi.getRequest( requestNr );
if( !pRequest->isOK() )
{
cout << "Error: " << pRequest->requestResult.readS() << endl;
// if the application wouldn't terminate at this point this buffer HAS TO be unlocked before
// it can be used again as currently it is under control of the user. However terminating the application
// will free the resources anyway thus the call
// fi.imageRequestUnlock( requestNr );
// can be omitted here.
return 0;
}

The image attached to the request can then be processed and/or displayed if the request does not report an error.

After the image handle you have to unlock the image buffer:

// unlock the buffer to let the driver know that you no longer need this buffer
fi.imageRequestUnlock( requestNr );
Source code
#ifdef _MSC_VER // is Microsoft compiler?
# if _MSC_VER < 1300 // is 'old' VC 6 compiler?
# pragma warning( disable : 4786 ) // 'identifier was truncated to '255' characters in the debug information'
# endif // #if _MSC_VER < 1300
#endif // #ifdef _MSC_VER
#include <iostream>
#include <apps/Common/exampleHelper.h>
#include <mvIMPACT_CPP/mvIMPACT_acquire.h>
#ifdef _WIN32
# define USE_DISPLAY
# include <mvDisplay/Include/mvIMPACT_acquire_display.h>
#endif // #ifdef _WIN32
using namespace mvIMPACT::acquire;
using namespace std;
//-----------------------------------------------------------------------------
int main( void )
//-----------------------------------------------------------------------------
{
DeviceManager devMgr;
Device* pDev = getDeviceFromUserInput( devMgr );
if( !pDev )
{
cout << "Unable to continue! Press [ENTER] to end the application" << endl;
cin.get();
return 1;
}
try
{
pDev->open();
}
catch( const ImpactAcquireException& e )
{
// this e.g. might happen if the same device is already opened in another process...
cout << "An error occurred while opening the device(error code: " << e.getErrorCode() << ")." << endl
<< "Press [ENTER] to end the application" << endl;
cin.get();
return 1;
}
FunctionInterface fi( pDev );
// send a request to the default request queue of the device and wait for the result.
manuallyStartAcquisitionIfNeeded( pDev, fi );
// Wait for results from the default capture queue by passing a timeout (The maximum time allowed
// for the application to wait for a Result). Infinity value: -1, positive value: The time to wait in milliseconds.
// 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.
// Once the device is configured for triggered image acquisition and the timeout elapsed before
// the device has been triggered this might happen as well.
// 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.
int requestNr = fi.imageRequestWaitFor( 10000 );
manuallyStopAcquisitionIfNeeded( pDev, fi );
// check if the image has been captured without any problems.
if( !fi.isRequestNrValid( requestNr ) )
{
// If the error code is -2119(DEV_WAIT_FOR_REQUEST_FAILED), the documentation will provide
// additional information under TDMR_ERROR in the interface reference
cout << "imageRequestWaitFor failed maybe the timeout value has been too small?" << endl;
return 1;
}
const Request* pRequest = fi.getRequest( requestNr );
if( !pRequest->isOK() )
{
cout << "Error: " << pRequest->requestResult.readS() << endl;
// if the application wouldn't terminate at this point this buffer HAS TO be unlocked before
// it can be used again as currently it is under control of the user. However terminating the application
// will free the resources anyway thus the call
// fi.imageRequestUnlock( requestNr );
// can be omitted here.
return 1;
}
#ifdef USE_DISPLAY
cout << "Please note that there will be just one refresh for the display window, so if it is" << endl
<< "hidden under another window the result will not be visible." << endl;
// everything went well. Display the result
ImageDisplayWindow display( "mvIMPACT_acquire sample" );
display.GetImageDisplay().SetImage( pRequest );
display.GetImageDisplay().Update();
#endif // USE_DISPLAY
cout << "Image captured(" << pRequest->imagePixelFormat.readS() << " " << pRequest->imageWidth.read() << "x" << pRequest->imageHeight.read() << ")" << endl;
// unlock the buffer to let the driver know that you no longer need this buffer.
fi.imageRequestUnlock( requestNr );
cout << "Press [ENTER] to end the application" << endl;
cin.get();
return 0;
}
Grants access to devices that can be operated by this software interface.
Definition: mvIMPACT_acquire.h:6964
This class and its functions represent an actual device detected by this interface in the current sys...
Definition: mvIMPACT_acquire.h:5948
void open(void)
Opens a device.
Definition: mvIMPACT_acquire.h:6238
ZYX read(int index=0) const
Reads a value from a property.
Definition: mvIMPACT_acquire.h:4170
The function interface to devices supported by this interface.
Definition: mvIMPACT_acquire.h:10443
int imageRequestWaitFor(int timeout_ms, int queueNr=0) const
Waits for a request object to become ready.
Definition: mvIMPACT_acquire.h:11335
int imageRequestUnlock(int nr) const
Unlocks the request for the driver again.
Definition: mvIMPACT_acquire.h:11303
int imageRequestSingle(ImageRequestControl *pImageRequestControl=0, int *pRequestUsed=0) const
Sends an image request to the mvIMPACT::acquire::Device driver.
Definition: mvIMPACT_acquire.h:11222
bool isRequestNrValid(int nr) const
Check if nr specifies a valid mvIMPACT::acquire::Request.
Definition: mvIMPACT_acquire.h:11470
Request * getRequest(int nr) const
Returns a pointer to the desired mvIMPACT::acquire::Request.
Definition: mvIMPACT_acquire.h:10908
A base class for exceptions generated by mvIMPACT Acquire.
Definition: mvIMPACT_acquire.h:248
int getErrorCode(void) const
Returns a unique numerical representation for this error.
Definition: mvIMPACT_acquire.h:267
std::string readS(int index=0, const std::string &format="") const
Reads data from this property as a string.
Definition: mvIMPACT_acquire.h:3213
Contains information about a captured buffer.
Definition: mvIMPACT_acquire.h:8408
PropertyI imageHeight
An integer property (read-only) containing the height of the image in pixels.
Definition: mvIMPACT_acquire.h:10020
bool isOK(void) const
Convenience function to check if a request has been processed successfully.
Definition: mvIMPACT_acquire.h:9197
PropertyIRequestResult requestResult
An enumerated integer property (read-only) defining the result of this request.
Definition: mvIMPACT_acquire.h:9500
PropertyI imageWidth
An integer property (read-only) containing the width of the image in pixels.
Definition: mvIMPACT_acquire.h:10009
PropertyIImageBufferPixelFormat imagePixelFormat
An enumerated integer property (read-only) containing the pixel format of this image.
Definition: mvIMPACT_acquire.h:9825
A class that can be used to display images in a window.
Definition: mvIMPACT_acquire_display.h:582
ImageDisplay & GetImageDisplay(void)
Returns a reference to the actual display object associated with this window.
Definition: mvIMPACT_acquire_display.h:609
void SetImage(const void *pData, int width, int height, int bitsPerPixel, int pitch)
Sets the next image to display.
Definition: mvIMPACT_acquire_display.h:292
void Update(void) const
Immediately redraws the current image.
Definition: mvIMPACT_acquire_display.h:381
This namespace contains classes and functions that can be used to display images.
This namespace contains classes and functions belonging to the image acquisition module of this SDK.
Definition: mvImageBuffer.h:42