当前位置:   article > 正文

C#使用OpenCvSharp3实现摄像头录制及拍照功能_c# opencv 拍照

c# opencv 拍照

成果展示

背景

这边有这样的需求,需要对特定的生产片段进行录像,由我系统进行控制。最开始测试了AForge的开源库,但发现x64的情况下,有些类库不能正常运行,才有了现在用的OpenCvSharp3的方式实现。程序的界面参考了网上找的一个AForge的项目界面,感谢!

开发运行环境

win10 x64

.Net Framework 4.6.1

摄像头:海康威视 DS-UVC-U64 Pro

实现方式

首先,nuget OpenCvSharp3

主要代码

  1. class UvcCamera : CameraInterface
  2. {
  3. //标识:是否开始录制,是否暂停
  4. private bool gIsStartRecord = false;
  5. private bool gIsPauseRecord = false;
  6. VideoCapture _gVideoCapture;
  7. private VideoCapture gVideoCapture { get => _gVideoCapture; set => _gVideoCapture = value; }
  8. VideoWriter _gVideoWriter;
  9. private VideoWriter gVideoWriter { get => _gVideoWriter; set => _gVideoWriter = value; }
  10. private Bitmap _CurrentDisplay;
  11. public override Bitmap CurrentDisplay { get => _CurrentDisplay; set => _CurrentDisplay=value; }
  12. /// <summary>
  13. /// 录制线程
  14. /// </summary>
  15. private Thread gRecordThread;
  16. public override bool Connect(int index)
  17. {
  18. try
  19. {
  20. gVideoCapture = new VideoCapture(index);
  21. if (gVideoCapture.IsOpened())
  22. return true;
  23. else
  24. return false;
  25. }
  26. catch
  27. {
  28. return false;
  29. }
  30. }
  31. public override void DisConnect()
  32. {
  33. gVideoWriter?.Dispose();
  34. gVideoCapture?.Release();
  35. }
  36. public override void PauseRecordVideo()
  37. {
  38. if(gIsStartRecord)
  39. {
  40. gIsPauseRecord = true;
  41. gIsStartRecord = false;
  42. }
  43. }
  44. public override void StartRecordVideo()
  45. {
  46. if (!gIsStartRecord)
  47. {
  48. if (gIsPauseRecord)
  49. {
  50. gIsStartRecord = true;
  51. gIsPauseRecord = false;
  52. }
  53. else
  54. {
  55. // 设置输出文件名及格式为MP4
  56. string outputFilePath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(),"Camera",DateTime.Now.ToString("yyyyMMddHHmmss")+".avi");
  57. double fps = 30.0;
  58. gVideoWriter = new VideoWriter(outputFilePath, FourCC.MP43, fps, new OpenCvSharp.Size(gVideoCapture.FrameWidth, gVideoCapture.FrameHeight), true);
  59. //FourCC.MP43
  60. gIsStartRecord = true;
  61. gRecordThread = new Thread(Record);
  62. gRecordThread.Start();
  63. }
  64. }
  65. }
  66. public override void StopRecordVideo()
  67. {
  68. gIsStartRecord = false;
  69. gIsPauseRecord = false;
  70. }
  71. public override Bitmap TakePhoto()
  72. {
  73. Mat photo = new Mat();
  74. gVideoCapture.Read(photo);
  75. return BitmapConverter.ToBitmap(photo);
  76. }
  77. private void Record()
  78. {
  79. Mat frame = new Mat();
  80. while (true)
  81. {
  82. // 读取当前帧图像
  83. gVideoCapture.Read(frame);
  84. //把当前帧转成Bitmap,供前端页面显示
  85. CurrentDisplay = BitmapConverter.ToBitmap(frame);
  86. // 将图像显示在窗口中
  87. //Cv2.ImShow("Camera", frame);
  88. if (gIsStartRecord)
  89. {
  90. // 将当前帧写入输出文件
  91. gVideoWriter.Write(frame);
  92. }
  93. else if (!gIsPauseRecord) //如果没有开启录制,也不是暂停状态,则退出循环
  94. break;
  95. Thread.Sleep(10);
  96. }
  97. }
  98. }

界面代码

  1. public VideoForm()
  2. {
  3. InitializeComponent();
  4. }
  5. private void btn_Connect_Click(object sender, EventArgs e)
  6. {
  7. bool result=gUvc.Connect(0); //默认连接第一个摄像头
  8. if (result)
  9. ShowMsg("摄像头连接成功!");
  10. else
  11. ShowMsg("摄像头连接失败!");
  12. }
  13. private void ShowMsg(string msg)
  14. {
  15. System.Windows.Forms.MessageBox.Show(msg);
  16. }
  17. private void btn_Close_Click(object sender, EventArgs e)
  18. {
  19. gUvc.DisConnect();
  20. }
  21. private void btn_StartVideo_Click(object sender, EventArgs e)
  22. {
  23. timer_count.Enabled = true;
  24. gUvc.StartRecordVideo();
  25. }
  26. private void btn_PauseVideo_Click(object sender, EventArgs e)
  27. {
  28. timer_count.Enabled = false;
  29. gUvc.PauseRecordVideo();
  30. }
  31. private void btn_EndVideo_Click(object sender, EventArgs e)
  32. {
  33. timer_count.Enabled = false;
  34. tick_num = 0;
  35. gUvc.StopRecordVideo();
  36. }
  37. private void timer1_Tick(object sender, EventArgs e)
  38. {
  39. pictureBox1.Image = gUvc.CurrentDisplay;
  40. }
  41. private void btn_Capture_Click(object sender, EventArgs e)
  42. {
  43. pictureBoxPhoto.Image = gUvc.TakePhoto();
  44. }
  45. private void btn_Save_Click(object sender, EventArgs e)
  46. {
  47. if(pictureBoxPhoto.Image != null)
  48. {
  49. string path = Path.Combine(Directory.GetCurrentDirectory(), "Camera", DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png");
  50. pictureBoxPhoto.Image.Save(path);
  51. }
  52. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/650988
推荐阅读
相关标签
  

闽ICP备14008679号