什么网站可以做动图,深圳网站建设公司那家好,dede网站被黑,给网站做导流WPF开发者QQ群#xff1a;340500857由于微信群人数太多入群请添加小编微信号yanjinhuawechat 或 W_Feng_aiQ 邀请入群需备注WPF开发者 PS#xff1a;有更好的方式欢迎推荐。接着很久前的上一篇此项目使用了OpenCVSharp加载本地摄像头#xff0c;多个摄像头支持切换展示… WPF开发者QQ群340500857 由于微信群人数太多入群请添加小编微信号 yanjinhuawechat 或 W_Feng_aiQ 邀请入群 需备注WPF开发者 PS有更好的方式欢迎推荐。 接着很久前的上一篇 此项目使用了OpenCVSharp加载本地摄像头多个摄像头支持切换展示也可以展示rtsp地址。使用NuGet如下01—代码如下一、创建MainWindow.xaml代码如下。ws:Window x:ClassOpenCVSharpExample.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:wshttps://github.com/WPFDevelopersOrg.WPFDevelopers.Minimalxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:OpenCVSharpExampleIconOpenCV_Logo.pngmc:Ignorabled WindowStartupLocationCenterScreenTitleOpenCVSharpExample https://github.com/WPFDevelopersOrg Height450 Width800GridGrid.RowDefinitionsRowDefinition HeightAuto/RowDefinition /RowDefinition HeightAuto//Grid.RowDefinitionsComboBox NameComboBoxCamera ItemsSource{Binding CameraArray,RelativeSource{RelativeSource AncestorTypelocal:MainWindow}} Width200 SelectedIndex{Binding CameraIndex,RelativeSource{RelativeSource AncestorTypelocal:MainWindow}}SelectionChangedComboBoxCamera_SelectionChanged/Image Grid.Row1 NameimgViewport Margin0,4/StackPanel OrientationHorizontalHorizontalAlignmentCenterGrid.Row2!--Button NamebtRecord ClickbtRecord_Click ContentRecord Style{StaticResource PrimaryButton} Width100 Height50 Margin16/--Button NamebtStop ClickbtStop_Click ContentStop Width100 Height50 Margin16//StackPanel/Grid
/ws:Window二、MainWindow.xaml.cs代码如下。using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
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
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow{private VideoCapture capCamera;private Mat matImage new Mat();private Thread cameraThread;public Liststring CameraArray{get { return (Liststring)GetValue(CameraArrayProperty); }set { SetValue(CameraArrayProperty, value); }}public static readonly DependencyProperty CameraArrayProperty DependencyProperty.Register(CameraArray, typeof(Liststring), 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 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 CameraIndex)return;if (capCamera ! null cameraThread ! null){cameraThread.Abort();StopDispose();}capCamera new VideoCapture(CameraIndex);capCamera.Fps 30;CreateCamera();}private void InitializeCamera(){CameraArray GetAllConnectedCameras();}Liststring GetAllConnectedCameras(){var cameraNames new Liststring();using (var searcher new ManagementObjectSearcher(SELECT * FROM Win32_PnPEntity WHERE (PNPClass Image OR PNPClass Camera))){foreach (var device in searcher.Get()){cameraNames.Add(device[Caption].ToString());}}return cameraNames;}void CreateCamera(){cameraThread new Thread(PlayCamera);cameraThread.Start();}private void PlayCamera(){while (capCamera ! null !capCamera.IsDisposed){capCamera.Read(matImage);if (matImage.Empty()) break;Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() {var converted Convert(BitmapConverter.ToBitmap(matImage));imgViewport.Source converted;}));}}private void btStop_Click(object sender, RoutedEventArgs e){StopDispose();btStop.IsEnabled false;}void StopDispose(){if (capCamera ! null capCamera.IsOpened()){capCamera.Dispose();capCamera null;}}void CreateRecord(){cameraThread new Thread(PlayCamera);cameraThread.Start();}BitmapImage Convert(Bitmap src){System.Drawing.Image img src;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));brush.Dispose();g.Dispose();MemoryStream ms new MemoryStream();img.Save(ms, ImageFormat.Bmp);ms.Seek(0, SeekOrigin.Begin);BitmapImage image new BitmapImage();image.BeginInit();image.StreamSource ms;image.EndInit();image.Freeze();return image;}protected override void OnClosed(EventArgs e){StopDispose();}}
}02—效果预览鸣谢素材提供者 - 屈越源码地址如下Githubhttps://github.com/WPFDevelopersOrghttps://github.com/WPFDevelopersOrg/OpenCVSharpExampleGiteehttps://gitee.com/WPFDevelopersOrgWPF开发者QQ群 340500857 Githubhttps://github.com/WPFDevelopersOrg出处https://www.cnblogs.com/yanjinhua版权本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。转载请著名作者 出处 https://github.com/WPFDevelopersOrg扫一扫关注我们更多知识早知道点击阅读原文可跳转至源代码