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

WPF實現(xiàn)可視化掃碼器的示例代碼

 更新時間:2022年11月17日 16:30:02   作者:黑夜中的潛行者  
AForge.NET 是一個專門為開發(fā)者和研究者基于C#框架設(shè)計的,他包括計算機視覺與人工智能,圖像處理,神經(jīng)網(wǎng)絡(luò),遺傳算法,機器學(xué)習(xí),模糊系統(tǒng),機器人控制等領(lǐng)域。本文就將用它編寫一個可視化掃碼器,感興趣的可以了解一下

概述

以識別粵康碼識別為例。現(xiàn)在的深圳進出小區(qū)、商場、辦公樓、乘坐公共交通工具等都需要出示并掃描健康碼,也被稱之為“電子哨兵”。不多說那個用的是一般的掃碼槍。

攝像頭調(diào)度

調(diào)度攝像頭選擇使用 AForge.NET

AForge.NET 是一個專門為開發(fā)者和研究者基于C#框架設(shè)計的,他包括計算機視覺與人工智能,圖像處理,神經(jīng)網(wǎng)絡(luò),遺傳算法,機器學(xué)習(xí),模糊系統(tǒng),機器人控制等領(lǐng)域。AForge.Net 是C#的一個圖像計算機視覺庫,該庫是一個開源項目,提供很多圖像的處理,和視頻處理功能。

官方地址:http://www.aforgenet.com/

需要引入三個NuGet包:

  • AForge
  • AForge.Video
  • AForge.Video.DirectShow

如何使用這里先不介紹,后面有完整代碼

二維碼識別

需要的NuGet包:ZXing.Net

ZXing.Net 是由 Google ZXing 移植并優(yōu)化改進而來的。

Google ZXing 是目前一個常用的基于Java實現(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掃描已啟動,請將健康碼對準(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($"時間:{DateTime.Now:yyyy年MM月dd日 HH時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實現(xiàn)可視化掃碼器的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于WPF可視化掃碼器的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • c#調(diào)用winrar解壓縮文件代碼分享

    c#調(diào)用winrar解壓縮文件代碼分享

    這篇文章主要介紹了c#調(diào)用winrar解壓縮文件的方法,大家參考使用吧
    2013-12-12
  • C#實現(xiàn)提高xml讀寫速度的方法

    C#實現(xiàn)提高xml讀寫速度的方法

    這篇文章主要介紹了C#實現(xiàn)提高xml讀寫速度的方法,并且針對各類文件的讀寫做了較為細(xì)致的分析,非常實用,需要的朋友可以參考下
    2014-11-11
  • C#幾種截取字符串的方法小結(jié)

    C#幾種截取字符串的方法小結(jié)

    C#幾種截取字符串的方法小結(jié),需要的朋友可以參考一下
    2013-04-04
  • C# Winform 實現(xiàn)TCP發(fā)消息

    C# Winform 實現(xiàn)TCP發(fā)消息

    這篇文章主要介紹了C# Winform 實現(xiàn)TCP發(fā)消息的示例,幫助大家更好的理解和學(xué)習(xí)使用c#技術(shù),感興趣的朋友可以了解下
    2021-03-03
  • C#?時間戳轉(zhuǎn)換實例

    C#?時間戳轉(zhuǎn)換實例

    本文主要介紹了C#?時間戳轉(zhuǎn)換實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • C#使用iTextSharp封裝的PDF文件操作類實例

    C#使用iTextSharp封裝的PDF文件操作類實例

    這篇文章主要介紹了C#使用iTextSharp封裝的PDF文件操作類,實例分析了C#操作pdf文件的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-03-03
  • C#把文件上傳到服務(wù)器中的指定地址

    C#把文件上傳到服務(wù)器中的指定地址

    這篇文章介紹了C#實現(xiàn)文件上傳到服務(wù)器指定地址的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#編程自學(xué)之運算符和表達(dá)式

    C#編程自學(xué)之運算符和表達(dá)式

    這篇文章主要介紹了C#運算符和表達(dá)式,這是自學(xué)C#編程的第五篇,希望對大家的學(xué)習(xí)有所幫助。
    2015-10-10
  • C#調(diào)用EXE文件實現(xiàn)傳參和獲取返回結(jié)果

    C#調(diào)用EXE文件實現(xiàn)傳參和獲取返回結(jié)果

    本文主要介紹了C#調(diào)用EXE文件實現(xiàn)傳參和獲取返回結(jié)果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • choosesubject c# switch

    choosesubject c# switch

    c# switch 實例代碼。
    2009-07-07

最新評論