Changing Capture Settings + SingleImage (GbE-2 / C++)
These are the steps to create a program that changes the resolution (image size), frame rate, and shutter speed parameters of a GbE-2, then captures and decodes one frame of compressed image data under the new settings, saving it in the PGM format (Portable Graymap).
-
Write the source code
Open the main file (e.g.
GbE2_SingleImage_Settings.cpp) and replace its contents with the following code.#include <stdio.h>#include <stdlib.h>#include <PUCLIB.h> // Load the PUCLIB headerint main() {printf("Starting INFINICAM GbE-2 capture settings change & SingleImage capture...\n");// 1. Initialize the SDKPUCRESULT ret = PUC_Initialize();if (ret != PUC_SUCCEEDED) {printf("Error: Failed to initialize the SDK. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 2. Search for a GbE-2 device on the networkPUC_DETECT_FILTER filter = { 0 };filter.nDeviceTypeFlags = PUC_DETECT_TARGET_GBE2; // Specify GbE-2PUC_DETECT_INFO detectInfo = { 0 };ret = PUC_DetectDevice2(&filter, &detectInfo);if (ret != PUC_SUCCEEDED || detectInfo.nDeviceCount == 0) {printf("Error: No GbE-2 camera was found. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 3. Open the first detected devicePUC_HANDLE hDevice = NULL;ret = PUC_OpenDevice2(0, &hDevice);if (ret != PUC_SUCCEEDED) {printf("Error: Failed to open the device. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 4. Change the resolution (width and height)UINT32 newWidth = 800;UINT32 newHeight = 320;ret = PUC_SetResolution(hDevice, newWidth, newHeight);if (ret != PUC_SUCCEEDED) {printf("Error: Failed to set the resolution. ErrorCode: %d\n", ret);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}printf("Changed the resolution: %u x %u pixels\n", newWidth, newHeight);// 5. Change the frame rate and shutter speedUINT32 newFramerate = 1000; // Frame rate: 1000 fpsUINT32 newShutterSpeed = 2000; // Shutter speed: 1/2000 secret = PUC_SetFramerateShutter(hDevice, newFramerate, newShutterSpeed);if (ret != PUC_SUCCEEDED) {printf("Error: Failed to set the frame rate/shutter speed. ErrorCode: %d\n", ret);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}printf("Changed the capture settings: %u fps, shutter 1/%u sec\n", newFramerate, newShutterSpeed);// 6. Retrieve the latest resolution, quantization table, and transfer data size after the setting changeUINT32 width = 0, height = 0;PUC_GetResolution(hDevice, &width, &height);unsigned short qVals[PUC_Q_COUNT] = { 0 };PUC_GetQuantization2(hDevice, qVals);UINT32 xferDataSize = 0;PUC_GetXferDataSize(hDevice, &xferDataSize);// 7. Allocate memory buffers// Buffer to store the compressed dataunsigned char* pCompressedBuffer = (unsigned char*)malloc(xferDataSize);// Buffer to store the decoded data (the row width is aligned to a 4-byte boundary)UINT32 lineBytes = (width + 3) & ~3;unsigned long decodedBufferSize = (unsigned long)lineBytes * height;unsigned char* pDecodedBuffer = (unsigned char*)malloc(decodedBufferSize);if (pCompressedBuffer == NULL || pDecodedBuffer == NULL) {printf("Error: Memory allocation failed.\n");free(pCompressedBuffer);free(pDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}// 8. Capture one frame of data under the new settingsPUC_XFER_DATA_INFO xferDataInfo = { 0 };xferDataInfo.pData = pCompressedBuffer;xferDataInfo.nDataSize = xferDataSize;printf("Capturing...\n");ret = PUC_GetSingleXferData(hDevice, &xferDataInfo);if (ret == PUC_SUCCEEDED) {// 9. Decode the compressed dataret = PUC_DecodeData(pDecodedBuffer, 0, 0, width, height, lineBytes, pCompressedBuffer, qVals);if (ret == PUC_SUCCEEDED) {// 10. Save the decoded image in PGM (P5) formatconst char* filename = "custom_setting_image.pgm";FILE* fp = NULL;if (fopen_s(&fp, filename, "wb") == 0 && fp != NULL) {// Write the PGM headerfprintf(fp, "P5\n%u %u\n255\n", width, height);// Write the valid image data one row at a timefor (UINT32 y = 0; y < height; y++) {fwrite(pDecodedBuffer + (y * lineBytes), sizeof(unsigned char), width, fp);}fclose(fp);printf("SUCCESS: Saved the image file (%s)\n", filename);} else {printf("Error: Failed to save the file.\n");}} else {printf("Error: Failed to decode the image data. ErrorCode: %d\n", ret);}} else {printf("Error: Failed to capture the image data. ErrorCode: %d\n", ret);}// 11. Free the memory and close the devicefree(pCompressedBuffer);free(pDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_SUCCESS;} -
Build and run
- Press
Ctrl+Shift+Bto build. - Run with
Ctrl+F5. If an image file at the configured size (800x320),custom_setting_image.pgm, is generated directly under the project folder, it has succeeded.
- Press
Key Configuration APIs and Parameters
Section titled “Key Configuration APIs and Parameters”Changing the resolution (PUC_SetResolution)
Section titled “Changing the resolution (PUC_SetResolution)”PUC_SetResolution(hDevice, width, height);- Configurable width/height: Width (e.g. 800), height (e.g. 600, 480, 320, 160, 80).
- Behavior when changed: When you change the resolution, the received data size (
PUC_GetXferDataSize) also changes automatically.
Changing the frame rate and shutter speed (PUC_SetFramerateShutter)
Section titled “Changing the frame rate and shutter speed (PUC_SetFramerateShutter)”PUC_SetFramerateShutter(hDevice, framerate, shutterSpeedFps);- framerate: The frame rate (e.g.
1000= 1000 fps). - shutterSpeedFps: The denominator of the shutter speed (e.g.
2000= 1/2000 sec). - Configuration constraint: The shutter speed must be set to a value equal to or faster than the frame rate (e.g. when capturing at 1000 fps, the shutter speed must be 1/1000 sec or faster).
