C#使用EmguCV預覽RTSP視頻,EmguCV保存RTSP視頻


在做一個視頻相關的業務,需要調用攝像頭查看監控,攝像頭使用的是海思的品牌,本打算用海思的SDK,但考慮到客戶可能不只是一個牌子的攝像頭,海思的SDK可能對其他品牌不兼容,所以考慮使用RTSP方式調用。

那么在C#中怎么調用RTSP實時視頻呢,在網上查了很多資料,各位大神提供的資料都是虎頭蛇尾的,最后在國外的網站找到了答案,免費分享出來供大家參考(發現很多資料CSDN上的,需要積分下載 而且不一定有用)。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
namespace WpfAppTest
{
    /// <summary>
    /// Window1.xaml 的交互邏輯
    /// </summary>
    public partial class Window1 : Window
    {
        private Capture currentDevice;
        private VideoWriter videoWriter;
        private bool recording;
        private int videoWidth;
        private int videoHeight;

        public Window1()
        {
            InitializeComponent();
            InitializeVariables();
        }
        private void InitializeVariables()
        {
            currentDevice = new Capture("rtsp://admin:***@172.16.21.80:554");
            recording = false;
            videoWidth = currentDevice.Width;
            videoHeight = currentDevice.Height;
        }
        private void CurrentDevice_ImageGrabbed(object sender, EventArgs e)
        {
            try
            {
                Mat m = new Mat();
                currentDevice.Retrieve(m,0);
                VideoPictureBox.Image = m.Bitmap;
                if (recording && videoWriter != null)
                {
                    videoWriter.Write(m);
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void Preview_Click(object sender, RoutedEventArgs e)
        {
            currentDevice.ImageGrabbed += CurrentDevice_ImageGrabbed;
            currentDevice.Start();
        }

        private void StartRecording_Click(object sender, RoutedEventArgs e)
        {
            recording = true;
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.DefaultExt = ".MP4";
            dialog.AddExtension = true;
            dialog.FileName = DateTime.Now.ToString();
            DialogResult dialogResult = dialog.ShowDialog();
            if (dialogResult != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            videoWriter = new VideoWriter(dialog.FileName, VideoWriter.Fourcc('M', 'P', '4', 'V'), 30, new System.Drawing.Size(videoWidth, videoHeight), true);
        }

        private void StopRecording_Click(object sender, RoutedEventArgs e)
        {
            recording = false;
            if (videoWriter != null)
            {
                currentDevice.Stop();
                videoWriter.Dispose();
            }
        }

    }
}
    

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM