2

我有一个 .Net Gadgeteer 相机应用程序,可将位图从相机流式传输到屏幕。按一下按钮,它会停止从相机流式传输位图,拍照,将其写入 SD 卡,然后重新启动流式传输。这是代码的简化版本(没有保存到 SD 卡的东西):

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));
        }
    }
}

但是,当我尝试重新启动流式传输(在 PictureCaptured 事件处理程序中)时,我得到了异常。

#### 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)

(注意,如果我将位图存储为实例变量并将其传递给对 StartStreamingBitmaps 的两个调用,而不是每次都创建一个新的位图,我会得到同样的异常。)

这是怎么回事?应该如何停止相机流式传输以捕获和图像,然后重新启动它流式传输?

4

2 回答 2

1

这几乎听起来像是StartStreamingBitmaps预计将在一个线程上运行(由注释到check arguments to Program.BeginInvoke are correct)。

看着这个:

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

我对这个Gadgeteer模块一无所知,但如果你正确连接它,我认为你的 newGT.Picture已经包含在picture传入的参数中。

在相机完成拍摄后,您几乎似乎正在尝试从相机流式传输图像。

您可以编辑您的帖子并提供更多详细信息吗?你能验证这个camera_PictureCaptured事件是你的异常被抛出的地方吗?通过将事件处理程序修改为:

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);
  }
}

如果这个事件处理程序不是引发异常的那个,请尝试try...catch在您的其他事件处理程序中放置类似的例程,直到您发现真正导致问题的事件处理程序。

于 2013-01-24T14:20:18.497 回答
0

停止流式传输位图时尝试删除回调:

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));
}
于 2013-06-12T21:11:03.240 回答