日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Win10 UWP开发:摄像头扫描二维码/一维码功能

發布時間:2024/8/26 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Win10 UWP开发:摄像头扫描二维码/一维码功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這個示例演示整合了Aran和微軟的示例,無需修改即可運行。

支持識別,二維碼/一維碼,需要在包清單管理器勾選攝像頭權限。

首先右鍵項目引用,打開Nuget包管理器搜索安裝:ZXing.Net.Mobile

BarcodePage.xmal頁面代碼

<Pagex:Class="SuperTools.Views.BarcodePage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:SuperTools.Views"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Page.Transitions><TransitionCollection><NavigationThemeTransition><SlideNavigationTransitionInfo /></NavigationThemeTransition></TransitionCollection></Page.Transitions><Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><Grid x:Name="LayoutRoot" ><Grid x:Name="ContentPanel" ><!--視頻流預覽--><CaptureElement x:Name="VideoCapture" Stretch="UniformToFill"/><Grid Width="300" Height="300" x:Name="ViewGrid"><Rectangle Width="3" Height="50" Fill="Orange" HorizontalAlignment="Left" VerticalAlignment="Top"/><Rectangle Width="3" Height="50" Fill="Orange" HorizontalAlignment="Right" VerticalAlignment="Top"/><Rectangle Width="3" Height="50" Fill="Orange" HorizontalAlignment="Left" VerticalAlignment="Bottom"/><Rectangle Width="3" Height="50" Fill="Orange" HorizontalAlignment="Right" VerticalAlignment="Bottom"/><Rectangle Width="50" Height="3" Fill="Orange" HorizontalAlignment="Left" VerticalAlignment="Top"/><Rectangle Width="50" Height="3" Fill="Orange" HorizontalAlignment="Right" VerticalAlignment="Top"/><Rectangle Width="50" Height="3" Fill="Orange" HorizontalAlignment="Left" VerticalAlignment="Bottom"/><Rectangle Width="50" Height="3" Fill="Orange" HorizontalAlignment="Right" VerticalAlignment="Bottom"/><Rectangle x:Name="recScanning" Margin="12,0,12,0" VerticalAlignment="Center" Height="2" Fill="Green" RenderTransformOrigin="0.5,0.5" /></Grid></Grid></Grid></Grid> </Page>

BarcodePage.xmal.cs后臺代碼

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using Windows.ApplicationModel; using Windows.Devices.Enumeration; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.Graphics.Display; using Windows.Graphics.Imaging; using Windows.Media; using Windows.Media.Capture; using Windows.Media.Devices; using Windows.Media.MediaProperties; using Windows.Storage; using Windows.Storage.FileProperties; using Windows.Storage.Streams; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Navigation; using ZXing;// https://go.microsoft.com/fwlink/?LinkId=234238 上介紹了“空白頁”項模板namespace SuperTools.Views {/// <summary>/// 可用于自身或導航至 Frame 內部的空白頁。/// </summary>public sealed partial class BarcodePage : Page{private Result _result;private MediaCapture _mediaCapture;private DispatcherTimer _timer;private bool IsBusy;private bool _isPreviewing = false;private bool _isInitVideo = false;BarcodeReader barcodeReader;private static readonly Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1");public BarcodePage(){barcodeReader = new BarcodeReader{AutoRotate = true,Options = new ZXing.Common.DecodingOptions { TryHarder = true }};this.InitializeComponent();this.NavigationCacheMode = NavigationCacheMode.Required;Application.Current.Suspending += Application_Suspending;Application.Current.Resuming += Application_Resuming;}private async void Application_Suspending(object sender, SuspendingEventArgs e){// Handle global application events only if this page is activeif (Frame.CurrentSourcePageType == typeof(MainPage)){var deferral = e.SuspendingOperation.GetDeferral();await CleanupCameraAsync();deferral.Complete();}}private void Application_Resuming(object sender, object o){// Handle global application events only if this page is activeif (Frame.CurrentSourcePageType == typeof(MainPage)){InitVideoCapture();}}protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e){// Handling of this event is included for completenes, as it will only fire when navigating between pages and this sample only includes one pageawait CleanupCameraAsync();}protected override void OnNavigatedTo(NavigationEventArgs e){base.OnNavigatedTo(e);InitVideoCapture();}private async Task CleanupCameraAsync(){if (_isPreviewing){await StopPreviewAsync();}_timer.Stop();if (_mediaCapture != null){_mediaCapture.Dispose();_mediaCapture = null;}}private void InitVideoTimer(){_timer = new DispatcherTimer();_timer.Interval = TimeSpan.FromSeconds(1);_timer.Tick += _timer_Tick;_timer.Start();}private async Task StopPreviewAsync(){_isPreviewing = false;await _mediaCapture.StopPreviewAsync();// Use the dispatcher because this method is sometimes called from non-UI threadsawait Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>{VideoCapture.Source = null;});}private async void _timer_Tick(object sender, object e){try{if (!IsBusy){IsBusy = true;var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;VideoFrame videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);VideoFrame previewFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame);WriteableBitmap bitmap = new WriteableBitmap(previewFrame.SoftwareBitmap.PixelWidth, previewFrame.SoftwareBitmap.PixelHeight);previewFrame.SoftwareBitmap.CopyToBuffer(bitmap.PixelBuffer);await Task.Factory.StartNew(async () => { await ScanBitmap(bitmap); });}IsBusy = false;await Task.Delay(50);}catch (Exception){IsBusy = false;}}/// <summary>/// 解析二維碼圖片/// </summary>/// <param name="writeableBmp">圖片</param>/// <returns></returns>private async Task ScanBitmap(WriteableBitmap writeableBmp){try{await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>{_result = barcodeReader.Decode(writeableBmp.PixelBuffer.ToArray(), writeableBmp.PixelWidth, writeableBmp.PixelHeight, RGBLuminanceSource.BitmapFormat.Unknown);if (_result != null){//TODO: 掃描結果:_result.Text }});}catch (Exception){}}private async void InitVideoCapture(){///攝像頭的檢測 var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);if (cameraDevice == null){System.Diagnostics.Debug.WriteLine("No camera device found!");return;}var settings = new MediaCaptureInitializationSettings{StreamingCaptureMode = StreamingCaptureMode.Video,MediaCategory = MediaCategory.Other,AudioProcessing = AudioProcessing.Default,VideoDeviceId = cameraDevice.Id};_mediaCapture = new MediaCapture();await _mediaCapture.InitializeAsync(settings);VideoCapture.Source = _mediaCapture;await _mediaCapture.StartPreviewAsync();var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);props.Properties.Add(RotationKey, 90);await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);var focusControl = _mediaCapture.VideoDeviceController.FocusControl;if (focusControl.Supported){await focusControl.UnlockAsync();var setting = new FocusSettings { Mode = FocusMode.Continuous, AutoFocusRange = AutoFocusRange.FullRange };focusControl.Configure(setting);await focusControl.FocusAsync();}_isPreviewing = true;_isInitVideo = true;InitVideoTimer();}private static async Task<DeviceInformation> FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel){var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredPanel);return desiredDevice ?? allVideoDevices.FirstOrDefault();}} }

?

轉載于:https://www.cnblogs.com/myhalo/p/6635325.html

總結

以上是生活随笔為你收集整理的Win10 UWP开发:摄像头扫描二维码/一维码功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 色老久久| 亚洲卡一卡二 | 国产97自拍 | 国产精品毛片一区二区在线看舒淇 | 免费一区二区三区四区 | 欧美成人区 | 插插看| 亚洲精品一| 精品一区二区三区久久久 | 六月丁香综合网 | 午夜av免费 | 欧美在线色视频 | 天天av天天干 | 免费av一区 | 香蕉视频在线视频 | 欧美极品videos精品 | 青青青国产在线 | 国产一区美女 | 欧美黑人巨大xxx极品 | 亚洲图片在线视频 | yy6080午夜 | 懂色av蜜臂av粉嫩av | 亚洲综合丁香 | 色很久 | 国产乱码av | 国产在线xx| av射进来| 四虎成人在线 | 亚洲色图 欧美 | 国产精品久久久网站 | 国产一区精品视频 | japanese国产 | 在线99 | 韩国福利一区 | 国产伦理久久精品久久久久 | 精品国产aⅴ | 91ts人妖另类精品系列 | 国产老女人乱淫免费可以 | av嫩草 | 少妇一级淫片免费放中国 | 疯狂揉花蒂控制高潮h | 激情视频免费观看 | 东北高大丰满bbbbzbbb | 精品中文一区二区三区 | 特大巨交吊性xxxx | 色午夜av| 国产精品一区二区在线看 | 国产一区二区三区四区精 | av大片免费看 | 开心激情播播 | 成人免费在线电影 | 中文字幕一二三 | 日夜夜操 | 欧美区一区 | 国产av一区二区三区传媒 | 久久国产精品综合 | 男人扒女人添高潮视频 | 亚洲av日韩av永久无码下载 | 久热青草 | 日韩精品高清在线观看 | 欧洲美女与动交zozzo | 国产又爽又黄免费视频 | 亚洲在线免费观看视频 | 国产古装艳史毛片hd | 国产一区精品在线 | 在线中文字幕第一页 | 亚洲无码精品免费 | 天堂视频中文在线 | 天天撸一撸| 精品成人在线视频 | 日韩成人短视频 | 在线观看波多野结衣 | 九色porny丨精品自拍视频 | 国产网站免费在线观看 | 91久久久久国产一区二区 | 欧美bbbbb| 99av视频 | 国模无码大尺度一区二区三区 | 欧洲视频一区 | 亚洲久久久 | 成人福利午夜 | 色倩网站 | 乖疼润滑双性初h | 18xxxx日本 | 色婷婷av一区二区三区在线观看 | 久久久欧美精品 | 羞羞网站在线看 | 午夜精品久久久久久久久久久久 | 亚洲aaaaaaa | 亚洲少妇第一页 | 手机在线永久免费观看av片 | 人体写真 福利视频 | 青青草国产 | 欧美性爱视频久久 | 伊人影院在线观看视频 | 欧美又粗又深又猛又爽啪啪九色 | 女性向片在线观看 | 9.1在线观看免费 | av日韩在线播放 |