Restarting Camera.StartStreamingBitmaps after Camera.PictureCaptured

681 views Asked by At

I have a .Net Gadgeteer camera app that streams bitmaps from the camera to a screen. On the press of a button it stops streaming the bitmaps from the camera, takes a picture, writes it to SD card, and then restarts the streaming. Here's a simplified version of the code (without the saving to SD card stuff):

using Microsoft.SPOT;
using GT = Gadgeteer;
using Gadgeteer.Modules.GHIElectronics;
namespace SimpleStopStreamingApp
{
    public partial class Program
    {
        void ProgramStarted()
        {
            camera.CameraConnected += new Camera.CameraConnectedEventHandler(camera_CameraConnected);
            camera.BitmapStreamed += new Camera.BitmapStreamedEventHandler(camera_BitmapStreamed);
            camera.PictureCaptured += new Camera.PictureCapturedEventHandler(camera_PictureCaptured);
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
        }
        void camera_CameraConnected(Camera sender)
        {
            camera.StartStreamingBitmaps(new Bitmap(camera.CurrentPictureResolution.Width, camera.CurrentPictureResolution.Height));
        }
        void camera_BitmapStreamed(Camera sender, Bitmap bitmap)
        {
            display_T35.SimpleGraphics.DisplayImage(bitmap, 0, 0);
        }
        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            camera.StopStreamingBitmaps();
            camera.TakePicture();
        }
        void camera_PictureCaptured(Camera sender, GT.Picture picture)
        {
            camera.StartStreamingBitmaps(new Bitmap(camera.CurrentPictureResolution.Width, camera.CurrentPictureResolution.Height));
        }
    }
}

However when I try to restart the streaming (in the PictureCaptured event handler) I get the exception.

#### Exception System.Exception - 0xffffffff (1) ####
#### Message: 
#### GHI.Premium.USBHost.USBH_Webcam::StartStreaming_Internal [IP: 0000] ####
#### GHI.Premium.USBHost.USBH_Webcam::StartStreaming [IP: 0005] ####
#### Gadgeteer.Modules.GHIElectronics.Camera::StartStreamingBitmaps [IP: 007e] ####
#### Gadgeteer.Modules.GHIElectronics.Camera::OnPictureCapturedEvent [IP: 0037] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####

A first chance exception of type 'System.Exception' occurred in GHI.Premium.USBHost.dll Error invoking method "Gadgeteer.Modules.GHIElectronics.Camera" (check arguments to Program.BeginInvoke are correct)

(N.B. I get this same exception if I store the bitmap as an instance variable and pass it in to both calls to StartStreamingBitmaps instead of creating a new Bitmap each time.)

What's going on? How should one stop the camera streaming to capture and image and then restart it streaming?

2

There are 2 answers

1
AudioBubble On

It almost sounds like StartStreamingBitmaps is expected to run on a thread (by the note to check arguments to Program.BeginInvoke are correct).

Looking at this:

void camera_PictureCaptured(Camera sender, GT.Picture picture)
{
  camera.StartStreamingBitmaps(new Bitmap(camera.CurrentPictureResolution.Width, camera.CurrentPictureResolution.Height));
}

I don't know anything about this Gadgeteer module, but if you wired it up correctly, I'd think your new GT.Picture is already contained in the picture parameter that was passed in.

It almost looks like you are trying to stream images from the camera after the camera has finished taking the picture.

Can you edit your post and give more details? Can you verify that this camera_PictureCaptured event is where your exception is being thrown? It'd be easy enough to do by modifying your event handler to be:

void camera_PictureCaptured(Camera sender, GT.Picture picture)
{
  try
  {
    camera.StartStreamingBitmaps(new Bitmap(camera.CurrentPictureResolution.Width, camera.CurrentPictureResolution.Height));
  } catch (Exception err)
  {
    throw new Exception("PictureCaptured: " + err.Message);
  }
}

If this event handler is not the one throwing the exception, try placing similar try...catch routines in your other event handlers until you discover which one is really causing the problem.

0
Benjy Kessler On

Try removing the callback when you stop streaming the bitmaps:

void button_ButtonPressed(Button sender, Button.ButtonState state)
{
    camera.BitmapStreamed -= new Camera.BitmapStreamedEventHandler(camera_BitmapStreamed);
    camera.StopStreamingBitmaps();
    camera.TakePicture();
}

void camera_PictureCaptured(Camera sender, GT.Picture picture)
{
    camera.BitmapStreamed += new Camera.BitmapStreamedEventHandler(camera_BitmapStreamed);
    camera.StartStreamingBitmaps(new Bitmap(camera.CurrentPictureResolution.Width, camera.CurrentPictureResolution.Height));
}