VS+C#+WPF 다중 스레드 비디오 카메라 플레이어 모니터링

주요 프로그램 예

C#+WPF 다중 스레드 비디오 카메라 플레이어 모니터링

운영 환경을 설치하거나 원격 디버깅이 필요한 경우 기사 하단의 개인 QQ 명함을 참조하면 전문 기술 인력이 원격으로 지원합니다!

머리말

이 블로그는 <<C#+WPF 다중 스레드 비디오 카메라 플레이어 모니터링>>을 위한 코드를 작성합니다. 코드는 깔끔하고 규칙적이며 읽기 쉽습니다. 학습 및 응용 추천을 위한 첫 번째 선택.

왜 멀티스레딩을 사용해야 할까요?멀티스레딩은 멈춤 현상, 블랙스크린, 블루스크린, 화면흐림 등을 방지하기 위한 원격 영상 캡쳐 및 재생의 주요 알고리즘 기능입니다. 재생은 채택되어야 하는 방법입니다.


기사 디렉토리

1. 필수 도구 소프트웨어

2. 사용 단계

        1. 라이브러리 가져오기

        2. 코드 구현

        3. 러닝 결과

3. 온라인 지원

1. 필수 도구 소프트웨어

1. VS,WPF

2. 오픈CV

2. 사용 단계

1. 라이브러리 가져오기

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Threading;
using System.Timers;

using System.ComponentModel;

2. 코드 구현

아래와 같이 코드 쇼:

public class Thread1Class : System.Windows.Window
{
    // 第一个线程要执行的代码
    //多线程1
    //opencv变量1
    public Mat frameMatT1;

    private volatile bool stopFlag = false;
    private ManualResetEvent pauseSignal = new ManualResetEvent(true);

    

    public void Thread1Function()
    {
        try
        {
            // 在这里编写第一个线程要执行的代码
            Console.WriteLine("capture0");
            // 在这里编写要在UI线程上执行的操作
            VideoCapture capture1 = new VideoCapture();
            capture1.Open("aa.mp4", VideoCaptureAPIs.ANY);
            Console.WriteLine("capture00");
            if (capture1.IsOpened())
            {
                Console.WriteLine("capture1");
                // 循环读取视频帧并在图像控件中显示
                while (!stopFlag)
                {
                    Mat frameMat1 = capture1.RetrieveMat();
                    if (!frameMat1.Empty())
                    {
                        //Console.WriteLine("hello111222");

                    }

                    frameMatT1 = new Mat();
                    frameMat1.CopyTo(frameMatT1);

                    // 暂停线程
                    pauseSignal.WaitOne();
                }
                // 释放资源
                capture1.Release();
            }

            
        }
        catch (ThreadAbortException ex)
        {
            // 线程已被终止
        }
    }

}

public class Thread2Class : System.Windows.Window
{
    // 第二个线程要执行的代码
    public void Thread2Function()
    {
        // 在这里编写第二个线程要执行的代码
        Console.WriteLine("Thread2Function");
    }
}



namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    /// 

    public partial class MainWindow : System.Windows.Window
    {

        private Thread1Class thread1Object; //线程1定义
        private Thread2Class thread2Object;  //线程2定义

        private DispatcherTimer _timer11; //计时器定义
        private int _count = 0;     // 计时器已经运行的秒数
        private bool _isPaused = false;    // 标识计时器是否处于暂停状态



        public MainWindow()
        {
            InitializeComponent();

            timer11Init11();

        }

        private void timer11Init11()
        {
            // 创建一个新的计时器对象
            _timer11 = new DispatcherTimer();
            // 设置计时器间隔时间为1秒
            _timer11.Interval = TimeSpan.FromSeconds(0.2);
            // 订阅Tick事件,并将其处理程序设置为我们想要自动执行的函数
            _timer11.Tick += new EventHandler(video_display11);
        }


        private void timerStart11(object sender, RoutedEventArgs e)
        {
            try
            {
                string buttonText = userPC.Content.ToString();
                if (buttonText == "开始")
                {
                    // 启动计时器
                    _timer11.Start();
                    Console.WriteLine("_timerStart");
                    userPC.Content = "暂停";
                }

                if (buttonText == "暂停")
                {
                    // 如果计时器正在运行,则暂停计时器
                    if (_timer11.IsEnabled)
                    {
                        _timer11.Stop();
                        _isPaused = true;
                    }
                    userPC.Content = "恢复";
                }

                if (buttonText == "恢复")
                {
                    // 如果计时器处于暂停状态,则恢复计时器
                    if (_isPaused)
                    {
                        _timer11.Start();
                        _isPaused = false;
                    }
                    userPC.Content = "暂停";
                }
            }
            catch (ThreadAbortException ex)
            {
                // 线程已被终止
            }

        }

        private void myPauseButton_Click(object sender, RoutedEventArgs e)
        {
            // 如果计时器正在运行,则暂停计时器
            if (_timer11.IsEnabled)
            {
                _timer11.Stop();
                _isPaused = true;
            }
        }

        private void myResumeButton_Click(object sender, RoutedEventArgs e)
        {
            // 如果计时器处于暂停状态,则恢复计时器
            if (_isPaused)
            {
                _timer11.Start();
                _isPaused = false;
            }
        }


        private void myStopButton_Click(object sender, RoutedEventArgs e)
        {
            // 停止计时器并重置计时器已运行的秒数
            _timer11.Stop();
            _count = 0;
            timerLabel11.Content = "0";
        }


        //链接计时器
        private void video_display11(object sender, EventArgs e)
        {
            try
            {
                Console.WriteLine("video_display");
                if (thread1Object != null)
                {
                    if (thread1Object.frameMatT1 != null)
                    {
                        Console.WriteLine("video_display0");
                        if (value != null && value.Rows > 0 && value.Cols > 0)
                        {

                        }
                        else
                        {
                            Console.WriteLine("video_close ");
                        }
                    }
                }
                _count++;
                timerLabel11.Content = _count.ToString();
            }
            catch (ThreadAbortException ex)
            {
                // 线程已被终止
            }
        }


        private void videoModel_init11(object sender, RoutedEventArgs e)
        {
            try
            {
                string buttonText = model_init11.Content.ToString();
                if (buttonText == "模型初始化")
                {
                    thread1Object = new Thread1Class();
                    thread1Object.RunThread();
                    Console.WriteLine("RunThread");

                    model_init11.Content = "暂停模型";
                }

                if (buttonText == "暂停模型")
                {
                    model_init11.Content = "恢复模型";
                    thread1Object.PauseThread();
                    
                }

                if (buttonText == "恢复模型")
                {
                    model_init11.Content = "暂停模型";
                    thread1Object.ResumeThread();
                    
                }
            }
            catch (ThreadAbortException ex)
            {
                // 线程已被终止
            }
        }

        private void videoModel_Stop11(object sender, RoutedEventArgs e)
        {
            try
            {
                thread1Object.StopThread();
            }
            catch (ThreadAbortException ex)
            {
                // 线程已被终止
            }
        }

    } //class类结束
}  //namespace结束


3. 러닝 결과

 

3. 온라인 지원:

운영 환경을 설치하거나 원격 디버깅이 필요한 경우 기사 하단의 개인 QQ 명함을 참조하면 전문 기술 인력이 원격으로 지원합니다! 1) 원격 설치 및 운영 환경, 코드 디버깅 2) Qt, C++, Python 진입 가이드 3) 인터페이스 미화 4) 소프트웨어 제작



Blogger 추천 기사: Python 얼굴 인식 통계 qt 양식 - CSDN Blog

Blogger 추천 기사: Python Yolov5 화염 연기 인식 소스 코드 공유 - CSDN Blog

                         Python OpenCV는 보행자 출입구에 출입하는 사람의 수를 인식합니다 - Python은 사람의 수를 인식합니다 - CSDN Blog

개인 블로그 홈페이지: alicema1111's blog_CSDN 블로그 - 웹 페이지 분야의 Python, C++, 블로거

모든 블로거의 기사를 보려면 여기를 클릭하십시오 . alicema1111's blog_CSDN 블로그-Python, C++, 웹 페이지 분야의 블로거

추천

출처blog.csdn.net/alicema1111/article/details/131227768