C#_WPF中创建二维码、识别二维码
生活随笔
收集整理的這篇文章主要介紹了
C#_WPF中创建二维码、识别二维码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#_WPF中創建二維碼、識別二維碼 原文: C#_WPF中創建二維碼、識別二維碼
第三方庫:
WPFMediaKit.dll (WPFMediaKit攝像頭處理)
zing.dll
?
NuGet安裝這兩個第三方dll
?
?
?
項目截圖預覽:
?
?項目代碼:
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; using WPFMediaKit.DirectShow.Controls; using ZXing; using ZXing.Common; using ZXing.QrCode.Internal;namespace QRcode {/// <summary>/// MainWindow.xaml 的交互邏輯/// </summary>public partial class MainWindow : Window{/// <summary>/// ZXING 二維碼掃描類/// </summary>BarcodeReader codeReader = new BarcodeReader();/// <summary>/// 定時器/// </summary>DispatcherTimer cameraTimer = new DispatcherTimer();public MainWindow(){InitializeComponent();// 配置的攝像頭名稱var camera = "Lenovo EasyCamera";if (MultimediaUtil.VideoInputNames.Contains(camera)){//控件制定攝像頭vce.VideoCaptureSource = camera;cameraTimer.IsEnabled = false;cameraTimer.Interval = new TimeSpan(200); //執行間隔0.2秒cameraTimer.Tick += cameraTimer_Tick;}}/// <summary>/// 計時器方法/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void cameraTimer_Tick(object sender, EventArgs e){RenderTargetBitmap bmp = new RenderTargetBitmap((int)vce.ActualWidth, (int)vce.ActualHeight, 96, 96, PixelFormats.Default);vce.Measure(vce.RenderSize);vce.Arrange(new Rect(vce.RenderSize));bmp.Render(vce);BitmapEncoder encoder = new JpegBitmapEncoder();encoder.Frames.Add(BitmapFrame.Create(bmp));using (MemoryStream ms = new MemoryStream()){encoder.Save(ms);Bitmap btiMap = new Bitmap(ms);var result = codeReader.Decode(btiMap);//解析條碼if (result != null){// 1:停止識別cameraTimer.Stop();vce.Play(); MessageBox.Show($"識別內容為:{result}");}}}private void BtnShiBie_Click(object sender, RoutedEventArgs e){cameraTimer.Start();}private void BtnShnegCeng_Click(object sender, RoutedEventArgs e){var codimg= Create("hello world!!");imgQR.Source = ChangeBitmapToImageSource(codimg);}/// <summary>/// 創建二維碼/// </summary>/// <param name="msg">二維碼中保存的信息</param>/// <returns></returns>public static Bitmap Create(string msg){MultiFormatWriter writer = new MultiFormatWriter();Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();//設置二維碼為utf-8編碼hint.Add(EncodeHintType.CHARACTER_SET, "utf-8");//設置糾錯等級, 高hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);BitMatrix bm = writer.encode(msg, BarcodeFormat.QR_CODE, 200, 120, hint);BarcodeWriter barcodeWriter = new BarcodeWriter();Bitmap bitmap = barcodeWriter.Write(bm);string codePath = Directory.GetCurrentDirectory() + "/code.jpg";if (File.Exists(codePath))File.Delete(codePath);bitmap.Save(codePath);return bitmap;}/// <summary>/// 從bitmap轉換成ImageSource/// </summary>/// <param name="icon"></param>/// <returns></returns>public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap){IntPtr hBitmap = bitmap.GetHbitmap();ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());if (!DeleteObject(hBitmap)){throw new System.ComponentModel.Win32Exception();}return wpfBitmap;}[DllImport("gdi32.dll", SetLastError = true)]private static extern bool DeleteObject(IntPtr hObject);} }
?項目源代碼地址:https://download.csdn.net/download/qingchundaima/11151083
?
posted on 2019-06-04 10:19 NET未來之路 閱讀( ...) 評論( ...) 編輯 收藏轉載于:https://www.cnblogs.com/lonelyxmas/p/10971979.html
總結
以上是生活随笔為你收集整理的C#_WPF中创建二维码、识别二维码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue中的this.$router.re
- 下一篇: 对比Windows Phone与iOS、