WPF 展示视频修改为WriteableBitmap |
您所在的位置:网站首页 › wpf视频播放控件 › WPF 展示视频修改为WriteableBitmap |
WPF开发者QQ群:340500857 由于微信群人数太多入群请添加小编微信号 yanjinhuawechat 或 W_Feng_aiQ 邀请入群 需备注WPF开发者 PS:有更好的方式欢迎推荐。 接着上一篇,进行WriteableBitmap性能优化 修改后运行对比如下: 前(CPU与内存不稳定): 后: 使用NuGet如下: 01 — 代码如下 一、创建MainWindow.xaml代码如下。 二、MainWindow.xaml.cs代码如下。 using OpenCvSharp; using OpenCvSharp.Extensions; using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Management; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using System.Windows.Threading; namespace OpenCVSharpExample { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow { private VideoCapture capCamera; private VideoWriter videoWriter; private Mat matImage = new Mat(); private Thread cameraThread; private Thread writerThread; private CascadeClassifier haarCascade; private WriteableBitmap writeableBitmap; private Rectangle rectangle; public List CameraArray { get { return (List)GetValue(CameraArrayProperty); } set { SetValue(CameraArrayProperty, value); } } public static readonly DependencyProperty CameraArrayProperty = DependencyProperty.Register("CameraArray", typeof(List), typeof(MainWindow), new PropertyMetadata(null)); public int CameraIndex { get { return (int)GetValue(CameraIndexProperty); } set { SetValue(CameraIndexProperty, value); } } public static readonly DependencyProperty CameraIndexProperty = DependencyProperty.Register("CameraIndex", typeof(int), typeof(MainWindow), new PropertyMetadata(0)); public bool IsSave { get { return (bool)GetValue(IsSaveProperty); } set { SetValue(IsSaveProperty, value); } } public static readonly DependencyProperty IsSaveProperty = DependencyProperty.Register("IsSave", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(IsSaveChanged)); private static void IsSaveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var mainWindow = d as MainWindow; if (e.NewValue != null) { var save = (bool) e.NewValue; if (save) mainWindow.StartRecording(); else mainWindow.StopRecording(); } } public MainWindow() { InitializeComponent(); Width = SystemParameters.WorkArea.Width / 1.5; Height = SystemParameters.WorkArea.Height / 1.5; this.Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { InitializeCamera(); } private void ComboBoxCamera_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (CameraArray.Count - 1 //{ // var converted = Convert(BitmapConverter.ToBitmap(matImage)); // imgViewport.Source = converted; //})); using (var img = BitmapConverter.ToBitmap(matImage)) { var now = DateTime.Now; var g = Graphics.FromImage(img); var brush = new SolidBrush(System.Drawing.Color.Red); g.DrawString($"北京时间:{ now.ToString("yyyy年MM月dd日 HH:mm:ss")}", new System.Drawing.Font("Arial", 18), brush, new PointF(5, 5)); rectangle = new Rectangle(0, 0, img.Width, img.Height); brush.Dispose(); g.Dispose(); Dispatcher.Invoke(new Action(() => { WriteableBitmapHelper.BitmapCopyToWriteableBitmap(img, writeableBitmap, rectangle, 0, 0, System.Drawing.Imaging.PixelFormat.Format32bppArgb); })); }; Thread.Sleep(100); } } private void StartRecording() { if (capCamera == null) { WPFDevelopers.Minimal.Controls.MessageBox.Show("未开启摄像机","提示",MessageBoxButton.OKCancel,MessageBoxImage.Error); return; } var videoFile = System.IO.Path.Combine(System.Environment.CurrentDirectory, "Video"); if (!System.IO.Directory.Exists(videoFile)) System.IO.Directory.CreateDirectory(videoFile); var currentTime = System.IO.Path.Combine(videoFile, $"{DateTime.Now.ToString("yyyyMMddHHmmsshh")}.avi"); videoWriter = new VideoWriter(currentTime, FourCCValues.XVID, capCamera.Fps, new OpenCvSharp.Size(capCamera.FrameWidth, capCamera.FrameHeight)); writerThread = new Thread(AddCameraFrameToRecording); writerThread.Start(); } private void StopRecording() { if (videoWriter != null && !videoWriter.IsDisposed) { videoWriter.Release(); videoWriter.Dispose(); videoWriter = null; } } private void AddCameraFrameToRecording() { var waitTimeBetweenFrames = 1_000 / capCamera.Fps; var lastWrite = DateTime.Now; while (!videoWriter.IsDisposed) { if (DateTime.Now.Subtract(lastWrite).TotalMilliseconds |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |