Toupcam API Manual


1. Version & Platform



2. Introduction


Toupcam cameras support various kinds of APIs (Application Program Interface), namely Native C/C++, .NET/C#/VB.NET, Python, Java, DirectShow, Twain, LabView, MabLab and so on. Compared with other APIs, Native C/C++ API, as a low level API, don't depend any other runtime libraries. Besides, this interface is simple, flexible and easy to be integrated into the customized applications. The SDK zip file contains all of the necessary resources and information:

toupcam.h, C/C++ head file


3. Concepts and terminology


a. Modes for accessing image data: "Pull Mode" vs "Push Mode"

Toupcam offers two modes to obtain image data: Pull Mode and Push Mode. The former is recommended since it's simpler and the application seldom gets stuck in multithreading conditions, especially when using windows message to notify the events.

There are to ways to notify applications:

a) Use Windows message: Start pull mode by using the function Toupcam_StartPullModeWithWndMsg. When event occurs, toupcam will post message (PostMessage) to the specified window. Parameter WPARAM is the event type, refer to the definition of TOUPCAM_EVENT_xxxx. This model avoids the multithreading issues, so it's the most simple way. (Obviously, this is only supported in Windows systems, and not supported in Linux and macOS.)

b) Use Callback function: Start pull mode by using the function Toupcam_StartPullModeWithCallback. When event occurs, toupcam will callback the function PTOUPCAM_EVENT_CALLBACK.

In Pull Mode, the SDK could not only notify the application that the image data or still image are available for 'PULL', but also inform you of the other events, such as:

TOUPCAM_EVENT_EXPOSURE exposure time changed
TOUPCAM_EVENT_TEMPTINT white balance changed. Temp/Tint Mode, please see here.
TOUPCAM_EVENT_WBGAIN white balance changed. RGB Gain Mode, please see here.
TOUPCAM_EVENT_IMAGE Video image data arrives. Use Toupcam_PullImage(V2) to 'pull' the image data
TOUPCAM_EVENT_STILLIMAGE Still image which is triggered by function Toupcam_Snap or Toupcam_SnapN arrives. Use Toupcam_PullStillImage(V2) to 'pull' the image data
TOUPCAM_EVENT_ERROR Generic error, data acquisition cannot continue
TOUPCAM_EVENT_DISCONNECTED Camera disconnected, maybe has been pulled out
TOUPCAM_EVENT_NOFRAMETIMEOUT Grab image no frame timeout error, data acquisition cannot continue
TOUPCAM_EVENT_NOPACKETIMEOUT Grab image no packet timeout
TOUPCAM_EVENT_TRIGGERFAIL trigger failed (for example, bad frame data or timeout)
TOUPCAM_EVENT_BLACK black balance changed
TOUPCAM_EVENT_FFC flat field correction status changed
TOUPCAM_EVENT_DFC dark field correction status changed
TOUPCAM_EVENT_ROI roi changed
TOUPCAM_EVENT_FACTORY restore factory settings. Please note that restoring factory settings may cause resolution changes.

b. Still Capture (Still Image)

Most cameras support the so-called still capture capability. This function switches the camera to another resolution temporarily when the camera is in preview mode, after a "still" image in the new resolution is captured and then switch back to the original resolution and resume preview mode.

For example, UCMOS05100KPA support 3 resolutions and the current one in preview mode is 1280 * 960. Call Toupcam_Snap(h, 0) to "still capture" an image in 2592 * 1944 resolution. To realize this function, the camera will temporarily switch to 2592 * 1944 firstly, get an image in 2592 * 1944 resolution and then switch back to 1280 * 960 and resume preview.

a) In pull mode operation, after the still capture, TOUPCAM_EVENT_STILLIMAGE will be sent out for external acknowledgement. The external application should call Toupcam_PullStillImage(V2) to get the still captured image.
b) In push mode operation, after the still capture, the callback function PTOUPCAM_DATA_CALLBACK_V3 will be called with bSnap parameter setting TRUE. The image information including the resolution information will be obtained via the parameter pHeader.

To check whether the camera have the still capture capability, call Toupcam_get_StillResolutionNumber function or check the still field of the struct ToupcamModelV2.

c. Data format: RGB vs RAW

Toupcam supports two data formats: RGB format (default) and RAW format. RAW format could be enabled by assigning TOUPCAM_OPTION_RAW parameter to 1 when calling Toupcam_put_Option function.

Users could switch these two format by calling Toupcam_put_Option function with different value setting to TOUPCAM_OPTION_RAW. You must call this function BEFORE the camera start function (Toupcam_StartPullModeWithWndMsg or Toupcam_StartPullModeWithCallback or Toupcam_StartPushMode).

d. White Balance and Auto White Balance: Temp/Tint mode vs RGB Gain mode

1. Toupcam sdk supports two independent modes for white balance: a) Temp/Tint Mode; b) RGB Gain Mode

a) Temp/Tint mode is the default white balance mode. In this mode, temp and tint are the parameters that could be used to control the white balance. Toupcam_get_TempTint function is used to acquire the temp and tint values and Toupcam_put_TempTint is used to set the temp and tint values. Function Toupcam_AwbOnePush is used to execute the auto white balance. When the white balance parameters change, TOUPCAM_EVENT_TEMPTINT event will be notified for external use.

b) In RGB Gain mode, the while balace is controled by the gain values of the R,G,B channels. Toupcam_get_WhiteBalanceGain is used to acquire the parameters and Toupcam_put_WhiteBalanceGain is used to set the white balance parameters. Toupcam_AwbInit is used to execute the execute the auto white balance. When the white balance parameters change, TOUPCAM_EVENT_WBGAIN event will be notified for external use.

The functions for these two modes cannot be misused:

a) In Temp/Tint mode, please use Toupcam_get_TempTint and Toupcam_put_TempTint and Toupcam_AwbOnePush. Toupcam_get_WhiteBalanceGain and Toupcam_put_WhiteBalanceGain and Toupcam_AwbInit cannot be used, they always return E_NOTIMPL.
b) In RGB Gain mode, please use Toupcam_get_WhiteBalanceGain and Toupcam_put_WhiteBalanceGain and Toupcam_AwbInit. Toupcam_get_TempTint and Toupcam_put_TempTint and Toupcam_AwbOnePush cannot be used, they always return E_NOTIMPL

When calling Toupcam_Open function, whether to add a '@' character at the beginning of the id parameter will determine the white balance mode. Add a '@' character at the beginning of the id parameter means the RGB gain mode. If you want to use the RGB Gain mode, for example, if the id parameter is "abcdef", please call the Toupcam_Open function with the id parameter "@abcdef".

2. There are two auto white balance mechanisms available in this field: one is continuous auto white balance and the other is a "one push" auto white balance. The white balance parameters will be always calculated and updated for the continuous auto white balance mechanism. For "one push" auto white balance mechanism, the white balance parameters will be calculated and updated only when triggered. Toupcam cameras support "one push" auto white balance mechanism since it is more accurate and propriate for the microscope application, especially when the background is in pure color. Continuous white balance mechanism will encounter some problem in some cases.

3. Monochromatic camera does not support white balance. The functions metioned above always return E_NOTIMPL.

e. Trigger Mode

1. What is Trigger Mode

Toupcam camera has two working modes: video mode and trigger mode. When in trigger mode, no images will be available until the trigger conditions are met. Cameras have 2 types for triggering according to the types of trigger source, including software trigger mode, external trigger mode and simulated trigger mode.

2. The Difference between Trigger and Snap

Trigger mode is designed for accurate control of the camera and images would be acquired only when the conditions are met. Users could get the images by controlling the trigger conditions. Trigger mode must be pre-specified. Once the trigger mode is entered, no images will come out from the camera until the trigger conditions are met. The triggered images will stay the same property as the pre-specified resolution. Snap is designed to acquire the images from the camera in video mode. The resolution of the snapped image could be the same resolution as the video or could be different.

3. Software Trigger Mode

Camera could be triggered by software. In software trigger mode, images burst out only when users trigger the camera from the software. Numbers of the images of the triggering could also be controlled by software.

4. External Trigger Mode

Camera could be triggered by external trigger signal. By now Toupcam camera only supports positive-edge trigger mode.

5. Mix Trigger Mode

Both external and software trigger are enabled.

6. Simulated Trigger Mode

For cameras that do not support software trigger and external trigger, simulated trigger mode could be available. When in simulated trigger mode, the camera hardware actually works in the same mode as the video mode. But the up-level software will not obtain any images from the camera. The software buffer will stay empty until the user set the trigger conditions by software.

7. How to Enter the Trigger Mode

After the numeration of the connected camera, you can check the flag and find out what trigger mode does the camera support according to the following definition.
#define TOUPCAM_FLAG_TRIGGER_SOFTWARE   0x00080000  /* support software trigger */
#define TOUPCAM_FLAG_TRIGGER_EXTERNAL   0x00100000  /* support external trigger */
#define TOUPCAM_FLAG_TRIGGER_SINGLE     0x00200000  /* only support trigger single: one trigger, one image */
Function Toupcam_put_Option(HToupcam h, unsigned iOption, int iValue) could be used to set the camera to trigger mode when assign TOUPCAM_OPTION_TRIGGER to the iOption parameter. iValue is used to specify the types of the trigger modes. Please see the following for reference.
#define TOUPCAM_OPTION_TRIGGER   0x0b    /* 0 = video mode, 1 = software or simulated trigger mode, 2 = external trigger mode, 3 = external + software trigger, default value =  0 */
Function Toupcam_get_Option(HToupcam h, unsigned iOption, int* piValue) could be used to get what type of trigger mode the camera is in.

8. How to Trigger the camera

Function Toupcam_Trigger(HToupcam h, unsigned short nNumber) could be used to trigger the camera. Assigning different value to nNumber means different:
nNumber = 0 means stop the trigger.
nNumber = 0xFFFF means trigger continuously, the same as video mode;
nNumber = other valid values means nNumber images will come out from the camera.
If the TOUPCAM_FLAG_TRIGGER_SINGLE flag is checked, the nNumber parameter must be assigned to 1 and 1 image will come out from the camera when Toupcam_Trigger is called.
Enter the trigger mode first and then call Toupcam_Trigger function to trigger the camera.

9. Trigger timeout

The timeout is recommended for not less than (Exposure Time * 102% + 4 Seconds).

4. Functions



5. .NET and C# and VB.NET


Toupcam does support .NET development environment (C# and VB.NET).

toupcam.cs use P/Invoke to call into toupcam.dll. Copy toupcam.cs to your C# project, please reference demowinformcs1, demowinformcs2 and demowinformcs3 in the samples directory.

Please pay attation to that the object of the C# class Toupcam. Toupcam must be obtained by static mothod Open or OpenByIndex, it cannot be obtained by obj = new Toupcam (The constructor is private on purpose).

Most properties and methods of the Toupcam class P/Invoke into the corresponding Toupcam_xxxx functions of toupcam.dll/so. So, the descriptions of the Toupcam_xxxx function are also applicable for the corresponding C# properties or methods. For example, the C# Snap method call the function Toupcam_Snap, the descriptions of the Toupcam_Snap function is applicable for C# Snap method:

[DllImport("toupcam.dll", ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int Toupcam_Snap(SafeHToupcamHandle h, uint nResolutionIndex);

public bool Snap(uint nResolutionIndex)
{
    if (_handle == null || _handle.IsInvalid || _handle.IsClosed)
        return false;
    return (Toupcam_Snap(_handle, nResolutionIndex) >= 0);
}

VB.NET is similar with C#, not otherwise specified.


6. Python


Toupcam does support Python (version 3.0 or above), please import toupcam to use toupcam.py and reference the sample code simplest.py and qt.py.

Please pay attation to that the object of the python class toupcam.Toupcam must be obtained by classmethod Open or OpenByIndex, it cannot be obtained by obj = toupcam.Toupcam()

Most methods of the Toupcam class use ctypes to call into the corresponding Toupcam_xxxx functions of toupcam.dll/so/dylib. So, the descriptions of the Toupcam_xxxx function are also applicable for the corresponding python methods.

Please reference __errcheck in toupcam.py, the original HRESULT return code is mapped to HRESULTException exception (in win32 it's inherited from OSError).

Please make sure the toupcam dll/so/dylib library file is in the same directory with toupcam.py.

7. Java


Toupcam does support Java, toupcam.java use JNA (https://github.com/java-native-access/jna) to call into toupcam.dll/so/dylib. Copy toupcam.java to your java project, please reference the sample code simplest.java (Console), javafx.java, swing.java.

Please pay attation to that the object of the java class toupcam must be obtained by static method Open or OpenByIndex, it cannot be obtained by obj = new toupcam()(The constructor is private on purpose).

Most methods of the toupcam class use JNA to call into the corresponding Toupcam_xxxx functions of toupcam.dll/so/dylib. So, the descriptions of the Toupcam_xxxx function are also applicable for the corresponding java methods.

Please reference errcheck in toupcam.java, the original HRESULT return code is mapped to HRESULTException exception.

Remark: (1) Download jna-*.jar from github; (2) Make sure toupcam.dll/so/dylib is placed in the correct directory.


8. Changelog


v46: Add support denose. Please see here

v45: Add sequencer trigger, UART, mix trigger (external and software trigger both are enabled)

v44: Extend the realtime mode, Please see here
       Add TOUPCAM_OPTION_CALLBACK_THREAD and TOUPCAM_OPTION_FRAME_DEQUE_LENGTH

v43: Reload the last frame in the trigger mode. Please see TOUPCAM_OPTION_RELOAD

v42: Precise frame rate and bandwidth. Please see here and TOUPCAM_FLAG_PRECISE_FRAMERATE

v41: no packet timeout. Please see here

v40: Auto test tool, see samples\AutoTestTool

v39: Update C#/VB.NET/Java/Python

v38: Add support to byte order, change BGR/RGB. Please see here

v37: Add Android support
       Add Toupcam_StartPushModeV3 (Toupcam_StartPushModeV2 and Toupcam_StartPushMode are obsoleted)

v36: Add Java support. Please see here

v35: Add Python support. Please see here

v34: Auto Focus and Focus Motor

v33: extend TOUPCAM_OPTION_AGAIN to TOUPCAM_OPTION_AUTOEXP_POLICY, support more options. Please see here

v32: Addd support to Windows 10 on ARM and ARM64, both desktop and WinRT

v31: Add Toupcam_deBayerV2, support RGB48 and RGB64

v30: Add TOUPCAM_FLAG_CGHDR

v29: Add ToupcamFrameInfoV2, a group of functions (PullImageV2 and StartPushModeV2), some cameras support frame sequence number and timestamp. Please see here

v28: Add Toupcam_read_Pipe, Toupcam_write_Pipe, Toupcam_feed_Pipe

v27: Add Toupcam_SnapN, support to snap multiple images, please see here and democpp

v26: Add support to restore factory settings, TOUPCAM_OPTION_FACTORY

v25: Add sharpening, TOUPCAM_OPTION_SHARPENING

v24: Add support to Auto Exposure with the 50/60 HZ constraint

v23: Add support to Linux armhf, armel and arm64
       Add FFC and DFC, please see here and here

v22: Add TOUPCAM_OPTION_DDR_DEPTH, please see here

v21: Add Toupcam_IoControl

v20: Add Toupcam_EnumV2, ToupcamModelV2, ToupcamDeviceV2; (Toupcam_Enum, ToupcamModel and ToupcamDevice are obsoleted)
       Add Pixel Format, see TOUPCAM_OPTION_PIXEL_FORMAT; (TOUPCAM_OPTION_PIXEL_FORMAT is the super set of TOUPCAM_OPTION_BITDEPTH)
       Add Flat Field Correction

v19: Add Black Balance: please see here

v18: Add Toupcam_get_Revision

v17: Add TOUPCAM_OPTION_ROTATE

v16: Add TOUPCAM_FLAG_DDR, use very large capacity DDR (Double Data Rate SDRAM) for frame buffer

v15: Add TOUPCAM_OPTION_BINNING

v14: Add support to WinRT / UWP (Universal Windows Platform) / Windows Store App

v13: support row pitch, please see Toupcam_PullImageWithRowPitch and Toupcam_PullStillImageWithRowPitch

v12: support RGB32, 8 bits Gray, 16 bits Gray, please see here

v11: black level: please see here

v10: demosaic method: please see here

v9: change the histogram data type from double to float

v8: support simulated trigger, please see here

v7: support RGB48 when bit depth > 8, please see here

v6: support trigger mode, please see here

v5: White Balance: Temp/Tint Mode vs RGB Gain Mode, please see here

v4: ROI (Region Of Interest) supported: please see here

v3: more bit depth supported: 10bits, 12bits, 14bits, 16bits

v2: support RAW format, please see here and here; support Linux and macOS

v1: initial release


9. Contact


If you have any problems in using this SDK, please use the following information to contact us.
www: www.touptek.com
email: support@touptek.com