0

我正在制作一个手电筒应用程序,其中我需要在按下 ON 按钮时不断使用相机的 LED 并在按下同一个按钮时将其关闭。我按照这篇文章使用 Reflection 使用摄像机打开 LED。ON/OFF 操作只能正常工作一次。代码如下:

 private VideoCamera _videoCamera;
 private VideoCameraVisualizer _videoCameraVisualizer;
 bool _isFlashOff = true;

 private void FlashButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (_isFlashOff)
            {
                _isFlashOff = false;

                // Check to see if the camera is available on the device.
                if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
                {
                   // Use standard camera on back of device.
                    _videoCamera = new VideoCamera();
                   // Event is fired when the video camera object has been initialized.
                    _videoCamera.Initialized += VideoCamera_Initialized;

                    // Add the photo camera to the video source
                    _videoCameraVisualizer = new VideoCameraVisualizer();
                    _videoCameraVisualizer.SetSource(_videoCamera);
                }
            }
            else
            {                    
                _isFlashOff = true;
                _videoCamera.StopRecording();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void VideoCamera_Initialized(object sender, EventArgs e)
    {
        _videoCamera.LampEnabled = true;
        _videoCamera.StartRecording();
    }

由于没有按照文章中指定的 VideoCamera 类中的StopRecording方法实现:使用 Reflection 使用摄像机打开 LED。我将函数设为:

 public void StopRecording()
    {
        // Invoke the stop recording method on the video camera object.
         _videoCameraStopRecordingMethod.Invoke(_videoCamera, null);
    }

问题是当我再次按下 ON 按钮时,“异常”被抛出为“TargetInvocationException”。我无法找出导致异常的问题。StopRecording() 函数是否正确..??

4

2 回答 2

2

那是因为您应该只初始化一次相机。在OnNavigatedTo活动期间执行此操作,然后重新使用相同的实例:

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {                    
        // Use standard camera on back of device.
        _videoCamera = new VideoCamera();

        // Event is fired when the video camera object has been initialized.
        _videoCamera.Initialized += VideoCamera_Initialized;


        // Add the photo camera to the video source
        _videoCameraVisualizer = new VideoCameraVisualizer();
        _videoCameraVisualizer.SetSource(_videoCamera);
    }

    private void VideoCamera_Initialized(object sender, EventArgs e)
    {
        isInitialized = true;
    }

    bool isInitialized;
    bool isFlashEnabled;

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        if (!isInitialized)
        {
            MessageBox.Show("Please wait during camera initialization");
            return;
        }

        if (!isFlashEnabled)
        {
            isFlashEnabled = true;

            // Check to see if the camera is available on the device.
            if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
            {
                _videoCamera.LampEnabled = true;
                _videoCamera.StartRecording();
            }
        }
        else
        {
            isFlashEnabled = false;

            _videoCamera.StopRecording();
        }
    }
于 2012-09-02T10:28:19.687 回答
0

试试这个:

http://msdn.microsoft.com/en-us/library/hh202949.aspx

于 2012-08-31T20:28:50.497 回答