欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF實現(xiàn)調(diào)用本機攝像頭的示例代碼

 更新時間:2022年08月03日 17:11:23   作者:驚鏵  
這篇文章主要介紹了如何利用WPF實現(xiàn)調(diào)用本機攝像頭,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,需要的可以參考一下

此項目使用了OpenCVSharp加載本地攝像頭,多個攝像頭支持切換展示,也可以展示rtsp地址。

使用NuGet如下:

代碼如下

一、創(chuàng)建MainWindow.xaml代碼如下。

?<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>

二、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?的交互邏輯
????///?</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();
????????}


????}
}

效果預覽

源碼地址如下

Github:https://github.com/WPFDevelopersOrg

https://github.com/WPFDevelopersOrg/OpenCVSharpExample

Gitee:https://gitee.com/WPFDevelopersOrg

到此這篇關于WPF實現(xiàn)調(diào)用本機攝像頭的示例代碼的文章就介紹到這了,更多相關WPF調(diào)用攝像頭內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 一文詳解Unity3D?AudioSource組件使用示例

    一文詳解Unity3D?AudioSource組件使用示例

    這篇文章主要為大家介紹了一文詳解Unity3D?AudioSource組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • WPF實現(xiàn)繪制3D圖形的示例代碼

    WPF實現(xiàn)繪制3D圖形的示例代碼

    WPF的3D功能可以在不編寫任何c#代碼的情況下進行繪制,只需要使用xaml即可完成3D圖形的渲染。本文主要講述了WPF-3D中的關鍵概念, 以及常用到的命中測試、2d控件如何在3D對象中進行渲染,希望大家有所幫助
    2023-03-03
  • C#?壓榨cpu的辦法(推薦)

    C#?壓榨cpu的辦法(推薦)

    這篇文章主要介紹了C#?壓榨cpu的辦法,通過修改num的值,觀察cpu的核數(shù),例如我電腦是8核的,改成8,運行時各個核都能跑滿,感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • C#+MO實現(xiàn)一個道路編輯軟件(剛開始)

    C#+MO實現(xiàn)一個道路編輯軟件(剛開始)

    C#+MO實現(xiàn)一個道路編輯軟件(剛開始)...
    2007-04-04
  • C#在 .NET中使用依賴注入的示例詳解

    C#在 .NET中使用依賴注入的示例詳解

    這篇文章主要為大家詳細介紹了C#如何在 .NET中使用依賴注入,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以跟隨小編一起了解一下
    2024-01-01
  • c# List find()方法返回值的問題說明(返回結果為對象的指針)

    c# List find()方法返回值的問題說明(返回結果為對象的指針)

    本篇文章主要介紹了c#中List find()方法返回值的問題說明(返回結果為對象的指針) 需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#多線程開發(fā)實戰(zhàn)記錄之線程基礎

    C#多線程開發(fā)實戰(zhàn)記錄之線程基礎

    線程是一個獨立的運行單元,每個進程內(nèi)部有多個線程,每個線程可以各自同時執(zhí)行指令,每個線程有自己獨立的棧,但是與進程內(nèi)的其他線程共享內(nèi)存,這篇文章主要給大家介紹了關于C#多線程開發(fā)實戰(zhàn)記錄之線程基礎的相關資料,需要的朋友可以參考下
    2021-09-09
  • C#多線程之線程通訊(AutoResetEvent)

    C#多線程之線程通訊(AutoResetEvent)

    這篇文章介紹了C#多線程之線程通訊(AutoResetEvent)的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 淺談C#中Action和Func回調(diào)的常用方式

    淺談C#中Action和Func回調(diào)的常用方式

    Action和Func泛型委托實際上就是一個.NET?Framework預定義的委托,本文主要介紹了C#中Action和Func回調(diào)的常用方式,具有一定的參加價值,感興趣的可以了解一下
    2022-03-03
  • C#如何遍歷Dictionary

    C#如何遍歷Dictionary

    這篇文章主要為大家詳細介紹了C#遍歷Dictionary的方法,.NET中的Dictionary是鍵/值對的集合,使用起來比較方便,Dictionary也可以用KeyValuePair來迭代遍歷,感興趣的小伙伴們可以參考一下
    2016-04-04

最新評論