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
<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: , ,


Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]