Wednesday, June 09, 2010
DataObject method accessible with instance and without
Below explains how to create a single chunk of method logic in a class that is accessible from both an instance of that class and without any instance of the class.
1. Create some data object class, perhaps called Theme.
2. Create method to copy from Model into both instance and static
1. Create some data object class, perhaps called Theme.
2. Create method to copy from Model into both instance and static
private static Theme toStatic(Model.Theme fromModel)3. Create methods to preform the function both in an instance and without/statically.
{
Theme theme = new Theme();
theme.ID = fromModel.ID;
return theme
}
private Theme toInstance(Model.Theme fromModel)
{
this.ID = fromModel.ID;
return null;
}
public static Theme Get(int id)4. create delegate to call ToStatic or ToInstance
{
return toStatic(WebService.GetTheme(id));
}
public Theme(int id)
{
toInstance(WebService.GetTheme(id));
}
private delegate Theme staticOrInstance(Model.Theme theme);5. create method to preform the get action once
private static Theme Get(staticOrInstance statOrInst, int id)6. change methods above to use delegate to send to single Get
{ return statOrInst(WebService.GetTheme(id)); }
public static Theme Get(int id)7. This allows the dataobject to be used as.
{
return Get(toStatic, id);
}
public Theme(int id)
{
Get(toInstance, id);
}
string name = Theme.Get(4).Name;to use the same underlying code.
//or
Theme newTheme = new Theme(4);
Tuesday, June 08, 2010
ensure Silverlight webcam is capturing
I had some trouble with working with a logitech C250 webcam. Silverlight detects the webcam just fine. However if the webcam has already been utilized by another program the webcam captures only blank images. Detecting this problem is problematic because: the webcam state is started, the videocapturedevice is not null, and the capture failed event is not fired. It bombs when trying to make a call to CaptureImageAsync() that never completes.
I found the best way to detect this is by examining the image that is being captured. The problem is the raw image can't be easily viewed using the CaptureSource object nor the VideoBrush. It can only be easily tested after it is painted to a frameworkElement, such as a Rectangle.
To do this test create an XAML element that the webcam will paint to, such as
Then wire a button up to run the following.
I found the best way to detect this is by examining the image that is being captured. The problem is the raw image can't be easily viewed using the CaptureSource object nor the VideoBrush. It can only be easily tested after it is painted to a frameworkElement, such as a Rectangle.
To do this test create an XAML element that the webcam will paint to, such as
<Rectangle x:Name="rect" Width="200" Height="200" />
Then wire a button up to run the following.
private void btn_Click(object sender, RoutedEventArgs e)
{ if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
{
System.Windows.Media.CaptureSource webCam = new CaptureSource();
webCam.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
webCam.Start();
VideoBrush camBrush = new VideoBrush();
camBrush.SetSource(webCam);
this.rect.Fill = camBrush;
if ((new System.Windows.Media.Imaging.WriteableBitmap(rect, null))
.Pixels.Average() != 0);// then we're getting a picture
//an average of 0 means no image is actually being created on the rectangle.
}
}
Labels: detect blank webcam, Silverlight, webcam
Subscribe to Posts [Atom]