阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

简单介绍C#调用USB摄像头的方法

30次阅读
没有评论

共计 2459 个字符,预计需要花费 7 分钟才能阅读完成。

导读 这篇文章主要为大家详细介绍了 C# 调用 USB 摄像头的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

C# 调用 USB 摄像头使用 AForge 类库进行开发,供大家参考,具体内容如下

1、AForge 安装

右击工程,在管理 NuGet 程序包中搜索 Aforge 类库,选择安装,如下图所示

简单介绍 C# 调用 USB 摄像头的方法

简单介绍 C# 调用 USB 摄像头的方法

2、进行 USB 摄像头类封装

a、初始化,初始化时要注意,加载的设备分辨率需要人工配置,如果配置分辨率不存在需要从默认的分辨率中选择

videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  if (videoDevices.Count > 0 && videoDevices.Count >= CameraIndex)
       {FilterInfo info = videoDevices[videoDevices.Count - 1];
         videoSource = new VideoCaptureDevice(info.MonikerString);
          if (videoSource.VideoCapabilities.Length > 0)
            {
             VideoCapabilities tmp = videoSource.VideoCapabilities.
               First(x => x.FrameSize.Width == LocalSize.Width &&
                       x.FrameSize.Height == LocalSize.Height);
                   if (tmp != null)
                   {
                    videoSource.SnapshotResolution = tmp;
                    videoSource.VideoResolution = tmp;
                   }
                 else
                  {int index = (videoSource.VideoCapabilities.Length + 1) / 2;
                    tmp = videoSource.VideoCapabilities[index];
                    }
                  videoSourcePlayer.VideoSource = videoSource;
                  videoSourcePlayer.Start();
                  videoSource.NewFrame += new NewFrameEventHandler(Video_NewFrame);
                    }
                }
            }
      catch (Exception ex)
       {LogHelper.Debug(ex);
}

b、绑定回调方法,此方法在摄像头成功预览之后会实时返回数据帧,封装时可以传入 PictureBox,把回调旋转后的图片显示在此控件上

private void Video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            try
            {Bitmap video = (Bitmap)eventArgs.Frame.Clone();
                BmpRotate(video);
                if (UsbVideo != null)
                    UsbVideo.Image = video;
            }
            catch (Exception ex)
            {LogHelper.Debug(ex);
            }
        }
  
        /// 
        /// 图像旋转
        /// 
        /// 
        private void BmpRotate(Bitmap _bmp)
        {
            try
            {if (CameraRotate == "0")
                { }
                else if (CameraRotate == "90")
                {_bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
                }
                else if (CameraRotate == "180")
                {_bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
                }
                else if (CameraRotate == "270")
                {_bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
                }
            }
            catch (Exception ex)
            {LogHelper.Debug(ex);
            }
}

c、抓图事件,手动抓图事件,通过调用 GetCurrentVideoFrame() 方法获取 Bitmap 图片

public Bitmap GetCurrentVideoFrame()
      {
            Bitmap bmp = null;
            try
            {bmp = videoSourcePlayer.GetCurrentVideoFrame();
                BmpRotate(bmp);
            }
            catch (Exception ex)
            {LogHelper.Debug(ex);
            }
            return bmp;
        }

d、摄像头重连,此类库中 videoSourcePlayer 有个属性 IsRunning 可以判断是否 USB 摄像头预览中,可以对设备进行重连

private FilterInfoCollection videoDevices = null; // 摄像头设备
public VideoCaptureDevice videoSource = null; // 视频的来源选择
private VideoSourcePlayer videoSourcePlayer = new VideoSourcePlayer();
public Bitmap img = null;
public int CameraIndex = 1;
        /// 
        /// 默认分辨率
        /// 
        public Size LocalSize = new Size(640, 480);
        bool isHave = false;
        public string CameraRotate = "0";
        private System.Windows.Forms.PictureBox UsbVideo = null;
        public void ReConnect()
        {
            try
            {if (!videoSourcePlayer.IsRunning)
                {videoSource.Stop();
                   videoSource.Start();}
            }
            catch (Exception)
            {}}

以上就是本文的全部内容,希望对大家的学习有所帮助。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2459字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中