闪光灯使用摄像机

时间:2013-09-10 10:23:45

标签: c# windows-phone-7

我在互联网上找到了一个教程(http://www.locked.nl)来永久打开闪光灯。在StartRecording()方法中,我在调用Method两次时遇到异常。另外,我怎么能停止录音?非常感谢你

我有这两个班。

VideoCamera类:

public class VideoCamera
{
    private object _videoCamera;
    private PropertyInfo _videoCameraLampEnabledPropertyInfo;
    private MethodInfo _videoCameraStartRecordingMethod;
    private MethodInfo _videoCameraStopRecordingMethod;
    private EventHandler _videoCameraInitialized;

    public object InnerCameraObject
    {
        get { return _videoCamera; }
    }

    public bool LampEnabled
    {
        get { return (bool)_videoCameraLampEnabledPropertyInfo.GetGetMethod().Invoke(_videoCamera, new object[0]); }
        set { _videoCameraLampEnabledPropertyInfo.GetSetMethod().Invoke(_videoCamera, new object[] { value }); }
    }

    public VideoCamera()
    {
        // Load the media extended assembly which contains the extended video camera object.
        Assembly mediaExtendedAssembly = Assembly.Load("Microsoft.Phone.Media.Extended, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e");

        // Get the camera source type (primary camera).
        Type cameraSourceType = mediaExtendedAssembly.GetType("Microsoft.Phone.CameraSource");
        FieldInfo field = cameraSourceType.GetField("PrimaryCamera");
        object cameraSource = Enum.ToObject(cameraSourceType, (int)field.GetValue(cameraSourceType));

        // Create the video camera object.
        Type videoCameraType = mediaExtendedAssembly.GetType("Microsoft.Phone.VideoCamera");
        ConstructorInfo videoCameraConstructor = videoCameraType.GetConstructor(new Type[] { cameraSourceType });
        _videoCamera = videoCameraConstructor.Invoke(new object[] { cameraSource });

        // Set the properties and methods.
        _videoCameraLampEnabledPropertyInfo = videoCameraType.GetProperty("LampEnabled");
        _videoCameraStartRecordingMethod = videoCameraType.GetMethod("StartRecording");
        _videoCameraStopRecordingMethod = videoCameraType.GetMethod("StopRecording");

        // Let the initialize event bubble through.
        _videoCameraInitialized = new EventHandler(VideoCamera_Initialized);
        MethodInfo addInitializedEventMethodInfo = videoCameraType.GetMethod("add_Initialized");
        addInitializedEventMethodInfo.Invoke(_videoCamera, new object[] { _videoCameraInitialized });
    }

    /// <summary>
    /// Occurs when the camera object has been initialized.
    /// </summary>
    public event EventHandler Initialized;

    /// <summary>
    /// Videoes the camera_ initialized.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="eventArgs">The event args.</param>
    private void VideoCamera_Initialized(object sender, object eventArgs)
    {
        if (Initialized != null)
        {
            Initialized.Invoke(this, new EventArgs());
        }
    }
    public bool IsRecording { get; private set; }
    /// <summary>
    /// Start recording.
    /// </summary>
    public void StartRecording()
    {
         _videoCameraStartRecordingMethod.Invoke(_videoCamera, new object[0]);
    }

    public void StopRecording()
    {

    }
}

}

和这个类VideoCameraVisualizer

public class VideoCameraVisualizer : UserControl
{
    private object _cameraVisualizer;
    private MethodInfo _cameraVisualizerSetSourceMethod;

    public VideoCameraVisualizer()
    {
        // Load the media extended assembly which contains the extended video camera object.
        Assembly mediaExtendedAssembly = Assembly.Load("Microsoft.Phone.Media.Extended, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e");

        // Get the camera source type (primary camera).
        Type cameraVisualizerType = mediaExtendedAssembly.GetType("Microsoft.Phone.CameraVisualizer");
        ConstructorInfo cameraVisualizerConstructor = cameraVisualizerType.GetConstructor(Type.EmptyTypes);
        _cameraVisualizer = cameraVisualizerConstructor.Invoke(null);

        // Set the properties and methods.
        _cameraVisualizerSetSourceMethod = cameraVisualizerType.GetMethod("SetSource");
    }

    public void SetSource(VideoCamera camera)
    {
        // Invoke the set source method on the camera visualizer object.
        _cameraVisualizerSetSourceMethod.Invoke(_cameraVisualizer, new object[] { camera.InnerCameraObject });
    }
}

}

现在在MainPage中我有这个代码出错了。

    private void ContentPanel_Loaded(object sender, RoutedEventArgs e)
    {
        // Check to see if the camera is available on the device.
        if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
        {
            // Use standard camera on back of device.
            videoCam = new VideoCamera();

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

            // Add the photo camera to the video source
            CameraVisualizer = new VideoCameraVisualizer();
            CameraVisualizer.SetSource(videoCam);
        }
        else MessageBox.Show("No Flash supported for Your Device. Try Color Button");
    }

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



    private void FlashButton_Click(object sender, RoutedEventArgs e)
    {
        if (count % 2 == 0)
        {

            videoCam.LampEnabled = true;
            videoCam.StartRecording();
        }
        else
        {

            videoCam.LampEnabled = false;
            videoCam.StopRecording();
        }
        count++;
    }

}

}

1 个答案:

答案 0 :(得分:0)