贵阳建立网站,织梦搭建企业网站,阿里巴巴官网国际站,牛二网站建设目的#xff1a;输出两台摄像头图像和两路设备图像#xff0c;每一路设备截图6张主要知识#xff1a;1. 通过SDK调取摄像头图像#xff0c;并对图像进行剪裁#xff1b;2. WPF中定时器DispatcherTimer用法#xff1b;3. WPF中跨线程访问控件方法Dispatcher.Invoke((Actio… 目的输出两台摄像头图像和两路设备图像每一路设备截图6张主要知识1. 通过SDK调取摄像头图像并对图像进行剪裁2. WPF中定时器DispatcherTimer用法3. WPF中跨线程访问控件方法 Dispatcher.Invoke((Action)delegate {}区别于winform中this.Invoke((Action)delegate {}4.xml操作 XmlDocument xmlDoc new XmlDocument();xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory \\config.xml);XmlNode settingNode xmlDoc.DocumentElement;XmlElement e settingNode.SelectSingleNode(DeviceType) as XmlElement;if (e null){deviceType tps2000;}else{deviceType e.InnerText;}
5. 带多个参数的委托 DP1 new DataProcess(DeviceIP1, LocalPort1,0);DP1.ShowEvent1 DrawControls1;DP1.Start();//启动线程
6.多线程操作 Thread t1 new Thread(new ThreadStart(DataRevThread)); //开启DataRevThreadt1.Name DataRevThread; //线程名字t1.Start();t1.IsBackground true; //后台运行
7. UDP接收解码8. emgucv使用9. 工厂模式ModelFactory MF new ModelFactory();DM MF.CreateDataModelFactory_v1(sNeed.ToString());
10.信号量线程间同步Semaphore TaskSemaphoreData new Semaphore(0, 2560); //数据缓存队列缓存区TaskSemaphoreRev.WaitOne(); //等待接收队列
11.Bitmap转换为ImageSource [System.Runtime.InteropServices.DllImport(gdi32.dll)]public static extern bool DeleteObject(IntPtr hObject);public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap){IntPtr hBitmap bitmap.GetHbitmap();ImageSource wpfBitmap System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());if (!DeleteObject(hBitmap)){throw new System.ComponentModel.Win32Exception();}return wpfBitmap;}
12.Queue和list的操作包括但是不限于以上内容代码如下MainWindow.xamlFluent:RibbonWindow x:ClassthzSoftware.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:Fluenturn:fluent-ribbonxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:wfclr-namespace:System.Windows.Forms;assemblySystem.Windows.Formsxmlns:wficlr-namespace:System.Windows.Forms.Integration;assemblyWindowsFormsIntegrationxmlns:localclr-namespace:thzSoftwaremc:IgnorabledTitleMainWindow Height600 Width800 WindowStartupLocationCenterScreen WindowStateMaximized BackgroundLightBlue ClosingRibbonWindow_ClosingGrid ShowGridLinesTrue Grid.RowDefinitionsRowDefinition Height*/RowDefinitionRowDefinition Height10*/RowDefinitionRowDefinition Height*/RowDefinitionRowDefinition Height10*/RowDefinition/Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition Width*/ColumnDefinition Width*/ColumnDefinition Width*/ColumnDefinition Width*/ColumnDefinition Width*/ColumnDefinition Width*/ColumnDefinition Width*/ColumnDefinition Width*//Grid.ColumnDefinitionsLabel Grid.Row0 Grid.Column0 NamelabelCamera1Status Content摄像头连接状态 HorizontalContentAlignmentCenter VerticalContentAlignmentCenter BackgroundLightCyan/wfi:WindowsFormsHost Grid.Row1 Grid.Column0 BackgroundLightGraywf:PictureBox x:NameCam1 //wfi:WindowsFormsHostLabel Grid.Row2 Grid.Column0 NamelabelCamera2Status Content摄像头连接状态 HorizontalContentAlignmentCenter VerticalContentAlignmentCenter BackgroundLightCyan/wfi:WindowsFormsHost Grid.Row3 Grid.Column0 BackgroundLightGraywf:PictureBox x:NameCam2 //wfi:WindowsFormsHostLabel Grid.Row0 Grid.Column1 NamelabelThz1Status Content太赫兹连接状态 HorizontalContentAlignmentCenter VerticalContentAlignmentCenter BackgroundLightCyan/Image Grid.Row1 Grid.Column1 NameThz1 /Label Grid.Row2 Grid.Column1 NamelabelThz2Status Content太赫兹连接状态 HorizontalContentAlignmentCenter VerticalContentAlignmentCenter BackgroundLightCyan/Image Grid.Row3 Grid.Column1 NameThz2 /Image Grid.Row1 Grid.Column2 NameThz1Image1 /Image Grid.Row1 Grid.Column3 NameThz1Image2 /Image Grid.Row1 Grid.Column4 NameThz1Image3 /Image Grid.Row1 Grid.Column5 NameThz1Image4 /Image Grid.Row1 Grid.Column6 NameThz1Image5 /Image Grid.Row1 Grid.Column7 NameThz1Image6 /Image Grid.Row3 Grid.Column2 NameThz2Image1 /Image Grid.Row3 Grid.Column3 NameThz2Image2 /Image Grid.Row3 Grid.Column4 NameThz2Image3 /Image Grid.Row3 Grid.Column5 NameThz2Image4 /Image Grid.Row3 Grid.Column6 NameThz2Image5 /Image Grid.Row3 Grid.Column7 NameThz2Image6 //Grid
/Fluent:RibbonWindowMainWindow.xaml.csusing System;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using MessageBox System.Windows.MessageBox;
using thzModel;
using System.Drawing;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Xml;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Threading;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Windows.Controls;
using Image System.Windows.Controls.Image;namespace thzSoftware
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Fluent.RibbonWindow{DispatcherTimer Cam1ReconnectTimer, Cam2ReconnectTimer;public MainWindow()
{try{InitializeComponent();Environment.CurrentDirectory AppDomain.CurrentDomain.BaseDirectory;init();}catch (Exception ex){MessageBox.Show(ex.StackTrace ex.Message);LogWrite.logWrite(ex.Message, ex.StackTrace);}}public IntPtr PictureDev1Cam { get { return Cam1.Handle; } }public IntPtr PictureDev2Cam { get { return Cam2.Handle; } }IntPtr Cam1Handle IntPtr.Zero;IntPtr Cam2Handle IntPtr.Zero;Camera Camera1 new Camera();Camera Camera2 new Camera();static private string Cam1IP 192.168.1.64;static private string Cam2IP 192.168.1.61;DataProcess DP1 null, DP2 null;object ThreadLockBitmap new object();ListImageSource thzBitmapList1 new ListImageSource();ListImageSource thzBitmapList2 new ListImageSource();int snapFlag1 0, snapFlag2 0;static public String DeviceType{get{return deviceType;}set{deviceType value;}}static private string deviceType tps2000;void init()
{ReadConfigXML();Cam1Handle PictureDev1Cam;Cam2Handle PictureDev2Cam;Cam1.SizeMode PictureBoxSizeMode.Zoom;Cam2.SizeMode PictureBoxSizeMode.Zoom;Cam1ReconnectTimer new DispatcherTimer();Cam1ReconnectTimer.Interval new TimeSpan(0, 0, 3);//定时间隔3秒Cam1ReconnectTimer.Tick Cam1ReconnectTimer_Tick;//加载事件敲tab键事件框架可以自己出来Cam1ReconnectTimer.Start();Cam2ReconnectTimer new DispatcherTimer();Cam2ReconnectTimer.Interval new TimeSpan(0, 0, 3);//定时间隔3秒Cam2ReconnectTimer.Tick Cam2ReconnectTimer_Tick;//加载事件敲tab键事件框架可以自己出来Cam2ReconnectTimer.Start();thzModel.Command.CommandUp(8);//发送启动指令string DeviceIP1 192.168.1.110;int LocalPort1 8007;DP1 new DataProcess(DeviceIP1, LocalPort1,0);DP1.ShowEvent1 DrawControls1;DP1.Start();//启动线程string DeviceIP2 192.168.1.120;int LocalPort2 8009;DP2 new DataProcess(DeviceIP2, LocalPort2, 1);DP2.ShowEvent2 DrawControls2;DP2.Start();//启动线程}private void DrawControls1(Bitmap image, bool isWarning, bool isBlack)
{Image[] iSource { Thz1Image1, Thz1Image2, Thz1Image3, Thz1Image4, Thz1Image5, Thz1Image6 };Dispatcher.Invoke((Action)delegate{labelThz1Status.Content 太赫兹连接成功;Thz1.Source ChangeBitmapToImageSource(image);snapFlag1;thzBitmapList1.Add(Thz1.Source);if (thzBitmapList1.Count 6)thzBitmapList1.RemoveAt(0);if (snapFlag1 8){snapFlag1 0;for (int i 0; i thzBitmapList1.Count; i){iSource[i].Source thzBitmapList1[i];}}});}[System.Runtime.InteropServices.DllImport(gdi32.dll)]public static extern bool DeleteObject(IntPtr hObject);public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
{IntPtr hBitmap bitmap.GetHbitmap();ImageSource wpfBitmap System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());if (!DeleteObject(hBitmap)){throw new System.ComponentModel.Win32Exception();}return wpfBitmap;}private void DrawControls2(Bitmap image, bool isWarning, bool isBlack)
{Image[] iSource { Thz2Image1, Thz2Image2, Thz2Image3, Thz2Image4, Thz2Image5, Thz2Image6 };Dispatcher.Invoke((Action)delegate {labelThz2Status.Content 太赫兹连接成功; Thz2.Source ChangeBitmapToImageSource(image);snapFlag2;thzBitmapList2.Add(Thz2.Source);if (thzBitmapList2.Count 6)thzBitmapList2.RemoveAt(0);if (snapFlag2 8){snapFlag2 0;for (int i 0; i thzBitmapList2.Count; i){iSource[i].Source thzBitmapList2[i];}}});}private void ReadConfigXML()
{try{XmlDocument xmlDoc new XmlDocument();xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory \\config.xml);XmlNode settingNode xmlDoc.DocumentElement;XmlElement e settingNode.SelectSingleNode(DeviceType) as XmlElement;if (e null){deviceType tps2000;}else{deviceType e.InnerText;}}catch (Exception ex){LogWrite.logWrite(ex.Message, ex.StackTrace);}}private void ConnectCamera(int whitch)
{try{string userName admin;string password a123456.;int PortCamera 8000;if (whitch 1){labelCamera1Status.Content 摄像头连接中...;Task.Run(() {if (!Camera1.ConnectCamera(Cam1IP, PortCamera, userName, password)){Dispatcher.Invoke((Action)delegate { labelCamera1Status.Content 摄像头连接失败; });}else{Dispatcher.Invoke((Action)delegate { labelCamera1Status.Content 摄像头连接成功; });Camera1.Preview(Cam1Handle);Camera1.AdjustMirrorPara(1);Cam1ReconnectTimer.Stop();}});}else{labelCamera2Status.Content 摄像头连接中...;Task.Run(() {if (!Camera2.ConnectCamera(Cam2IP, PortCamera, userName, password)){Dispatcher.Invoke((Action)delegate { labelCamera2Status.Content 摄像头连接失败; });}else{Dispatcher.Invoke((Action)delegate { labelCamera2Status.Content 摄像头连接成功; });Camera2.Preview(Cam2Handle);Camera2.AdjustMirrorPara(1);Cam2ReconnectTimer.Stop();}});}}catch (Exception ex){MessageBox.Show(ex.StackTrace ex.Message);LogWrite.logWrite(ex.Message, ex.StackTrace);}}private void Cam1ReconnectTimer_Tick(object sender, EventArgs e)
{ConnectCamera(1);}private void RibbonWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{thzModel.Command.CommandUp(0);//发送停止指令//Application.Exit();}private void Cam2ReconnectTimer_Tick(object sender, EventArgs e)
{ConnectCamera(2);}}}其余部分代码太长不贴了需要的话自己下载百度网盘链接https://pan.baidu.com/s/1k2F0C-0gXX-tK_32m_ksOA 提取码abs5