WPF實(shí)現(xiàn)可視化掃碼器的示例代碼
概述
以識別粵康碼識別為例?,F(xiàn)在的深圳進(jìn)出小區(qū)、商場、辦公樓、乘坐公共交通工具等都需要出示并掃描健康碼,也被稱之為“電子哨兵”。不多說那個(gè)用的是一般的掃碼槍。
攝像頭調(diào)度
調(diào)度攝像頭選擇使用 AForge.NET
AForge.NET 是一個(gè)專門為開發(fā)者和研究者基于C#框架設(shè)計(jì)的,他包括計(jì)算機(jī)視覺與人工智能,圖像處理,神經(jīng)網(wǎng)絡(luò),遺傳算法,機(jī)器學(xué)習(xí),模糊系統(tǒng),機(jī)器人控制等領(lǐng)域。AForge.Net 是C#的一個(gè)圖像計(jì)算機(jī)視覺庫,該庫是一個(gè)開源項(xiàng)目,提供很多圖像的處理,和視頻處理功能。
官方地址:http://www.aforgenet.com/
需要引入三個(gè)NuGet包:
- AForge
- AForge.Video
- AForge.Video.DirectShow
如何使用這里先不介紹,后面有完整代碼
二維碼識別
需要的NuGet包:ZXing.Net
ZXing.Net 是由 Google ZXing 移植并優(yōu)化改進(jìn)而來的。
Google ZXing 是目前一個(gè)常用的基于Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫。
這支持的編碼方式種類相對齊全,并且支持可移植庫。
如何使用這里先不介紹,后面有完整代碼
源代碼
<Window x:Class="HealthCodeIdentification.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HealthCodeIdentification" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="MainWindow" Width="800" Height="450" WindowStartupLocation="CenterScreen" mc:Ignorable="d"> <Grid Background="AliceBlue"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" Margin="10" VerticalAlignment="Center"> <TextBlock x:Name="txtTips" Margin="0,10" HorizontalAlignment="Center" /> <Image x:Name="imageVideo" /> </StackPanel> <Grid Grid.Column="1" Margin="10"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBox x:Name="txtInfo" BorderThickness="0" TextWrapping="Wrap" /> <TextBlock x:Name="txtCodeColor" Grid.Row="1" FontSize="32"> <TextBlock.Style> <Style TargetType="TextBlock"> <Style.Triggers> <Trigger Property="Text" Value="綠碼"> <Setter Property="Foreground" Value="#0d0" /> </Trigger> <Trigger Property="Text" Value="黃碼"> <Setter Property="Foreground" Value="Yellow" /> </Trigger> <Trigger Property="Text" Value="紅碼"> <Setter Property="Foreground" Value="Red" /> </Trigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </Grid> </Grid> </Window>
using AForge.Video.DirectShow; using Newtonsoft.Json; using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; using ZXing; namespace HealthCodeIdentification { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { private FilterInfoCollection _videoDevices; private VideoCaptureDevice _videoSource; public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; Closed += MainWindow_Closed; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { OpenVideoCaptureDevice(); } private void MainWindow_Closed(object sender, EventArgs e) { if (_videoSource is null) return; _videoSource.SignalToStop(); } /// <summary> /// 查找并打開攝像頭 /// </summary> private void OpenVideoCaptureDevice() { _videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (_videoDevices.Count > 0) { _videoSource = new VideoCaptureDevice(_videoDevices[0].MonikerString); _videoSource.NewFrame += _camera_NewFrame; _videoSource.Start(); txtTips.Text = "AI掃描已啟動(dòng),請將健康碼對準(zhǔn)攝像頭"; return; } MessageBox.Show("請連接攝像頭!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); } /// <summary> /// 加載視頻 /// </summary> /// <param name="sender"></param> /// <param name="eventArgs"></param> private async void _camera_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) { var image = eventArgs.Frame.Clone(); Image videoImage = (Bitmap)image; Bitmap bitmapImage = (Bitmap)image; //image.RotateFlip(RotateFlipType.RotateNoneFlipX); // 設(shè)置圖像旋轉(zhuǎn) Graphics g = Graphics.FromImage(videoImage); //SolidBrush brush = new SolidBrush(Color.Red); //g.DrawString($"時(shí)間:{DateTime.Now:yyyy年MM月dd日 HH時(shí)mm分ss秒}", new Font("Arial", 18), brush, new PointF(5, 5)); //brush.Dispose(); //g.Dispose(); MemoryStream ms = new MemoryStream(); videoImage.Save(ms, ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); bi.Freeze(); await Dispatcher.BeginInvoke(new ThreadStart(async delegate { imageVideo.Source = bi; await QRCodeReader(bitmapImage); })); } private async Task<Task> QRCodeReader(Bitmap bitmap) { await Dispatcher.BeginInvoke(new ThreadStart(delegate { BarcodeReader barcodeReader = new BarcodeReader(); var result = barcodeReader.Decode(bitmap); if (result != null) { txtInfo.Text = result.ToString(); if (result.ToString().StartsWith("{")) { var data = JsonConvert.DeserializeObject<CodeData>(result.ToString()); switch (data.c.ToUpper()) { case "G": txtCodeColor.Text = "綠碼"; break; case "Y": txtCodeColor.Text = "黃碼"; break; case "R": txtCodeColor.Text = "紅碼"; break; } } } })); return Task.CompletedTask; } } public class CodeData { public string label { get; set; } public string cid { get; set; } public string cidtype { get; set; } public string name { get; set; } public string phone { get; set; } public string encode { get; set; } /// <summary> /// 顏色,綠碼G,黃碼Y,紅碼R /// </summary> public string c { get; set; } public string t { get; set; } public string v { get; set; } public string s { get; set; } } }
以上就是WPF實(shí)現(xiàn)可視化掃碼器的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于WPF可視化掃碼器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# Winform 實(shí)現(xiàn)TCP發(fā)消息
這篇文章主要介紹了C# Winform 實(shí)現(xiàn)TCP發(fā)消息的示例,幫助大家更好的理解和學(xué)習(xí)使用c#技術(shù),感興趣的朋友可以了解下2021-03-03C#使用iTextSharp封裝的PDF文件操作類實(shí)例
這篇文章主要介紹了C#使用iTextSharp封裝的PDF文件操作類,實(shí)例分析了C#操作pdf文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03C#調(diào)用EXE文件實(shí)現(xiàn)傳參和獲取返回結(jié)果
本文主要介紹了C#調(diào)用EXE文件實(shí)現(xiàn)傳參和獲取返回結(jié)果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01