Coding Zen

my .NET coding endeavors and more

Need to convert a System.Drawing.Bitmap to BitmapImage? Try this helper out

Facebook
RSS

It’s been a loooong time between posts and I apologize for that. The resent months have been quite busy and a somewhat recent change in direction on my software development has led to me no longer diving into WIA.

In this post I am providing a functional class that provides a means to enumerate the devices properties, set a device property and take a picture. What this class does not include is a function for downloading the device image. This class is meant to be a simple basis from which you can build upon. The code is offered as is.

public class WIACamera {
private Dictionary _properties = new Dictionary();
private Device _cam = null;

///
/// Retrieves the camera's properties available throug WIA
///
private void GetProperties() {
InitializePropertiesTables();
foreach (WIA.Property p in _cam.Properties) {
_properties.Add(p.Name, p);
}
}
///
/// Gets the value of the passed property name.
///
///
Name/Key of the property to get the value of. /// Property's value as a string.
private string GetPropertyValue(string propertyName) {
try {
if (_cam != null && _properties != null && _properties[propertyName] != null) {
WIA.Property p = (WIA.Property)_properties[propertyName];
return p.get_Value().ToString();
}
} catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); }
return "";
}
///
/// Sets the passed WIA property to the passed value.
///
/// The name of the WIA property to change.
/// The new value of the reference WIA property.
public void SetPropertyValue(string propertyName, int value) {
// try to set focus length
Object Object1 = null;
Object Object2 = null;
// get property
try {
if (_cam != null && _properties != null && _properties[propertyName] != null) {
WIA.Property p = (WIA.Property)_properties[propertyName];
Object1 = (Object)p.PropertyID.ToString();
Object2 = (Object)value;

Property prop = _cam.Properties.get_Item(ref Object1);

prop.set_Value(ref Object2);
}

} catch (Exception ex) {System.Windows.Forms.MessageBox.Show(ex.Message);}
}
///
/// Takes a single picture with camera device.
///
public void TakePicture() {
if (_cam != null) {
_prevItemsCnt = _cam.Items.Count;
try {
// take picture
_cam.ExecuteCommand(WIA.CommandID.wiaCommandTakePicture);

} catch (Exception ex) {Debug.WriteLine("Error has occurred! " + ex.Message);}
}
}

///
/// Populates lookup tables for properties needing them.
///
private void InitializePropertiesTables() {

#region Shutter table

_dicShutterTable.Add(1, "6400");
_dicShutterTable.Add(2, "4000");
_dicShutterTable.Add(3, "3200");
_dicShutterTable.Add(4, "2500");
_dicShutterTable.Add(5, "2000");
_dicShutterTable.Add(6, "1600");
_dicShutterTable.Add(8, "1250");
_dicShutterTable.Add(10, "1000");
_dicShutterTable.Add(12, "800");
_dicShutterTable.Add(15, "640");
_dicShutterTable.Add(20, "500");
_dicShutterTable.Add(25, "400");
_dicShutterTable.Add(31, "320");
_dicShutterTable.Add(40, "250");
_dicShutterTable.Add(50, "200");
_dicShutterTable.Add(62, "160");
_dicShutterTable.Add(80, "125");
_dicShutterTable.Add(100, "100");
_dicShutterTable.Add(125, "80");
_dicShutterTable.Add(166, "60");
_dicShutterTable.Add(200, "50");
_dicShutterTable.Add(250, "40");
_dicShutterTable.Add(333, "30");
_dicShutterTable.Add(400, "25");
_dicShutterTable.Add(500, "20");
_dicShutterTable.Add(666, "15");
_dicShutterTable.Add(769, "13");
_dicShutterTable.Add(1000, "10");
_dicShutterTable.Add(1250, "8");
_dicShutterTable.Add(1666, "6");
_dicShutterTable.Add(2000, "5");
_dicShutterTable.Add(2500, "4");
_dicShutterTable.Add(3333, "3");
_dicShutterTable.Add(4000, "2.5");
_dicShutterTable.Add(5000, "2");
_dicShutterTable.Add(6250, "1.6");
_dicShutterTable.Add(7692, "1.3");
_dicShutterTable.Add(10000, "1s");
_dicShutterTable.Add(13000, "1.3s");
_dicShutterTable.Add(16000, "1.6s");
_dicShutterTable.Add(20000, "2s");
_dicShutterTable.Add(25000, "2.5s");
_dicShutterTable.Add(30000, "3s");
_dicShutterTable.Add(40000, "4s");
_dicShutterTable.Add(50000, "5s");
_dicShutterTable.Add(60000, "6s");
_dicShutterTable.Add(80000, "8s");
_dicShutterTable.Add(100000, "10s");
_dicShutterTable.Add(130000, "13s");
_dicShutterTable.Add(150000, "15s");
_dicShutterTable.Add(200000, "20s");
_dicShutterTable.Add(250000, "25s");
_dicShutterTable.Add(300000, "30s");

#endregion

#region White balance table

_dicWBTable.Add(2, "Auto");
_dicWBTable.Add(4, "Daylight");
_dicWBTable.Add(5, "Fluorescent ");
_dicWBTable.Add(6, "Incandescent");
_dicWBTable.Add(7, "Flash");
_dicWBTable.Add(32784, "Cloudy");
_dicWBTable.Add(32785, "Shade");
_dicWBTable.Add(32786, "Kelvin");
_dicWBTable.Add(32787, "Custom");

#endregion

#region Exposure mode table

_dicExposureMode.Add(1,"M");
_dicExposureMode.Add(2,"P");
_dicExposureMode.Add(3,"A");
_dicExposureMode.Add(4,"S");

#endregion

}
}

Related Posts:

  • No Related Posts
  • http://2006torino.blogspot.com/ maborg

    what are the undeclared variables ? _dicWBTable ?

    • Anonymous

      maborg, _dicWBTable is of type Dictionary

    • Anonymous

      It is

      Dictionary

Kinect for Windows i

[caption id="attachment_264" align="aligncenter" width="600" caption="Kinect for Windows Hardware buy yours ...

Kinect for Windows b

If you have been keeping up with the Kinect SDK ...

C# System.Drawing Im

I was recently presented with an issue of casting a ...

Kinect App Aids in T

I’ve been playing with the Microsoft Kinect and SDK at ...

Get those Camera Raw

If you are like me you have been long frustrated ...

Better Tag Cloud