CatchBEST Camera API Programming Introduction
CatchBEST Camera API Programming Introduction
3. Set the capture field of view (if the user does not set, the default is the large format field of view, no
skip mode), the following setting is the 0th capture device field of view 0,0,1280,1024, no skip mode:
4. “KSJ_CaptureGetSize” function can get image’s width and height (unit: pixel):
5. Call “KSJ_SetParam (KSJ_RED, nRedValue);” to set the gain, for the black&white camera, you can
casually select “KSJ_RED” or “KSJ_GREEN”, or “KSJ_BLUE”, only call one of the three, the others will
follow the changes to the same value. You can set the exposure value by calling “KSJ_SetParam
(KSJ_EXPOSURE, nEValue);”.
7. Call the capture function to capture “KSJ_CaptureRawData (0, pData);” , for black&white camera do
not call this function.
KSJ_CaptureRgbData.
8. If you want to save the data as bmp, jpg, please call “KSJ_HelperSaveToBmp”, or
“KSJ_HelperSaveToJpg”. Note: “nBitCount” value is 8.
2. Set the preview field of view (if the user does not set, the default is the large format field of view, no
skip mode), the following setting is the 0th capture device field of view 0,0,1280,1024, no skip mode:
3. Call “KSJ_PreviewGetSize” function to get the width and height of preview image (unit:pixel):
4. Call “KSJ_PreviewSetPos” to set which window the preview image will be displayed in and the
position of this window
6. Call “KSJ_SetParam (KSJ_RED, nRedValue);” to set the gain, for the black&white camera, you can
casually select “KSJ_RED” or “KSJ_GREEN”, or “KSJ_BLUE”, only call one of the three, the others will
follow the changes to the same value. You can set the exposure value by calling “KSJ_SetParam
(KSJ_EXPOSURE, nEValue);”.
Note: The development package completely separated video preview and capture, these two parts
can be run separately, and also can be run at the same time time,, so the skip mode can be different at
view,, It is more in line with the requirements of the application.
preview and capture of field of view
2. Get the device number of contacted the PC: int nDeviceNum = KSJ_DeviceGetCount();
3. Set the capture field of view (if the user does not set, the default is the large format field of view, no
skip mode), the following setting is the 0th capture device field of view 0,0,1280,1024, no skip mode:
5. Call “KSJ_SetParam” function to set “KSJ_RED”, “KSJ_GREEN”, “KSJ_BLUE” colour’s gain. Call
“KSJ_SetParam(KSJ_EXPOSURE,nEValue);” function to set the exposure value.
8. If you want to save the data as format of bmp, jpg, please call function “KSJ_HelperSaveToBmp” or
“KSJ_HelperSaveToJpg”, note: the value of nBitCount is 24.
1. Get the device number of contacted host by calling function “int nDeviceNum
=KSJ_DeviceGetCount();”
2. Set the preview field of view (if the user does not set, the default is the large format field of view, no
skip mode), the following setting is the 0th capture device field of view 0,0,1280,1024, no skip mode:
3. Calling function “KSJ_PreviewGetSize” to get the preview image’s width and height (unit: pixel): Int
nWidth, nHeight;
4. Call function “KSJ_PreviewSetPos” to set which window the preview image will be displayed in and
the position of window.
6. Call function “KSJ_SetParam” to set “KSJ_RED”, “KSJ_GREEN”, “KSJ_BLUE” colour ‘s gain. Call function
“KSJ_SetParam(KSJ_EXPOSURE,nEValue);” to set the exposure value.
7. Call “KSJ_PreviewPause(0,true);” to pause the preview. After the successful pause, call function
“KSJ_PreviewGetPausedData(0, pData)” to get that frame image, you need allocated memory before
calling this function, and get the required memory size by calling “KSJ_PreviewGetSize”, the data can
be saved as BMP or JPG file by calling “KSJ_HelperSaveToBmp” or “KSJ_HelperSaveToJpg” function.
After that call “KSJ_PreviewPause(0, false);” function to continue the video stream, finally call
“KSJ_PreviewStart(0, false);” to turn off the video stream.
GND
In the camera, SNAP connection is connected to the pull-up resistor, so the normal is high-voltage (3.3V),
when the SW1 button is pressed, SNAP connection is low-voltage.
We can the call function “KSJ_API int __ stdcall KSJ_SnapButtonStatus ( int nChannel,
KSJ_SNAPBUTTONSTATUS * pSnapButtonStatus);” to get SNAP connection status to determine the status
of the SW1 button. Through this status, we can program to do low-voltage trigger for capture or fall time
of pulse trigger for capture control.
The capture operation of the external control trigger has two simple programmings:
1.1 Set the preview callback function, please refer to the explaination of “KSJ_PreviewSetCallback” in the
“CatchBEST Camera API Function Instruction Manual” and see the call example.
1.2 Read the button’s status by calling the callback function “KSJ_SnapButtonStatus”, when the conditions
are met, this frame image will be captured by the external trigger. The following example means that
when you pressed one button you can make a capture:
int CMyWnd::PreviewCallback(unsigned char *pImageData, int nWidth, int nHeight, int nBitCount)
{
// Read the status of the button
KSJ_SnapButtonStatus( m_nDeviceIndex, &CurSnapButtonStatus );
TCHAR Buf[256] = {‘\0’};
_stprintf( Buf, _T("Snap Button: %s\n"), CurSnapButtonStatus == KSJ_UP ? _T("UP") : _T("DOWN") );
OutputDebugString( Buf );
// Press the button, save this frame image
if( CurSnapButtonStatus == KSJ_DOWN && PrvSnapButtonStatus == KSJ_UP )
KSJ_HelperSaveToBmp( pImageData, nWidth, nHeight, nBitCount, "c:\\snap.bmp" );
PrvSnapButtonStatus = CurSnapButtonStatus;
}
2.P
Polling in the thread test
2.2 Check the external button status in the threads or timer condition reached, and when conditions
fullfilled, call the related capture function to get the image.
Note: this external trigger capture mode can completely separate the preview and external trigger
capture, that is, you can check the external button status while you open the preview video stream, when
the conditions fullfilled, You can call the capture function to capture and preview different field of view.
For details, please refer to the related function explain of capture and preview in the “CatchBEST Camera
API Function Instruction Manual”. The following code means read the external trigger buton status in the
timer of 1000ms, when the button press is detected, capture an image according to the setted capture
field of view:
// First, set one timer of 1000 ms
SetTimer( 1, 1000, NULL );
// Read the information of the external trigger key within timer, capture a image when the external
button is pressed.
// Read the capture image size in the current field of view setting.
KSJ_CaptureGetSize( m_nDeviceIndex, &nWidth, &nHeight );
PrvSnapButtonStatus = CurSnapButtonStatus;
CMDIChildWnd::OnTimer(nIDEvent);
}
Explain: there’s only inquery mode, and no intermit mode in reading the external trigger.
Programming for multiple cameras is relatively simple, brief steps are as follows:
1. Initialize the dynamic library, call function “KSJ_Init”.
2. Get the device number of connected to the host: int nDeviceNum = KSJ_DeviceGetCount();
3. According to the devices number, in turn call “KSJ_API int __ stdcall, KSJ_DeviceGetInformation (IN
int nChannel, the OUT KSJ_DEVICETYPE * pDeviceType, OUT int * pnIndex, the OUT WORD *
pwFirmwareVersion); ” to get the model of each camera, the same type of equipment distinguish by
serial number. We also can provide the corresponding program to rewrite the internal serial number
of each camera. The detail operations please see “BestScope firmware upgrade guide and rewrite the
serial number”.
4. After that use “nChannel=0,1,...(nDeviceNum-1)” to call “KSJApi” function to operate each camera
separately. For the detail operation, please refer to the above described “black and white camera
operation” and “color camera operation”.
1. Preview recovery
When the user calls function “KSJ_PreviewStart (nChannel, TRUE);”, API development package will display
the video stream on the user specified window, unplug the camera's USB cable, the API will continuously
detect user’s re-insert the camera operation, if Yes, API will call the user’s previous field of view setting to
camera, and automatically restore the video stream, so the abnormal recovery of the camera are
auto-completed in development package, the user don’t need special operation in the programming.
2. Capture recovery
When the user called function “KSJ_CaptureRawData”, or “KSJ_CaptureRgbData”, if the camera's USB
cable has been pulled out, the above function will return to the “RET_RECOVERY_FAIL” , when the camera
is connected again, call “KSJ_CaptureRawData” or “KSJ_CaptureRgbData” function will automatically
recover the camera, and reset settings in accordance with previous user’s field of view setting, so users
do not need special operate for the capture automatically recovery, but must judge whether or not
“KSJ_CaptureRawData”, or “KSJ_CaptureRgbData” return value is “RET_SUCCESS”.
Note: When between the camera pulled out to re-connect, if the user does not call
“KSJ_CaptureRawData “ or “KSJ_CaptureRgbData”, then once the camera is connected, the first time call
“KSJ_CaptureRawData “ or “KSJ_CaptureRgbDat”, it will return to “RET_RECOVERY_SUCCESS”, this does
not mean “KSJ_CaptureRawData”, or “KSJ_CaptureRgbData” are successfully called, but means the
capture failed, the recovery is successful. Call “KSJ_CaptureRawData” or “KSJ_CaptureRgbData” function
again, then it will return to “RET_SUCCESS”.
Note: after the camera is disconnected, it’s will return to “RET_FAIL” when call some functions which are
communicating with the hardware. For example, the “KSJ_DeviceGetInformation” and “KSJ_SetParam”
functions only can be called after confirm that the camera has recovered to nomal status.
“KSJ_DeviceGetInformation”, and “KSJ_SetParam” functions do not have internal recovery mechanism.
For capture recovery, the recovery mechanism only exists in the two functions of “KSJ_CaptureRawData”,
and “KSJ_CaptureRgbData” function.
Each square represents a pixel, each pixel only receives R, G, B, one of them. After Bayer conversion, each
pixel will be reconstruct of a 24 or 32-bit RGB of color pixels, all pixels will be reconstructed to create a
color image by Bayer mode. The user can set Bayer mode by calling “KSJ_BayerSetMode”. Currently
supported Bayer mode including:
KSJ_BGGR_BGR24
Pixels arrangement is:
B G B G…
G R G R…
B G B G…
…
After conversion it is a 24-bit bitmap format. In order to keep the display image not flip vertically, we will
transform the first pixel line to the last line of the RGB bitmap memory (this is because “Windows GDI”
function displays the first line in the image is actually the last line of the image in memory), so when you
set “KSJ_BGGR_BGR24”, “KSJ_GRBG_BGR24”, “KSJ_RGGB_BGR24”, “KSJ_GBRG_BGR24”,
“KSJ_BGGR_BGR32”, “KSJ_GRBG_BGR32”, “KSJ_RGGB_BGR32”, “KSJ_GBRG_BGR32” functions. You can
get the RGB bitmap data after calling of the “KSJ_CaptureRgbData” or “KSJ_CaptureRgbDataEx”. The pixel
conversion results under the photosensitive array picture are stored in the starting address.
If you want to save pixel converted values of the photosensitive array into the RGB bitmap data
starting address in memory, you can call “KSJ_BGGR_BGR24_FLIP”, “KSJ_GRBG_BGR24_FLIP”,
“KSJ_RGGB_BGR24_FLIP”, “KSJ_GBRG_BGR24_FLIP”, “KSJ_BGGR_BGR32_FLIP”, “KSJ_GRBG_BGR32_FLIP”,
“KSJ_RGGB_BGR32_FLIP”, “KSJ_GBRG_BGR32_FLIP” functions according to the pixel arrangement.
KSJ_GRBG_BGR24
KSJ_GRBG_BGR32
KSJ_GRBG_BGR24_FLIP
KSJ_GRBG_BGR32_FLIP
Pixels arrangement is:
G R G R…
B G B G…
G R G R…
…
After conversion it is a 24-bit or 32-bit bitmap format(it is decided by “BGR24” or “BGR32”).
The 1st row of pixel arrangement is converted to the last row or the first row of RGB bitmap memory
address(it is decided by the “FLIP”).
KSJ_RGGB_BGR24
KSJ_RGGB_BGR32
KSJ_RGGB_BGR24_FLIP
KSJ_RGGB_BGR32_FLIP
Pixels arrangement is:
R G R…
G B G…
R G R…
…
After conversion it is a 24-bit or 32-bit bitmap format(it is decided by “BGR24” or “BGR32”).
The 1st row of pixel arrangement is converted to the last row or the first row of RGB bitmap memory
address(it is decided by the “FLIP”).
KSJ_GBRG_BGR24
KSJ_GBRG_BGR32
KSJ_GBRG_BGR24_FLIP
The 1st row of pixel arrangement is converted to the last row or the first row of RGB bitmap memory
address(it is decided by the “FLIP”).
Note: The white balance area location will also be adjusted, the adjusted result makes the upper left
conrner of the display images as the origin, the user setted white balance area unchanged relative to the
upper left corner of the image.
Note: When a user call “KSJ_SetParam” function and set “KSJ_FLIP” as 1, it means that control the front
array to output from the last row, that is, the raw data which we read will vertically flip. So that the
“Bayer” mode also need to be setted based on the actual situation. For example, the original is “RGGB”,
after flip, it is “GBRG” mode.
Explanation: The above settings on the modes, regardless of the flip or not flip, it will not affect the
performance of capture. So users please do not write your own flip algorithm, you should use “KSJ_FLIP”
and “KSJ_BayerSetMode” of “KSJ_SetParam” to find a proper combination to flip.
Instruction on “GPIO
GPIO”” operation
CatchBESTA/B/C-130C, CatchBESTA/B/C-130M and CatchBESTA/B/C-320C have 4 “IO” can be provided for
customers to operate. Users can call “KSJ_GpioSetDirection”, “KSJ_GpioGetDirection”,
“KSJ_GpioSetStatus”, and “KSJ_GpioGetStatus” functions to set and read input/output status, electrical
level status of “IO” pins. The 4 “IO” are connected to a pull-up resistor inside the camera. For detail
introduction of the 4 functions, please refer to “CatchBEST Camera API Function Instruction Manual”.
Call example (Set “GPIO0-GPIO” as output, “GPIO0” output 1, “GPIO1” output 0, “GPIO2” output 1;
Set “GPIO3” as input, and read the input status of “GPIO3” pin)
// Set input/output status of pin, “GPIO3:GPIO” correspond to “BIT3:BIT0”, the corresponding 1 means
output, 0 means input.
KSJ_GpioSetDirection( nDeviceIndex, 0x07);
// Set output electrical level of the output pin, will not effect input pin
KSJ_GpioSetStatus( nDeviceIndex, 0x05);
If btStatus & 0x08 != 0x00, then means “GPIO3” input is 1, otherwise means input is 0.
PIN 1 2 3 4 5 6 7 8 9 10
Name LED+ USB_5V USB_GND SNAP IO1 IO0 IO3 IO2 TXD RXD
Pin1 (LED +): Anode drive LED, LED light when the PC capture images. LED + internal series connect 330Ω
resistor and connected to +3.3 V, the circuit connection is as follows:
LED+
LED
GND
Pin2 (USB_5V): Take +5 V voltage from the PC's USB interface for external supply, the current should be
less than 200mA.
Pin4 (SNAP): Connected to external capture button, press button to capture a single image. About
“External capture trigger” please refer to the related “the external trigger capture Programming " part in
this article. The connection circuit is as follows:
SW1
SNAP
GND
Pin5 (GPIO1), Pin6 (GPIO0), Pin7 (GPIO3), Pin8 (GPIO2): 4-bit GPIO port can be set by software for input
or output, and flexible to control. All of the “IO” ports in the camera are connected to +3.3 V via the
pull-up resistor. Regarding the related operation of “GPIO”, please refer to “Instructions on the GPIO”
section of this article.
Pin9 (TXD), Pin10 (RXD): extension of the asynchronous serial port. Not supported at present.
PIN 1 2 3 4 5 6 7 8 9 10
Name FLASH USB_5V USB_GND SNAP IO1 IO0 IO3 IO2 IO4 IO5
Pin1 (LED+): the flash trigger signal, the user can set the flash trigger signal by “KSJ_FlashControl”,
including enable or not, the effective level is overturned or not, the effective width etc. Details please
refer to the introductions on flash trigger signal in this article and “CatchBEST Camera API Function
Instruction Manual".
Please note that the signal directly comes from the camera sensor, it is only used as the trigger signal,
users need to consider of using switch triode circuit or Optical coupling isolation circuit. Large current or
pin2 (USB_5V): Take +5 V voltage from the PC's USB interface for external supply, the current should be
less than 200mA.
Pin4 (SNAP): Connected to external capture button, press button to capture a single image. About
“External capture trigger” please refer to the related “the external trigger capture Programming" part in
this article. The connection circuit is as follows:
SW1
SNAP
GND
Pin5 (GPIO1), Pin6 (GPIO0), Pin7 (GPIO3), Pin8 (GPIO2), Pin9(GPIO4), Pin10(GPIO5): 6-bit GPIO port can
be set by software for input or output, and flexible to control. All of the “IO” ports in the camera are
connected to +3.3 V via the pull-up resistor. Regarding the related operation of “GPIO”, please refer to
“Instructions on the GPIO” section of this article.
In order to support the exposure and external flash synchronization, the effective moment of the Flash
signal is right exposure time of all the rows. For the image sensor of electronic rolling shutter, only this
time is the global exposure, so suitable for use at night to add the flash to capture fast moving objects.
After Flash signal works (by calling KSJ_FlashControl), when capture image each time, output flash
signals.
In order to take normal images with flash, you must follow the below exposure settings:
The number of exposure rows must be greater than (effective line number of capture field of view +256)
the required time. The number 256 is the camera's default setted number of rows vertical blank. The
time required of each line is (capture field of view’s number of columns +1280) * (1/48000000).
1/48000000 is the point frequency, means the camera output 48000000 points per second, then the time
each point takes is 1/48000000 second. So (the number of columns of capture field of view +1280) *
(1/48000000) is the time of one line, the 1280 is the camera default settings for the line blanking time.
The exposure time unit which we provide for API function is “ms”, so you need to conversion.
For example, you set the column numbers of capture field of view, columns: X=4096, rows: Y = 2048, then
with the following formula, you can calculate the minimum exposure time:
(Y+256)*(X+1280)/48000000 = (2048+256)*(4096+1280)/48000000 = 0.258048(S) = 258.048(ms)
After user call the capture function, when the camera receive capture instructions of “TRIGGER” singal,
the camera will send the trigger signal to the image sensor, user don’t need concern it.
STROBE signal is what we said FLASH signal, when the image sensor receives the Trigger signal, in the (H +
VB) * tRow time, “H” means the number of rows in the setted field of view, “VB” is the vertical blanking
We default the time from TSE to TSW is a tRow time, this is the camera default setting timing, it is also
the timing of normal to use the flash. In fact, we can set “TSE” appear moment after “8* tRow” moment,
which after TRIGGER, extend the high levle of “FLASH (STROBE)”. We can customerize this program if
customers need.
All external trigger pins can be output by customorized air plugs, currently support four ports output.
For UC500C_MRYN, UC500M_MRYN, UC36C_MGYN and UC36M_MGYN, the standardized output way
are as follows:
Pin1:FLASH
Pin2:USB_5V
Pin3:USB_GND
Pin4:SNAP
Users can find the pin’s serial number next to the lead pin of plug. And offer socket as an accessory.