-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yanjinhuagood
committed
Feb 12, 2022
1 parent
7b1a67d
commit d1fd112
Showing
16 changed files
with
695 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
################################################################################ | ||
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。 | ||
################################################################################ | ||
|
||
/OpenCVSharpExample/OpenCVSharpExample/obj | ||
/OpenCVSharpExample/OpenCVSharpExample/bin | ||
/OpenCVSharpExample/packages | ||
This comment has been minimized.
Sorry, something went wrong. |
||
/OpenCVSharpExample/.vs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.31729.503 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenCVSharpExample", "OpenCVSharpExample\OpenCVSharpExample.csproj", "{E84892C2-C202-4FC8-8CF1-7A6896590DD6}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{E84892C2-C202-4FC8-8CF1-7A6896590DD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{E84892C2-C202-4FC8-8CF1-7A6896590DD6}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{E84892C2-C202-4FC8-8CF1-7A6896590DD6}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{E84892C2-C202-4FC8-8CF1-7A6896590DD6}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {5ACA24EE-F42F-40D1-B7DE-F754CD00BC29} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Application x:Class="OpenCVSharpExample.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:OpenCVSharpExample" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
<ResourceDictionary> | ||
<ResourceDictionary.MergedDictionaries> | ||
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers.Minimal;component/Themes/Theme.xaml"/> | ||
</ResourceDictionary.MergedDictionaries> | ||
</ResourceDictionary> | ||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Windows; | ||
|
||
namespace OpenCVSharpExample | ||
{ | ||
/// <summary> | ||
/// App.xaml 的交互逻辑 | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<ws:Window x:Class="OpenCVSharpExample.MainWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:ws="https://github.com/WPFDevelopersOrg.WPFDevelopers.Minimal" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:OpenCVSharpExample" | ||
Icon="OpenCV_Logo.png" | ||
mc:Ignorable="d" WindowStartupLocation="CenterScreen" | ||
Title="OpenCVSharpExample https://github.com/WPFDevelopersOrg" Height="450" Width="800"> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto"/> | ||
<RowDefinition /> | ||
<RowDefinition Height="Auto"/> | ||
</Grid.RowDefinitions> | ||
<ComboBox Name="ComboBoxCamera" ItemsSource="{Binding CameraArray,RelativeSource={RelativeSource AncestorType=local:MainWindow}}" | ||
Width="200" SelectedIndex="{Binding CameraIndex,RelativeSource={RelativeSource AncestorType=local:MainWindow}}" | ||
SelectionChanged="ComboBoxCamera_SelectionChanged"/> | ||
<Image Grid.Row="1" Name="imgViewport" Margin="0,4"/> | ||
<StackPanel Orientation="Horizontal" | ||
HorizontalAlignment="Center" | ||
Grid.Row="2"> | ||
<!--<Button Name="btRecord" Click="btRecord_Click" Content="Record" Style="{StaticResource PrimaryButton}" Width="100" Height="50" Margin="16"/>--> | ||
<Button Name="btStop" Click="btStop_Click" Content="Stop" Width="100" Height="50" Margin="16"/> | ||
</StackPanel> | ||
</Grid> | ||
</ws:Window> |
166 changes: 166 additions & 0 deletions
166
OpenCVSharpExample/OpenCVSharpExample/MainWindow.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
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 的交互逻辑 | ||
/// </summary> | ||
public partial class MainWindow | ||
{ | ||
private VideoCapture capCamera; | ||
private Mat matImage = new Mat(); | ||
private Thread cameraThread; | ||
|
||
public List<string> CameraArray | ||
{ | ||
get { return (List<string>)GetValue(CameraArrayProperty); } | ||
set { SetValue(CameraArrayProperty, value); } | ||
} | ||
|
||
public static readonly DependencyProperty CameraArrayProperty = | ||
DependencyProperty.Register("CameraArray", typeof(List<string>), 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(); | ||
} | ||
List<string> GetAllConnectedCameras() | ||
{ | ||
var cameraNames = new List<string>(); | ||
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(); | ||
} | ||
|
||
|
||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
OpenCVSharpExample/OpenCVSharpExample/OpenCVSharpExample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="..\packages\OpenCvSharp4.runtime.win.4.2.0.20200108\build\net\OpenCvSharp4.runtime.win.props" Condition="Exists('..\packages\OpenCvSharp4.runtime.win.4.2.0.20200108\build\net\OpenCvSharp4.runtime.win.props')" /> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{E84892C2-C202-4FC8-8CF1-7A6896590DD6}</ProjectGuid> | ||
<OutputType>WinExe</OutputType> | ||
<RootNamespace>OpenCVSharpExample</RootNamespace> | ||
<AssemblyName>OpenCVSharpExample</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> | ||
<WarningLevel>4</WarningLevel> | ||
<Deterministic>true</Deterministic> | ||
<NuGetPackageImportStamp> | ||
</NuGetPackageImportStamp> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="OpenCvSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\OpenCvSharp4.4.2.0.20200108\lib\net40\OpenCvSharp.dll</HintPath> | ||
</Reference> | ||
<Reference Include="OpenCvSharp.Blob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\OpenCvSharp4.4.2.0.20200108\lib\net40\OpenCvSharp.Blob.dll</HintPath> | ||
</Reference> | ||
<Reference Include="OpenCvSharp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\OpenCvSharp4.4.2.0.20200108\lib\net40\OpenCvSharp.Extensions.dll</HintPath> | ||
</Reference> | ||
<Reference Include="OpenCvSharp.UserInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\OpenCvSharp4.4.2.0.20200108\lib\net40\OpenCvSharp.UserInterface.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Drawing" /> | ||
<Reference Include="System.Management" /> | ||
<Reference Include="System.Xml" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="System.Xaml"> | ||
<RequiredTargetFramework>4.0</RequiredTargetFramework> | ||
</Reference> | ||
<Reference Include="WindowsBase" /> | ||
<Reference Include="PresentationCore" /> | ||
<Reference Include="PresentationFramework" /> | ||
<Reference Include="WPFDevelopers.Minimal, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\WPFDevelopers.Minimal.2.0.0\lib\net40\WPFDevelopers.Minimal.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ApplicationDefinition Include="App.xaml"> | ||
<Generator>MSBuild:Compile</Generator> | ||
<SubType>Designer</SubType> | ||
</ApplicationDefinition> | ||
<Page Include="MainWindow.xaml"> | ||
<Generator>MSBuild:Compile</Generator> | ||
<SubType>Designer</SubType> | ||
</Page> | ||
<Compile Include="App.xaml.cs"> | ||
<DependentUpon>App.xaml</DependentUpon> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
<Compile Include="MainWindow.xaml.cs"> | ||
<DependentUpon>MainWindow.xaml</DependentUpon> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Properties\AssemblyInfo.cs"> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
<Compile Include="Properties\Resources.Designer.cs"> | ||
<AutoGen>True</AutoGen> | ||
<DesignTime>True</DesignTime> | ||
<DependentUpon>Resources.resx</DependentUpon> | ||
</Compile> | ||
<Compile Include="Properties\Settings.Designer.cs"> | ||
<AutoGen>True</AutoGen> | ||
<DependentUpon>Settings.settings</DependentUpon> | ||
<DesignTimeSharedInput>True</DesignTimeSharedInput> | ||
</Compile> | ||
<EmbeddedResource Include="Properties\Resources.resx"> | ||
<Generator>ResXFileCodeGenerator</Generator> | ||
<LastGenOutput>Resources.Designer.cs</LastGenOutput> | ||
</EmbeddedResource> | ||
<None Include="packages.config" /> | ||
<None Include="Properties\Settings.settings"> | ||
<Generator>SettingsSingleFileGenerator</Generator> | ||
<LastGenOutput>Settings.Designer.cs</LastGenOutput> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Resource Include="OpenCV_Logo.png" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> | ||
<PropertyGroup> | ||
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText> | ||
</PropertyGroup> | ||
<Error Condition="!Exists('..\packages\OpenCvSharp4.runtime.win.4.2.0.20200108\build\net\OpenCvSharp4.runtime.win.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\OpenCvSharp4.runtime.win.4.2.0.20200108\build\net\OpenCvSharp4.runtime.win.props'))" /> | ||
</Target> | ||
</Project> |
6 changes: 6 additions & 0 deletions
6
OpenCVSharpExample/OpenCVSharpExample/OpenCVSharpExample.csproj.user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<ProjectView>ShowAllFiles</ProjectView> | ||
</PropertyGroup> | ||
</Project> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Ass