在WPF中開啟攝像頭掃描二維碼(Media+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" 。

效果如下。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM