1.移除在测试时存在内存泄漏问题的Aforge相关组件和代码

2.重新添加opencvsharp4组件和相关代码


Former-commit-id: 0b05f92e72ac7b6c75800ca0d7b585ad14fc8a59
TangShanKaiPing
wanggang 5 years ago
parent 3539a3cc27
commit 67d38ab328

@ -0,0 +1,18 @@
*.bak
*.suo
*.db
*.db-shm
*.db-wal
*.user
.vs
obj
Obj
bin
Bin
debug
Debug
release
Release
Logs
logs
node_modules

@ -22,7 +22,7 @@ using System.Runtime.InteropServices;
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("0f767d6d-72de-47fd-be40-432dadfa1fae")]
// 程序集的版本信息由下列四个值组成:
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.1")]
[assembly: AssemblyFileVersion("1.0.0.0")]

@ -39,15 +39,16 @@
| System.Windows.Forms.AnchorStyles.Right)));
this.log.Location = new System.Drawing.Point(12, 12);
this.log.Name = "log";
this.log.Size = new System.Drawing.Size(504, 184);
this.log.Size = new System.Drawing.Size(597, 370);
this.log.TabIndex = 0;
this.log.Text = "";
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(199, 213);
this.button2.Location = new System.Drawing.Point(267, 399);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 30);
this.button2.TabIndex = 2;
@ -58,7 +59,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(531, 255);
this.ClientSize = new System.Drawing.Size(624, 441);
this.Controls.Add(this.button2);
this.Controls.Add(this.log);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;

@ -2,6 +2,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
@ -19,7 +20,7 @@ namespace AutoUpdate
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterParent;
this.Text = "升级程序2";
this.Text += $" v{Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
this._file = file;
this._fileName = Path.GetFileNameWithoutExtension(file);
this._path = Path.GetDirectoryName(file);

@ -10,3 +10,4 @@ D:\1.0\ZHXY\labs\CameraCard\AutoUpdate\obj\Release\AutoUpdate.csproj.GenerateRes
D:\1.0\ZHXY\labs\CameraCard\AutoUpdate\obj\Release\AutoUpdate.csproj.CopyComplete
D:\1.0\ZHXY\labs\CameraCard\AutoUpdate\obj\Release\AutoUpdate.exe
D:\1.0\ZHXY\labs\CameraCard\AutoUpdate\obj\Release\AutoUpdate.pdb
D:\1.0\ZHXY\labs\CameraCard\AutoUpdate\obj\Release\AutoUpdate.csprojAssemblyReference.cache

@ -57,6 +57,7 @@
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="DirectShowLib-2005, Version=2.1.0.0, Culture=neutral, PublicKeyToken=67e7b740cdfc2d3f, processorArchitecture=MSIL">
@ -77,12 +78,12 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CVRSDK.cs" />
<Compile Include="Infrastructure\AForgeCameraHelper.cs" />
<Compile Include="Infrastructure\ImageHelper.cs" />
<Compile Include="Infrastructure\CVRHelper.cs" />
<Compile Include="Infrastructure\CVRSDK.cs" />
<Compile Include="Infrastructure\ICameraHelper.cs" />
<Compile Include="Infrastructure\IdCardModel.cs" />
<Compile Include="Infrastructure\OpenCvSharpCameraHelper.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
@ -119,8 +120,8 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AForge.Video.DirectShow">
<Version>2.2.5</Version>
<PackageReference Include="OpenCvSharp4.Windows">
<Version>4.2.0.20200208</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>

@ -1,71 +0,0 @@
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
public class AForgeCameraHelper : IDisposable, ICameraHelper
{
private CancellationTokenSource _cts = new CancellationTokenSource();
private List<string> _cameras = new List<string>();
private VideoCaptureDevice _videoSource;
private List<FilterInfo> _infos;
public Action<List<string>> OnCameraChange { get; set; }
public Action<Bitmap> OnFrame { get; set; }
public void Start()
{
Task.Run(async () =>
{
while (!this._cts.IsCancellationRequested)
{
this.ScanCameras();
await Task.Delay(2000);
}
});
}
private void ScanCameras()
{
this._infos = new FilterInfoCollection(FilterCategory.VideoInputDevice).Cast<FilterInfo>().ToList();
var cameras = this._infos.Select(o => o.Name).ToList();
if (!this._cameras.SequenceEqual(cameras))
{
this._cameras = cameras;
this.OnCameraChange?.Invoke(this._cameras);
}
}
public void SwitchCamera(int index)
{
this.Stop();
if (this._infos.Count > 0 && index < this._infos.Count)
{
var device = this._infos[index].MonikerString;
this._videoSource = new VideoCaptureDevice(device);
this._videoSource.VideoResolution = this._videoSource.VideoCapabilities.LastOrDefault();
this._videoSource.NewFrame += new NewFrameEventHandler((s, e) =>
{
this.OnFrame?.Invoke(e.Frame.Clone() as Bitmap);
});
this._videoSource.Start();
}
}
private void Stop()
{
if (this._videoSource != null && this._videoSource.IsRunning)
{
this._videoSource.Stop();
}
}
public void Dispose()
{
this._cts.Cancel();
this.Stop();
}
}

@ -0,0 +1,87 @@
using DirectShowLib;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OpenCvSharp;
using OpenCvSharp.Extensions;
public class OpenCvSharpCameraHelper : IDisposable, ICameraHelper
{
private CancellationTokenSource _cts = new CancellationTokenSource();
private List<string> _cameras = new List<string>();
private VideoCapture _videoCapture;
public Action<List<string>> OnCameraChange { get; set; }
public Action<Bitmap> OnFrame { get; set; }
public void Start()
{
Task.Run(async () =>
{
while (!this._cts.IsCancellationRequested)
{
this.ScanCameras();
await Task.Delay(2000);
}
});
}
private void ScanCameras()
{
var devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
var cameras = devices.Select(o => o.Name).ToList();
foreach (var item in devices)
{
item.Dispose();
}
if (!this._cameras.SequenceEqual(cameras))
{
this._cameras = cameras;
this.OnCameraChange?.Invoke(this._cameras);
}
}
public void SwitchCamera(int index)
{
this.Stop();
this._videoCapture = new VideoCapture(index);
if (this._videoCapture.IsOpened())
{
Task.Run(() =>
{
while (this._videoCapture.IsOpened())
{
try
{
using (var mat = new Mat())
{
this._videoCapture.Read(mat);
this.OnFrame?.Invoke(mat.ToBitmap());
}
}
catch
{
break;
}
}
Stop();
});
}
}
private void Stop()
{
if (this._videoCapture != null && _videoCapture.IsOpened())
{
this._videoCapture.Release();
}
}
public void Dispose()
{
this._cts.Cancel();
this.Stop();
}
}

@ -11,7 +11,7 @@ namespace CameraCard
{
public partial class MainForm : Form
{
private ICameraHelper _cameraHelper = new AForgeCameraHelper();
private ICameraHelper _cameraHelper = new OpenCvSharpCameraHelper();
private CVRHelper _cvrHelper = new CVRHelper();
@ -108,13 +108,16 @@ namespace CameraCard
private void shotBtn_Click(object sender, EventArgs e)
{
using (var bitmap = (this.cameraRender.Image.Clone() as Image))
lock (this.cameraRender.Image)
{
using (var bitmap2 = bitmap.ClipImage(this.cameraRender.Width, this.cameraRender.Height, this._rectWidth, this._rectHeight))
using (var bitmap = (this.cameraRender.Image.Clone() as Image))
{
this.photo.Image?.Dispose();
this.photo.Image = bitmap2.ResizeImage(this._photoWidth, this._photoHeight);
var base64Value = $"data:image/jpeg;base64,{this.photo.Image.ToBase64()}";
using (var bitmap2 = bitmap.ClipImage(this.cameraRender.Width, this.cameraRender.Height, this._rectWidth, this._rectHeight))
{
this.photo.Image?.Dispose();
this.photo.Image = bitmap2.ResizeImage(this._photoWidth, this._photoHeight);
var base64Value = $"data:image/jpeg;base64,{this.photo.Image.ToBase64()}";
}
}
}
}

Loading…
Cancel
Save