亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

概述

以識別粵康碼識別為例?,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)文章

最新評論