Introduction to CL Eye Platform C# APIAuthor: AlexPDate: 12-31-2009
Many of you asked us how can you use this camera in your applications, besides the existing implementation of PS3Eye DirectShow filter, we developed the PS3EyeLib as well...
Many of you asked us how can you use this camera in your applications, besides the existing implementation of PS3Eye DirectShow filter, we developed the PS3EyeLib as well. This SDK library lets you get full access to the PS3Eye camera and allows you to configure the camera and grab video frames.
Lets look at the Java Multicam API:
The latest setup file (PS3EyeSetup.2.0b81021), besides the PS3Eye driver, DirectShow filter and test app, includes the following files in the SDK directory:
IPS3EyeLib.h - Camera API include file
PS3EyeLib.lib - Camera API library file
PS3EyeLib.dll - Camera API dynamic library file
List of fixes/additions in the current release:
Created DirectShow camera property page (selectable resolution and frame rate)
Support for RGB-16/24/32 color output format
Implemented both 32-bit and 64-bit versions of the PS3Eye driver
Fixed the PS3EyeCamera.inf file so that drivers (32-bit and 64-bit) install correctly
Improved capture performance and responsiveness on Vista OS
////////////////////////////////////////////////////////////////////////////////////
//
// This file is part of CL-EyeMulticam SDK
//
// WPF C# Sample Application
//
// It allows the use of multiple CL-Eye cameras in your own applications
//
// For updates and file downloads go to: http://codelaboratories.com/research/view/cl-eye-muticamera-sdk
//
// Copyright 2008-2010 (c) Code Laboratories, Inc. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Threading;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
namespace CLEyeMulticam
{
#region [ Camera Parameters ]
// camera color mode
public enum CLEyeCameraColorMode
{
CLEYE_GRAYSCALE,
CLEYE_COLOR
};
// camera resolution
public enum CLEyeCameraResolution
{
CLEYE_QVGA, // Allowed frame rates: 15, 30, 60, 75, 100, 125
CLEYE_VGA // Allowed frame rates: 15, 30, 40, 50, 60, 75
};
// camera parameters
public enum CLEyeCameraParameter
{
// camera sensor parameters
CLEYE_AUTO_GAIN, // [false, true]
CLEYE_GAIN, // [0, 79]
CLEYE_AUTO_EXPOSURE, // [false, true]
CLEYE_EXPOSURE, // [0, 511]
CLEYE_AUTO_WHITEBALANCE, // [false, true]
CLEYE_WHITEBALANCE_RED, // [0, 255]
CLEYE_WHITEBALANCE_GREEN, // [0, 255]
CLEYE_WHITEBALANCE_BLUE, // [0, 255]
// camera linear transform parameters
CLEYE_HFLIP, // [false, true]
CLEYE_VFLIP, // [false, true]
CLEYE_HKEYSTONE, // [-500, 500]
CLEYE_VKEYSTONE, // [-500, 500]
CLEYE_XOFFSET, // [-500, 500]
CLEYE_YOFFSET, // [-500, 500]
CLEYE_ROTATION, // [-500, 500]
CLEYE_ZOOM, // [-500, 500]
// camera non-linear transform parameters
CLEYE_LENSCORRECTION1, // [-500, 500]
CLEYE_LENSCORRECTION2, // [-500, 500]
CLEYE_LENSCORRECTION3, // [-500, 500]
CLEYE_LENSBRIGHTNESS // [-500, 500]
};
#endregion
public class CLEyeCameraDevice : DependencyObject, IDisposable
{
#region [ CLEyeMulticam Imports ]
[DllImport("CLEyeMulticam.dll")]
public static extern int CLEyeGetCameraCount();
[DllImport("CLEyeMulticam.dll")]
public static extern Guid CLEyeGetCameraUUID(int camId);
[DllImport("CLEyeMulticam.dll")]
public static extern IntPtr CLEyeCreateCamera(Guid camUUID, CLEyeCameraColorMode mode, CLEyeCameraResolution res, int frameRate);
[DllImport("CLEyeMulticam.dll")]
public static extern bool CLEyeDestroyCamera(IntPtr camera);
[DllImport("CLEyeMulticam.dll")]
public static extern bool CLEyeCameraStart(IntPtr camera);
[DllImport("CLEyeMulticam.dll")]
public static extern bool CLEyeCameraStop(IntPtr camera);
[DllImport("CLEyeMulticam.dll")]
public static extern bool CLEyeSetCameraParameter(IntPtr camera, CLEyeCameraParameter param, int value);
[DllImport("CLEyeMulticam.dll")]
public static extern int CLEyeGetCameraParameter(IntPtr camera, CLEyeCameraParameter param);
[DllImport("CLEyeMulticam.dll")]
public static extern bool CLEyeCameraGetFrameDimensions(IntPtr camera, ref int width, ref int height);
[DllImport("CLEyeMulticam.dll")]
public static extern bool CLEyeCameraGetFrame(IntPtr camera, IntPtr pData, int waitTimeout);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr lpFileMappingAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool UnmapViewOfFile(IntPtr hMap);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr hHandle);
#endregion
#region [ Private ]
private IntPtr _map = IntPtr.Zero;
private IntPtr _section = IntPtr.Zero;
private IntPtr _camera = IntPtr.Zero;
private bool _running;
private Thread _workerThread;
#endregion
#region [ Events ]
public event EventHandler BitmapReady;
#endregion
#region [ Properties ]
public int Framerate{ get; set; }
public CLEyeCameraColorMode ColorMode{ get; set; }
public CLEyeCameraResolution Resolution{ get; set; }
public bool AutoGain
{
get
{
if (_camera == null) return false;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_AUTO_GAIN) != 0;
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_AUTO_GAIN, value ? 1 : 0);
}
}
public int Gain
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_GAIN);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_GAIN, value);
}
}
public bool AutoExposure
{
get
{
if (_camera == null) return false;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_AUTO_EXPOSURE) != 0;
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_AUTO_EXPOSURE, value ? 1 : 0);
}
}
public int Exposure
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_EXPOSURE);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_EXPOSURE, value);
}
}
public bool AutoWhiteBalance
{
get
{
if (_camera == null) return true;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_AUTO_WHITEBALANCE) != 0;
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_AUTO_WHITEBALANCE, value ? 1 : 0);
}
}
public int WhiteBalanceRed
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_WHITEBALANCE_RED);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_WHITEBALANCE_RED, value);
}
}
public int WhiteBalanceGreen
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_WHITEBALANCE_GREEN);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_WHITEBALANCE_GREEN, value);
}
}
public int WhiteBalanceBlue
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_WHITEBALANCE_BLUE);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_WHITEBALANCE_BLUE, value);
}
}
public bool HorizontalFlip
{
get
{
if (_camera == null) return false;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_HFLIP) != 0;
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_HFLIP, value ? 1 : 0);
}
}
public bool VerticalFlip
{
get
{
if (_camera == null) return false;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_VFLIP) != 0;
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_VFLIP, value ? 1 : 0);
}
}
public int HorizontalKeystone
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_HKEYSTONE);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_HKEYSTONE, value);
}
}
public int VerticalKeystone
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_VKEYSTONE);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_VKEYSTONE, value);
}
}
public int XOffset
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_XOFFSET);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_XOFFSET, value);
}
}
public int YOffset
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_YOFFSET);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_YOFFSET, value);
}
}
public int Rotation
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_ROTATION);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_ROTATION, value);
}
}
public int Zoom
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_ZOOM);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_ZOOM, value);
}
}
public int LensCorrection1
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_LENSCORRECTION1);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_LENSCORRECTION1, value);
}
}
public int LensCorrection2
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_LENSCORRECTION2);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_LENSCORRECTION2, value);
}
}
public int LensCorrection3
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_LENSCORRECTION3);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_LENSCORRECTION3, value);
}
}
public int LensBrightness
{
get
{
if (_camera == null) return 0;
return CLEyeGetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_LENSBRIGHTNESS);
}
set
{
if (_camera == null) return;
CLEyeSetCameraParameter(_camera, CLEyeCameraParameter.CLEYE_LENSBRIGHTNESS, value);
}
}
#endregion
#region [ Static ]
public static int CameraCount { get { return CLEyeGetCameraCount(); } }
public static Guid CameraUUID(int idx){ return CLEyeGetCameraUUID(idx); }
#endregion
#region [ Dependency Properties ]
public InteropBitmap BitmapSource
{
get { return (InteropBitmap)GetValue(BitmapSourceProperty); }
private set { SetValue(BitmapSourcePropertyKey, value); }
}
private static readonly DependencyPropertyKey BitmapSourcePropertyKey =
DependencyProperty.RegisterReadOnly("BitmapSource", typeof(InteropBitmap), typeof(CLEyeCameraDevice), new UIPropertyMetadata(default(InteropBitmap)));
public static readonly DependencyProperty BitmapSourceProperty = BitmapSourcePropertyKey.DependencyProperty;
#endregion
#region [ Methods ]
public CLEyeCameraDevice()
{
// set default values
Framerate = 15;
ColorMode = default(CLEyeCameraColorMode);
Resolution = default(CLEyeCameraResolution);
}
~CLEyeCameraDevice()
{
// Finalizer calls Dispose(false)
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
Stop();
}
// free native resources if there are any.
Destroy();
}
public bool Create(Guid cameraGuid)
{
int w = 0, h = 0;
_camera = CLEyeCreateCamera(cameraGuid, ColorMode, Resolution, Framerate);
if (_camera == IntPtr.Zero) return false;
CLEyeCameraGetFrameDimensions(_camera, ref w, ref h);
if (ColorMode == CLEyeCameraColorMode.CLEYE_COLOR)
{
uint imageSize = (uint)w * (uint)h * 4;
// create memory section and map
_section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, imageSize, null);
_map = MapViewOfFile(_section, 0xF001F, 0, 0, imageSize);
BitmapSource = Imaging.CreateBitmapSourceFromMemorySection(_section, w, h, PixelFormats.Bgr32, w * 4, 0) as InteropBitmap;
}
else
{
uint imageSize = (uint)w * (uint)h;
// create memory section and map
_section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, imageSize, null);
_map = MapViewOfFile(_section, 0xF001F, 0, 0, imageSize);
BitmapSource = Imaging.CreateBitmapSourceFromMemorySection(_section, w, h, PixelFormats.Gray8, w, 0) as InteropBitmap;
}
// Invoke event
if (BitmapReady != null) BitmapReady(this, null);
BitmapSource.Invalidate();
return true;
}
public void Destroy()
{
if (_map != IntPtr.Zero)
{
UnmapViewOfFile(_map);
_map = IntPtr.Zero;
}
if (_section != IntPtr.Zero)
{
CloseHandle(_section);
_section = IntPtr.Zero;
}
}
public void Start()
{
_running = true;
_workerThread = new Thread(new ThreadStart(CaptureThread));
_workerThread.Start();
}
public void Stop()
{
if (!_running) return;
_running = false;
_workerThread.Join(1000);
}
void CaptureThread()
{
CLEyeCameraStart(_camera);
int i = 0;
while (_running)
{
if (CLEyeCameraGetFrame(_camera, _map, 500))
{
if (!_running) break;
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, (SendOrPostCallback)delegate
{
BitmapSource.Invalidate();
}, null);
i++;
}
}
CLEyeCameraStop(_camera);
CLEyeDestroyCamera(_camera);
}
#endregion
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This file is part of CL-EyeMulticam SDK
//
// WPF C# Sample Application
//
// It allows the use of multiple CL-Eye cameras in your own applications
//
// For updates and file downloads go to: http://codelaboratories.com/research/view/cl-eye-muticamera-sdk
//
// Copyright 2008-2010 (c) Code Laboratories, Inc. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Diagnostics;
using System.Windows.Data;
using System.Windows.Controls;
namespace CLEyeMulticam
{
public class CLEyeCameraImage : Image, IDisposable
{
public CLEyeCameraDevice Device { get; private set; }
public CLEyeCameraImage()
{
Device = new CLEyeCameraDevice();
Device.BitmapReady += OnBitmapReady;
}
~CLEyeCameraImage()
{
// Finalizer calls Dispose(false)
Dispose(false);
}
void OnBitmapReady(object sender, EventArgs e)
{
Source = Device.BitmapSource;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
if (Device != null)
{
Device.Stop();
Device.Dispose();
Device = null;
}
}
// free native resources if there are any.
}
#region [ Dependency Properties ]
public int Framerate
{
get { return (int)GetValue(FramerateProperty); }
set { SetValue(FramerateProperty, value); }
}
public static readonly DependencyProperty FramerateProperty =
DependencyProperty.Register("Framerate", typeof(int), typeof(CLEyeCameraImage),
new UIPropertyMetadata(15, (PropertyChangedCallback)delegate(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
CLEyeCameraImage typedSender = sender as CLEyeCameraImage;
typedSender.Device.Framerate = (int)e.NewValue;
}));
public CLEyeCameraColorMode ColorMode
{
get { return (CLEyeCameraColorMode)GetValue(ColorModeProperty); }
set { SetValue(ColorModeProperty, value); }
}
public static readonly DependencyProperty ColorModeProperty =
DependencyProperty.Register("ColorMode", typeof(CLEyeCameraColorMode), typeof(CLEyeCameraImage),
new UIPropertyMetadata(default(CLEyeCameraColorMode), (PropertyChangedCallback)delegate(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
CLEyeCameraImage typedSender = sender as CLEyeCameraImage;
typedSender.Device.ColorMode = (CLEyeCameraColorMode)e.NewValue;
}));
public CLEyeCameraResolution Resolution
{
get { return (CLEyeCameraResolution)GetValue(ResolutionProperty); }
set { SetValue(ResolutionProperty, value); }
}
public static readonly DependencyProperty ResolutionProperty =
DependencyProperty.Register("Resolution", typeof(CLEyeCameraResolution), typeof(CLEyeCameraImage),
new UIPropertyMetadata(default(CLEyeCameraResolution), (PropertyChangedCallback)delegate(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
CLEyeCameraImage typedSender = sender as CLEyeCameraImage;
typedSender.Device.Resolution = (CLEyeCameraResolution)e.NewValue;
}));
#endregion
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This file is part of CL-EyeMulticam SDK
//
// WPF C# Sample Application
//
// It allows the use of multiple CL-Eye cameras in your own applications
//
// For updates and file downloads go to: http://codelaboratories.com/research/view/cl-eye-muticamera-sdk
//
// Copyright 2008-2010 (c) Code Laboratories, Inc. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Interop;
using CLEyeMulticam;
namespace CLEyeMulticamWPFTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
int numCameras = 0;
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
}
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (numCameras >= 1)
{
cameraImage1.Device.Stop();
cameraImage1.Device.Destroy();
}
if (numCameras == 2)
{
cameraImage2.Device.Stop();
cameraImage2.Device.Destroy();
}
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Query for number of connected cameras
numCameras = CLEyeCameraDevice.CameraCount;
if(numCameras == 0)
{
MessageBox.Show("Could not find any PS3Eye cameras!");
return;
}
output.Items.Add(string.Format("Found CLEyeCamera devices", numCameras));
// Show camera's UUIDs
for (int i = 0; i < numCameras; i++)
{
output.Items.Add(string.Format("CLEyeCamera # UUID: ", i + 1, CLEyeCameraDevice.CameraUUID(i)));
}
// Create cameras, set some parameters and start capture
if (numCameras >= 1)
{
cameraImage1.Device.Create(CLEyeCameraDevice.CameraUUID(0));
cameraImage1.Device.Zoom = -50;
cameraImage1.Device.Start();
}
if (numCameras == 2)
{
cameraImage2.Device.Create(CLEyeCameraDevice.CameraUUID(1));
cameraImage2.Device.Rotation = 200;
cameraImage2.Device.Start();
}
}
}
}