Ejemplo n.º 1
0
//Get one image synchronously.
VmbErrorType Camera::AcquireSingleImage( FramePtr &rFrame, VmbUint32_t nTimeout )
{
    VmbErrorType    res;
    VmbInt64_t      PayloadSize;
    FeaturePtr      pFeature;

    res = GetFeatureValueInt( *this, "PayloadSize", PayloadSize );
    if ( VmbErrorSuccess == res )
    {
        AcquireImageHelper AcquireHelper( *this );
        res = AcquireHelper.Prepare( rFrame, PayloadSize );
        if ( VmbErrorSuccess == res )
        {
            res = (VmbErrorType)VmbCaptureFrameWait( GetHandle(), &(SP_ACCESS( rFrame )->m_pImpl->m_frame), nTimeout );
            if ( VmbErrorSuccess != res )
            {
                LOG_FREE_TEXT( "Could not acquire single image." )
            }
        }
Ejemplo n.º 2
0
VmbError_t SynchronousGrab( const char* pCameraID, const char* pFileName )
{
    VmbError_t          err                 = VmbStartup();     // Initialize the Vimba API
    VmbCameraInfo_t     *pCameras           = NULL;             // A list of camera details
    VmbUint32_t         nCount              = 0;                // Number of found cameras
    VmbUint32_t         nFoundCount         = 0;                // Change of found cameras
    const VmbUint32_t   nTimeout            = 2000;             // Timeout for Grab
    VmbAccessMode_t     cameraAccessMode    = VmbAccessModeFull;// We open the camera with full access
    VmbHandle_t         cameraHandle        = NULL;             // A handle to our camera
    VmbBool_t           bIsCommandDone      = VmbBoolFalse;     // Has a command finished execution
    VmbFrame_t          frame;                                  // The frame we capture
    const char*         pPixelFormat        = NULL;             // The pixel format we use for acquisition
    VmbInt64_t          nPayloadSize        = 0;                // The size of one frame
    AVTBitmap           bitmap;                                 // The bitmap we create
    
    PrintVimbaVersion();

    if ( VmbErrorSuccess == err )
    {
        // Is Vimba connected to a GigE transport layer?
        DiscoverGigECameras();
        
        // If no camera ID was provided use the first camera found
        if ( NULL == pCameraID )
        {
            // Get the amount of known cameras
            err = VmbCamerasList( NULL, 0, &nCount, sizeof *pCameras );
            if (    VmbErrorSuccess == err
                 && 0 < nCount )
            {
                pCameras = (VmbCameraInfo_t*)malloc( nCount * sizeof( *pCameras ));
                if ( NULL != pCameras )
                {
                    // Actually query all static details of all known cameras without having to open the cameras
                    // If a new camera was connected since we queried the amount of cameras (nFoundCount > nCount) we can ignore that one
                    err = VmbCamerasList( pCameras, nCount, &nFoundCount, sizeof *pCameras );
                    if (    VmbErrorSuccess != err
                         && VmbErrorMoreData != err )
                    {
                        printf( "Could not list cameras. Error code: %d\n", err );
                    }
                    else
                    {
                        // Use the first camera
                        if( nFoundCount != 0)
                        {
                            pCameraID = pCameras[0].cameraIdString;
                        }
                        else
                        {
                            pCameraID = NULL;
                            err = VmbErrorNotFound;
                            printf( "Camera lost.\n" );
                        }
                    }

                    free( pCameras );
                    pCameras = NULL;
                }
                else
                {
                    printf( "Could not allocate camera list.\n" );
                }
            }
            else
            {
                printf( "Could not list cameras or no cameras present. Error code: %d\n", err );
            }
        }

        if ( NULL != pCameraID )
        {
            // Open camera
            err = VmbCameraOpen( pCameraID, cameraAccessMode, &cameraHandle );
            if ( VmbErrorSuccess == err )
            {
                printf( "Camera ID: %s\n\n", pCameraID );

                // Set the GeV packet size to the highest possible value
                // (In this example we do not test whether this cam actually is a GigE cam)
                if ( VmbErrorSuccess == VmbFeatureCommandRun( cameraHandle, "GVSPAdjustPacketSize" ))
                {
                    do
                    {
                        if ( VmbErrorSuccess != VmbFeatureCommandIsDone(    cameraHandle,
                                                                            "GVSPAdjustPacketSize",
                                                                            &bIsCommandDone ))
                        {
                            break;
                        }
                    } while ( VmbBoolFalse == bIsCommandDone );
                }

                if ( VmbErrorSuccess == err )
                {
                    // Set pixel format. For the sake of simplicity we only support Mono and RGB in this example.
                    err = VmbFeatureEnumSet( cameraHandle, "PixelFormat", "RGB8Packed" );
                    if ( VmbErrorSuccess != err )
                    {
                        // Fall back to Mono
                        err = VmbFeatureEnumSet( cameraHandle, "PixelFormat", "Mono8" );
                    }
                    // Read back pixel format
                    VmbFeatureEnumGet( cameraHandle, "PixelFormat", &pPixelFormat );

                    if ( VmbErrorSuccess == err )
                    {
                        // Evaluate frame size
                        err = VmbFeatureIntGet( cameraHandle, "PayloadSize", &nPayloadSize );
                        if ( VmbErrorSuccess == err )
                        {
                            frame.buffer        = (unsigned char*)malloc( (VmbUint32_t)nPayloadSize );
                            frame.bufferSize    = (VmbUint32_t)nPayloadSize;

                            // Announce Frame
                            err = VmbFrameAnnounce( cameraHandle, &frame, (VmbUint32_t)sizeof( VmbFrame_t ));
                            if ( VmbErrorSuccess == err )
                            {
                                // Start Capture Engine
                                err = VmbCaptureStart( cameraHandle );
                                if ( VmbErrorSuccess == err )
                                {
                                    // Queue Frame
                                    err = VmbCaptureFrameQueue( cameraHandle, &frame, NULL );
                                    if ( VmbErrorSuccess == err )
                                    {
                                        // Start Acquisition
                                        err = VmbFeatureCommandRun( cameraHandle,"AcquisitionStart" );
                                        if ( VmbErrorSuccess == err )
                                        {
                                            // Capture one frame synchronously
                                            err = VmbCaptureFrameWait( cameraHandle, &frame, nTimeout );
                                            if ( VmbErrorSuccess == err )
                                            {
                                                // Convert the captured frame to a bitmap and save to disk
                                                if ( VmbFrameStatusComplete == frame.receiveStatus )
                                                {
                                                    bitmap.bufferSize = frame.imageSize;
                                                    bitmap.width = frame.width;
                                                    bitmap.height = frame.height;
                                                    // We only support Mono and RGB in this example
                                                    if ( 0 == strcmp( "RGB8Packed", pPixelFormat ))
                                                    {
                                                        bitmap.colorCode = ColorCodeRGB24;
                                                    }
                                                    else
                                                    {
                                                        bitmap.colorCode = ColorCodeMono8;
                                                    }

                                                    // Create the bitmap
                                                    if ( 0 == AVTCreateBitmap( &bitmap, frame.buffer ))
                                                    {
                                                        printf( "Could not create bitmap.\n" );
                                                    }
                                                    else
                                                    {
                                                        // Save the bitmap
                                                        if ( 0 == AVTWriteBitmapToFile( &bitmap, pFileName ))
                                                        {
                                                            printf( "Could not write bitmap to file.\n" );
                                                        }
                                                        else
                                                        {
                                                            printf( "Bitmap successfully written to file \"%s\"\n", pFileName );
                                                            // Release the bitmap's buffer
                                                            if ( 0 == AVTReleaseBitmap( &bitmap ))
                                                            {
                                                                printf( "Could not release the bitmap.\n" );
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    printf( "Frame not successfully received. Error code: %d\n", frame.receiveStatus );
                                                }
                                            }
                                            else
                                            {
                                                printf( "Could not capture frame. Error code: %d", err );
                                            }

                                            // Stop Acquisition
                                            err = VmbFeatureCommandRun( cameraHandle,"AcquisitionStop" );
                                            if ( VmbErrorSuccess != err )
                                            {
                                                printf( "Could not stop acquisition. Error code: %d\n", err );
                                            }
                                        }
                                        else
                                        {
                                            printf( "Could not start acquisition. Error code: %d", err );
                                        }
                                    }
                                    else
                                    {
                                        printf( "Could not queue frame. Error code: %d\n", err );
                                    }

                                    // Stop Capture Engine
                                    err = VmbCaptureEnd( cameraHandle );
                                    if ( VmbErrorSuccess != err )
                                    {
                                        printf( "Could not end capture. Error code: %d\n", err );
                                    }
                                }
                                else
                                {
                                    printf( "Could not start capture. Error code: %d\n", err );
                                }

                                // Revoke frame
                                err = VmbFrameRevoke( cameraHandle, &frame );
                                if ( VmbErrorSuccess != err )
                                {
                                    printf( "Could not revoke frame. Error code: %d\n", err );
                                }
                            }
                            else
                            {
                                printf( "Could not announce frame. Error code: %d\n", err );
                            }

                            free( frame.buffer );
                            frame.buffer = NULL;
                        }
                    }
                    else
                    {
                        printf( "Could not set pixel format to either RGB or Mono. Error code: %d\n", err );
                    }
                }
                else
                {
                    printf( "Could not adjust packet size. Error code: %d\n", err );
                }

                err = VmbCameraClose ( cameraHandle );
                if ( VmbErrorSuccess != err )
                {
                    printf( "Could not close camera. Error code: %d\n", err );
                }
            }
            else
            {
                printf( "Could not open camera. Error code: %d\n", err );
            }
        }
        VmbShutdown();
    }
    else
    {
        printf( "Could not start system. Error code: %d\n", err );
    }

    return err;
}