现在位置首页 / 项目实战 /正文

wpf教程-AForge.net调用摄像头/切换摄像头/保存图片及像素问题

作者: IT小兵 | 2014年7月16日| 热度:℃ | 评论: |参与:

上一篇说明在wpf下使用AForge.net还是比较方便和实用的([wpf控件-几个调用摄像头开源控件的使用说明及问题(WPFMediaKit/WpfCap/AForge.net)])。

这一篇说一下AForge.net具体使用和注意的地方。

1、获取设备上的所有摄像头:

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // 设定初始视频设备
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            xcount = videoDevices.Count;
            if (videoDevices.Count > 0)
            {   // 默认设备
                sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
            }
            sourcePlayer.Start();
           // 设置图片框初始图像
            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                Properties.Resources.noimage.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            fingerPictureBox1.InitialImage = bs;          
            if (sourcePlayer.IsRunning)
            {
                button_Capture.IsEnabled = true;
            }
        }

sourcePlayer.IsRunning标示的是摄像头是否工作。

2、多个摄像头切换:

    /// <summary>
        /// 切换摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Qh_Click(object sender, RoutedEventArgs e)
        {
            if (j < xcount-1)
            {
                j = j + 1;
            }
            else if (j == xcount-1)
            {
                j = 0;            
            }
            sourcePlayer.Stop();
            sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[j].MonikerString);
            sourcePlayer.Start();
        }

3、拍摄照片:

/// <summary>
        /// 拍摄图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Capture_Click(object sender, RoutedEventArgs e)
        {
            // 判断视频设备是否开启
            try
            {
                if (sourcePlayer.IsRunning)
                {   // 进行拍照
                    if (fingerPictureBox1.Visibility == Visibility.Hidden)
                    {
                        object box = this.FindName("fingerPictureBox1");
                        if (box is FingerPictureBox)
                        {
                            (box as FingerPictureBox).ActiveImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                sourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
                                IntPtr.Zero,
                                Int32Rect.Empty,
                                BitmapSizeOptions.FromEmptyOptions());
                        }
                        fingerPictureBox1.Visibility = Visibility.Visible;
                        cam_sumit.Visibility = Visibility.Hidden;
                        lb_text.Content = "启动摄像头";
                    }
                    else {
                        fingerPictureBox1.Visibility = Visibility.Hidden;
                        cam_sumit.Visibility = Visibility.Visible;
                        lb_text.Content = "拍摄图像";                       
                    }
                }
            }
            catch {
                MessageBox.Show("请等待摄像头准备就绪再拍照!");
            }
        }

4、保存并返回:

 /// <summary>
        /// 保存返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Close_Click(object sender, RoutedEventArgs e)
        {
            var tempfilepath = "";
            RenderTargetBitmap bmp = new RenderTargetBitmap(
            (int)fingerPictureBox1.ActualWidth, (int)fingerPictureBox1.ActualHeight,
            96, 96, PixelFormats.Default);
            fingerPictureBox1.Measure(fingerPictureBox1.RenderSize);
            fingerPictureBox1.Arrange(new Rect(fingerPictureBox1.RenderSize));
            bmp.Render(fingerPictureBox1);
            BitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
                CaptureData = ms.ToArray();
                var  temppath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyy-mm-dd-hh-mm-sss") + ".jpg";
                File.WriteAllBytes(temppath, CaptureData);
                tempfilepath = FileOperation.AddFile(Common.Contract.WebModel.AyncModuleEnum.系统拍照, temppath, true);
            }
            if (CameraDoEvent != null)
            {
                CameraDoEvent(tempfilepath);
            }
            this.Close();
        }

5、拍摄窗口关闭时一定要关闭摄像头,不然摄像头会一直开着:

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (sourcePlayer.IsRunning)
            {   // 停止视频
                sourcePlayer.SignalToStop();
                sourcePlayer.WaitForStop();
                sourcePlayer.Stop();
            }
        }


点击阅读本文所属分类的更多文章: 项目实战 。和高手一起交流:346717337
友荐云推荐

未注明转发、原文均为本站原创。分享本文请注明 原文链接

给您更多信息和帮助

在这里您可以找到更多:

技术交流群:346717337 Jquery插件交流

投稿:[email protected]

承接:企业网站门户/微网站/微商城/CMS系统/微信公众号运营/业务咨询

echarts教程系列
本月最热文章

微信扫一扫,徜徉悠嘻网,您的休闲乐园

微信公众号:快乐每一天

随机文章
标签

技术交流群:346717337

投稿:[email protected]

专业专注:企业网站门户/微网站/微商城/CMS系统/微信公众号运营/付费问题咨询