第三方庫:html
WPFMediaKit.dll (WPFMediaKit攝像頭處理)ide
zing.dllpost
NuGet安裝這兩個第三方dll編碼
項目截圖預覽:url
項目代碼:spa
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.net