Loading...
Searching...
No Matches
images.cs

This example can be found in `examples\csharp\images\images.cs`. When the PST SDK has been installed through the PST Software Suite installer the examples can be found in the Development folder.

This example shows how to enable image transfer on the PST Tracker and how to use the PST SDK to retrieve images. Images are 8 bit grayscale and are stored as an IntPtr array without memory alignment or padding.

When compiling and running this example, please make sure that the required dependencies can be found by the executable (e.g. by copying the `Redist` directory into the build directory. When the PST SDK has been installed through the PST Software Suite installer the Redist folder can be found in the Development folder.).

// Copyright PS-Tech B.V. All Rights Reserved.
using System;
using System.Runtime.InteropServices;
using System.Threading;
using PSTech.Pstsdk;
namespace PSTech.Images
{
public class Images
{
/*
* Define handler functions required to ensure a clean shutdown of the PST Tracker when the
* application is terminated.
*/
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(ConsoleEventHandler handler, bool add);
private delegate bool ConsoleEventHandler(CtrlType sig);
static ConsoleEventHandler ConsoleCtrlEventHandler;
public enum CtrlType
{
CtrlCEvent = 0,
CtrlBreakEvent = 1,
CtrlCloseEvent = 2,
CtrlLogOffEvent = 5,
CtrlShutdownEvent = 6,
}
private static bool ConsoleHandler(CtrlType sig)
{
Exithandler((int)sig);
return true;
}
// Control variable for main loop.
static bool Running = true;
// Number of data points to grab before application termination.
static int NumberOfSamplesToGrab = 1000;
static int SamplesGrabbed = 0;
class MyTrackerListener : TrackerListener
{
public override void OnTrackerData(TrackerData data, ErrorStatus status)
{
if (SamplesGrabbed++ >= NumberOfSamplesToGrab)
{
Running = false;
}
// Do something with the received data.
}
}
static void Exithandler(int sig)
{
Tracker.Shutdown(); // Cleans up native resources only; does NOT remove listeners
Environment.Exit(0);
}
static int Main(string[] args)
{
// Register the exit handler with the application
ConsoleCtrlEventHandler += new ConsoleEventHandler(ConsoleHandler);
SetConsoleCtrlHandler(ConsoleCtrlEventHandler, true);
// Implement error handling of PSTech.TrackerException exceptions to prevent
// improper PST Tracker shutdown on errors.
try
{
// Create an instance of the Tracker object using the default configuration path and file names.
// Wrap the Tracker in a using statement to ensure Dispose() is called automatically
using (Tracker tracker = new Tracker())
{
// Print version number of the tracker server being used.
Console.Write("Running PST Server version " + tracker.GetVersionInfo() + "\n");
//Create tracker listener with callback functions for data and/or mode updates.
TrackerListener listener = new MyTrackerListener();
// Register the TrackerListener object to the tracker server.
tracker.AddTrackerListener(listener);
// Start the Tracker server.
tracker.StartTracker();
// Perform a system check to see if the tracker server is running OK and print the result.
Console.Write("System check: " + tracker.Systemcheck() + "\n\n");
Console.Write("***************************\n\n");
// Set the frame rate to 60 Hz.
tracker.SetFramerate(60);
Console.Write("Current frame rate: " + tracker.GetFramerate() + " Hz\n\n");
// In order to start receiving images, enable image transfer. When image transfer is disabled,
// the vector of images returned by GetImage will be empty.
tracker.EnableImageTransfer();
// The standard PST trackers will run at a reduced frame rate of 30 Hz when image transfer is enabled.
// However, since this frame rate is temporary for as long as image transfer is enabled, that frame rate
// will not be reported as the current frame rate.
Console.Write("Enabled image transfer. Current frame rate: " + tracker.GetFramerate() + " Hz\n\n");
Console.Write("***************************\n\n");
// Try to capture 100 images.
for (int i = 0; i < 100; ++i)
{
// Try to get the last grabbed image.
// Note that enabling image transfer takes some time. While image transfer is being enabled,
// the images array in the PSTech.Pstsdk.Image object will be empty.
// Image object return array of Intptr to create images.
Pstsdk.Image images = tracker.GetImage();
Console.Write("Retrieval operation successful!\n");
Console.Write("Retrieved " + images.Images.Length + " image(s) of size: " + images.Width + " X " + images.Height + "\n\n");
// Do something with the image.
// Don't request images too fast, wait for around 1/60 seconds
Thread.Sleep(17);
}
// Wait for 5 seconds, since this is > 4 seconds, image transfer will be disabled automatically.
Console.Write("Waiting 5 seconds for image transfer to be automatically disabled...\n\n");
Thread.Sleep(5000);
// Try to grab one image. Since image retrieval timed out, it should return an empty image vector.
Pstsdk.Image image = tracker.GetImage();
Console.Write("Retrieval operation successful!\n");
Console.Write("Retrieved " + image.Images.Length + " image(s) of size: " + image.Width + " X " + image.Height + "\n\n");
// Main loop, wait for auto-termination.
while (Running)
{
Thread.Sleep(100);
}
}
}
catch (Exception e)
{
// Catch PSTech.TrackerException exceptions and print error messages.
Console.Write(e + "\n");
}
// Pause command line to see results.
Console.Write("Press enter to continue...\n");
Console.Read();
return 0;
}
}
}
PSTech.Pstsdk.TrackerData
Tracking information retrieved from tracker.
Definition TrackerData.cs:25
PSTech.Pstsdk.Tracker.GetFramerate
double GetFramerate()
Get current frame rate.
Definition Tracker.cs:589
PSTech.Pstsdk.Tracker.StartTracker
void StartTracker()
Start tracking.
Definition Tracker.cs:524
PSTech.Pstsdk.Tracker.EnableImageTransfer
void EnableImageTransfer()
Enable image transfer from the PST Tracker.
Definition Tracker.cs:758
PSTech.Pstsdk.Tracker.Systemcheck
StatusMessage Systemcheck()
Check if the tracker is running correctly.
Definition Tracker.cs:568
PSTech.Pstsdk.Tracker.AddTrackerListener
void AddTrackerListener(TrackerListener listener)
Add a listener for tracker data and/or mode change reporting.
Definition Tracker.cs:476
PSTech.Pstsdk.Tracker.GetImage
Image GetImage()
Retrieve images from the connected PST Tracker.
Definition Tracker.cs:783
PSTech.Pstsdk.Tracker.SetFramerate
void SetFramerate(double fps)
Set tracker frame rate.
Definition Tracker.cs:579
PSTech.Pstsdk.Tracker.GetVersionInfo
string GetVersionInfo()
Get version information of the SDK.
Definition Tracker.cs:400
PSTech.Pstsdk.TrackerListener
Implements the TrackerListener class to receive tracking information and tracking mode updates.
Definition Tracker.cs:166
PSTech.Pstsdk
Definition CApi.cs:9
PSTech.Pstsdk.ErrorStatus
ErrorStatus
Tracker error messages enum class.
Definition ErrorHandler.cs:17