无法在C#中使用AForge.NET初始化相机预览

我对AForge.Net库感到好奇。 VideoCaptureDevice被初始化,订阅NewFrameEventHandlerStart()。在初始函数处,属性VideoCaptureDevice.IsRunning返回true。一段时间后,它返回false。我尝试从AForge下载示例程序,结果为

  

视频已停止

两台相机测试

无法在C#中使用AForge.NET初始化相机预览

简单播放器

无法在C#中使用AForge.NET初始化相机预览

下面是我的摘录。我正在倒数计时,以在相机启动后拍摄相框照片。

public VideoCaptureDevice Cam = null;
private async void TakePictureBtn_Click(object sender,RoutedEventArgs e)
{
    try
    {
        if (StartCamera())
        {
            _time = TimeSpan.FromSeconds(CountDown);
            ViewCount.Visibility = Visibility.Visible;
            TbxCount.Foreground = System.Windows.Media.brushes.Black;
            TbxCount.Text = "\tGet Ready\t";
            // IsRunning is true from here
            await Task.Delay(2000);
            TbxCount.Text = "";

            _timer = new DispatcherTimer(new TimeSpan(0,1),DispatcherPriority.Normal,delegate
            {
                // IsRunning become false
                if (Cam.IsRunning)
                {
                    TbxCount.Foreground = System.Windows.Media.brushes.White;
                    TbxCount.Text = _time.Seconds.ToString();
                    Storyboard sb = FindResource("Countdown") as Storyboard;
                    Storyboard.SetTarget(sb,ViewCount);
                    sb.Begin();
                    if (_time == TimeSpan.Zero)
                    {
                        ViewCount.Visibility = Visibility.Collapsed;
                        _timer.Stop();
                        StopCamera();
                        if (frameHolder.Source != null)
                        {
                            byte[] imgByte = ImageSourceToBytes(new JpegBitmapEncoder(),frameHolder.Source);
                            //Convert the Byte Array to Base64 Encoded string.
                            string base64String = Convert.ToBase64String(imgByte,imgByte.Length);
                            WriteLog.Log("Image Base64: " + base64String,isError: false);
                        }
                        else
                        {
                            WriteLog.Log("Image frame null",isError: false);
                        }
                    }
                }
                else
                {
                    frameHolder.Source = new BitmapImage(new Uri("pack://siteoforigin:,./source/def/webcam_err.jpg",UriKind.RelativeOrAbsolute));
                    frameHolder.Stretch = Stretch.Uniform;
                    frameHolder.Margin = new Thickness(0,100,100);
                    StopCamera();
                }
                _time = _time.Add(TimeSpan.FromSeconds(-1));
            },Application.Current.Dispatcher);
            _timer.Start();
        }
    }
    catch (Exception ex)
    {
        string error = "Error to take picture. Ex-" + ex.Message;
        WriteLog.Log(error,isError: true);
        StopCamera();
    }
}

private bool StartCamera()
{
    try
    {
        if (CurrentDevice != null)
        {
            CloseCurrentVideoSource();

            Cam = new VideoCaptureDevice(monikerString);
            Cam.NewFrame += new NewFrameEventHandler(video_NewFrame);
            Cam.Start();

            //_videoSource = new VideoCaptureDevice(monikerString);
            //_videoSource.NewFrame += video_NewFrame;
            //_videoSource.Start();
            return true;
        }
    }
    catch (Exception ex)
    {
        string error = "Error to start camera. Ex-" + ex.Message;
        WriteLog.Log(error,isError: true);
    }
    return false;
}
private void StopCamera()
{
    try
    {
        if (Cam != null && Cam.IsRunning)
        {
            Cam.Stop();
            Cam.NewFrame -= new NewFrameEventHandler(video_NewFrame);
            Cam = null;
        }
    }
    catch (Exception ex)
    {
        string error = "Error to stop camera. Ex-" + ex.Message;
        WriteLog.Log(error,isError: true);
    }
}

我尝试重新启动Visual Studio,甚至重新启动PC。但是我的结果还是一样。

让我烦恼的是,Windows 10上的摄像头应用程序既可以显示笔记本电脑上的内置网络摄像头,又可以毫无问题地切换到Logitech便携式网络摄像头。

HP HD摄像头

无法在C#中使用AForge.NET初始化相机预览

Logitech

无法在C#中使用AForge.NET初始化相机预览

为什么会发生这种情况以及如何遇到这种情况?

TYCandy 回答:无法在C#中使用AForge.NET初始化相机预览

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3118389.html

大家都在问