近兩天項目中需要添加一個功能,是根據攝像頭來讀取二維碼信息,然後根據讀出來的信息來和資料庫中進行對比顯示數據。 選擇技術Zxing、WPFMediaKit。基本的原理就是讓WPFmediaKit來對攝像頭進行操作,然後Zxing這個庫對圖片進行分析大致就是這樣。 在後臺中定義了定時器,用於解析當前攝 ...
近兩天項目中需要添加一個功能,是根據攝像頭來讀取二維碼信息,然後根據讀出來的信息來和資料庫中進行對比顯示數據。
選擇技術Zxing、WPFMediaKit。基本的原理就是讓WPFmediaKit來對攝像頭進行操作,然後Zxing這個庫對圖片進行分析大致就是這樣。
在後臺中定義了定時器,用於解析當前攝像頭的圖像,然後直接讀數據。
需要註意的是一定要引入 using WPFMediaKit.DirectShow.Controls; using ZXing;
public partial class YIDong : Page { public YIDong() { InitializeComponent(); cb.ItemsSource = MultimediaUtil.VideoInputNames;//獲得所有攝像頭 if (MultimediaUtil.VideoInputNames.Length > 0) { cb.SelectedIndex = 0;//第0個攝像頭為預設攝像頭 } else { MessageBox.Show("電腦沒有安裝任何可用攝像頭"); } cameraTimer.IsEnabled = false; cameraTimer.Interval = new TimeSpan(200); //執行間隔0.2秒 cameraTimer.Tick += cameraTimer_Tick; ; } /// <summary> /// ZXING 二維碼掃描類 /// </summary> BarcodeReader codeReader = new BarcodeReader(); /// <summary> /// 定時器 /// </summary> DispatcherTimer cameraTimer = new DispatcherTimer(); 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) { string shelve_index = result.ToString(); ObservableCollection<XModel.STORe_DetailVm> list = XDAL.STORE_goods_Detail.GetGoodsByShelve_Index(shelve_index); Dialog.OpenWindow open = Lib.pubMethod.GetOpenWindow(this); if (open != null) { Application.Current.Properties["SweepList"] = list; open.CloseAsTrue(); } } } } private void btnCapture_Click(object sender, RoutedEventArgs e) { cameraTimer.Start(); } private void Restart_Click(object sender, RoutedEventArgs e) { cameraTimer.Stop(); vce.Play(); } private void cb_SelectionChanged(object sender, SelectionChangedEventArgs e) { //控制項制定攝像頭 vce.VideoCaptureSource = (string)cb.SelectedItem; }
前臺佈局很簡單,
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="86*"/> <ColumnDefinition Width="237*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="5*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Background="LightBlue" Grid.Row="0" Grid.ColumnSpan="2"> <wpfmedia:VideoCaptureElement x:Name="vce" Stretch="Fill" Width="auto" Height="auto" Margin="0" Grid.Row="0" RenderTransformOrigin="0.5,0.5"> <wpfmedia:VideoCaptureElement.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <TranslateTransform/> </TransformGroup> </wpfmedia:VideoCaptureElement.RenderTransform> </wpfmedia:VideoCaptureElement> </StackPanel> <Label x:Name="label" Content="攝像頭:" Grid.Row="1" Grid.Column="0" Width="49" HorizontalAlignment="Right" Margin="0,14,0,4" /> <ComboBox x:Name="cb" Style="{StaticResource Query_Combo}" Grid.Row="1" Width="204" HorizontalAlignment="Left" Margin="2,13,0,8" SelectionChanged="cb_SelectionChanged" Grid.Column="1" /> <Button Content="開始" x:Name="btnCapture" Style="{StaticResource Query_Button}" Click="btnCapture_Click" Width="50" Grid.Row="2" Grid.Column="1" Height="20" HorizontalAlignment="Left" Margin="9,10,0,14"/> <Button Content="暫停" x:Name="btnReStart" Style="{StaticResource Query_Button}" Click="Restart_Click" Width="50" Grid.Row="2" Grid.Column="1" Height="20" HorizontalAlignment="Left" Margin="67,11,0,15"/> </Grid>
需要註意的是xaml一定要引入 xmlns:wpfmedia="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit" 。
效果如下。