| ||
|
The Camera Wrapper for .NET CF control from Bistia can really improve the use of a digital camera in your applications. The control provides a live video (preview) stream in the control window that can be manipulated in various ways (different resolutions, color control, white balance, exposure, digital zoom, and so on). The camera wrapper supports three LifeView cameras and the cool SDIO-based HP Photosmart Mobile Camera. The control has a comprehensive interface with more than 40 attributes and methods respectively, close to 60 events, and over 100 protected members. The most interesting method is of course the SaveJpegToFile that obviously saves the image currently in the control frame as a JPEG file. Before the save is done, many options can be set like JPEG compression rate and resolution. The resolution of the control can be set to 320x240, 640x480, 1280x960 (only HP camera), and 1280x1024. For more info, please see the full set of features for each camera. Customer Feedback Sample Building on the samples that come with the camera wrapper, the sample Customer Feedback shows how to use the control in your applications. The sample application is built for .NET CF with Visual Studio .NET 2003 and starts up with the following form: ![]() Customer Feedback Sample The business scenario is a courier that is about to drop off a package at a destination site. The receiving part, the contact person at the customer (provided in the "Name:" field), is given the opportunity to provide comments about the delivery (in the "Comments:" field). Further, there is a possibility to take pictures of the delivered package if there is a problem. These pictures will provide valuable information for the back office handling a possible complaint, and for valuable goods they might also be used by the insurance company. The larger frame is a live video frame (capture frame), and when the "Capture!" menu option is selected, the picture frames on the right are filled in order starting from the top and going down. A maximum of five pictures can be captured, and when the last frame is filled, the next capture will start over from the top. ![]() Settings Menu The "Settings" menu provide the ability to change options of the capture frame such as auto exposure metering, color, white balance, and JPEG image capture quality. There are many more options available for the camera wrapper that could be implemented to further enhance the sample. The captured pictures are saved to the file system, but might as well be stored in a database or sent off to a server. Let's see how to use the camera control by having a closer look at the sample code. Code Walkthrough Most of the work when developing the application actually went into designing the form. As many of the properties of the camera wrapper are available in the designer, there is not much need for programming the control. However, if you want to leave some settings to the user of your application, a settings menu can be provided, but let's get back to that in a little while. The first thing that happens when the application starts (in the constructor), is that the camera wrapper control starts its preview mode: hpCameraWrapper.StartPreview();This will bring the live video stream from the camera into the control on the form. Logically, the preview is stopped with the StopPreview() method. As mentioned, all properties of the control can be set programmatically, and the code for the color settings submenu items looks like this:
private void colorFullMenuItem_Click(object sender,
System.EventArgs e)
{
hpCameraWrapper.Color =
Bistia.CF.Tools.Multimedia.Color.FullColor;
uncheckSubMenus(colorMenuItem);
colorFullMenuItem.Checked = true;
}
private void colorBlackWhiteMenuItem_Click(object sender,
System.EventArgs e)
{
hpCameraWrapper.Color =
Bistia.CF.Tools.Multimedia.Color.BlackAndWhite;
uncheckSubMenus(colorMenuItem);
colorBlackWhiteMenuItem.Checked = true;
}
private void colorNegativeMenuItem_Click(object sender,
System.EventArgs e)
{
hpCameraWrapper.Color =
Bistia.CF.Tools.Multimedia.Color.Negative;
uncheckSubMenus(colorMenuItem);
colorNegativeMenuItem.Checked = true;
}
Note that each color setting is available in a predefined enumeration, and as are many of
the other property values. The helper method to uncheck the submenu items looks like this:
private void uncheckSubMenus(MenuItem parentMenuItem)
{
foreach(MenuItem childMenuItem in parentMenuItem.MenuItems)
childMenuItem.Checked = false;
}
This method is probably useful in many projects. Now, let's look at the core of the sample
functionality – the capture of images from the camera wrapper control. To keep control of
each capture, a private class member is declared:
private int currentCapture = 0;This variable is used when the "Capture!" menu option is tapped:
PictureBox capturePictureBox = null;
try
{
Cursor.Current = Cursors.WaitCursor;
hpCameraWrapper.SaveJpegToFile(getApplicationPath() + @"\capture" + currentCapture.ToString() + ".jpg", true);
switch(this.currentCapture)
{
case 0: capturePictureBox = capture0PictureBox; break;
case 1: capturePictureBox = capture1PictureBox; break;
case 2: capturePictureBox = capture2PictureBox; break;
case 3: capturePictureBox = capture3PictureBox; break;
case 4: capturePictureBox = capture4PictureBox; break;
}
capturePictureBox.Image = new Bitmap(getApplicationPath() +
@"\capture" + currentCapture.ToString() + ".jpg");
if(++currentCapture > 4)
currentCapture = 0;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Cursor.Current = Cursors.Default;
}
The SaveJpegToFile method is used to save the image currently visible in the control's live
video frame as a JPEG file. The last parameter (set to true) indicates that the preview mode
should be resumed after the image is saved. The private variable (currentCapture) keep track
of which PictureBox should receive the captured image and when PictureBox is selected in a
local variable (capturePictureBox), the image is retrieved from the file system into the
PictureBox's Image property. The following private method is used to get the folder of the
current running application:
private string getApplicationPath()
{
return Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly(
).GetName().CodeBase.ToString());
}
Reflection is used to get the current executing assembly, its name, and the path.
For a more complete example, see the Customer Feedback source code and the samples that comes with the control. Naming Private Member With regards to the code samples above, a comment on the naming conventions for private members used is in place as this is a good practice. The (new) rule is that all private members (variables and methods) use camelCasing instead of the .NET tradition (!) to use PascalCasing at least for methods. This makes it possible, in a case sensitive language like C#, to have a private variable or method published as a public property or method with the same name – only different casing. This also applies to control naming. The main rule is to suffix all object instances with the class name, and therefore the first label is named nameLabel and its text box nameTextBox. This will automatically generate event names with camelCasing according to the rule described above. Note, however, that form events are still generated using PascalCasing as this is derived from the form class name (e.g. MainForm). For instances that will only occur once, it's a good practice to use the same name as the class but instead using camelCasing (e.g. mainMenu and inputPanel). An exception to this rule is the use of abbreviations for common objects like a simple "i" for an "int", "s" for a "string", "ds" for a "DataSet", "dr" for a "DataRow", "con" for a "SqlConnection", and so on. Conclusion The use of a ready-made control can save a lot of effort in utilizing a Pocket PC digital camera as the complexity is hidden from the developer that can focus more on solving the business issues. The advice is clearly to download the evaluation to take a closer look at this article's sample and also the samples that comes with the control. Any comments? |
||||||||||||||||||||||||||
| ©2001-2009 Christian Forsberg & Andreas Sjöström |
||