使用摄像机的闪光灯

凯文·K

我在互联网上找到了一个教程(http://www.locked.nl),用于永久打开闪光灯。在StartRecording()方法中,两次调用该方法时出现异常。另外,如何停止录制?太感谢了

我有这两个课程。

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

}

}

保罗·扎赫拉

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章