安装
像下面这样操作就可以了
using OpenCvSharp;
using OpenCvSharp.Extensions;
注意文件夹名字不能叫做OpenCVsharp4和库重名就安装不上了
视频读取
实验现象
点一下截屏就开始截屏 再点一下就停止
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
namespace FeDetect
{
public partial class Form1 : UIForm
{
System.Timers.Timer DTimer;
public Form1()
{
InitializeComponent();
videoC.CameraInit();
}
private void Form1_Load(object sender, EventArgs e)
{
DTimer = new System.Timers.Timer(30);//实例化Timer类,设置间隔时间为10000毫秒;
DTimer.Elapsed += new System.Timers.ElapsedEventHandler(DataTimeHandle);//到达时间的时候执行事件;
DTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
DTimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
}
public void DataTimeHandle(object source, System.Timers.ElapsedEventArgs e)
{
try
{
DTimer.Enabled = false;
this.Invoke((EventHandler)delegate
{
Bitmap bmp;
if (true == videoC.CapturePic(out bmp))
{
pictureBox1.Image = bmp;
}
});
DTimer.Enabled = true;
}
catch (Exception ex)
{
DTimer.Enabled = false;
}
}
//截图按钮
private void uiButton2_Click(object sender, EventArgs e)
{
videoC.PicSaveFlag=!videoC.PicSaveFlag;//取反
}
private void uiButton3_Click(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.IO;
namespace FeDetect
{
class videoC
{
public static VideoCapture VideoObj;
static int ID=0;
static bool BmpFlag = false;
public static int curIndex = 0;//重开次数
public static Bitmap bitmap;
public static bool PicSaveFlag = false;//为真就截屏
static string Npath= @"C:\pic";
public static void CameraInit()
{
VideoObj = new VideoCapture(ID);
VideoObj.Open(ID);
if (!VideoObj.IsOpened())
{
MessageBox.Show("摄像头打开失败", "摄像头故障", MessageBoxButtons.OK, MessageBoxIcon.Error);//using System.Windows.Forms;
return;
}
double pos = VideoObj.Get(5);//获得帧率
// CamTimer.Enabled = true;
//APIClass.SaveLog(@"相机帧率:"+ pos.ToString(), MainPanelForm.LogFileName);
}
public static bool CapturePic(out Bitmap bmp)
{
try
{
Mat src = new Mat();
if (!VideoObj.IsOpened())
{
CamReOpen();
bmp = null;
return false;
}
else
{
if (true == VideoObj.Read(src))
{
if (src.Width == 0)
{
CamReOpen();
bmp = null;
return false;
}
// Cv2.ImShow("test", src);
bitmap = BitmapConverter.ToBitmap(src);
if (PicSaveFlag == true)
{
if (Directory.Exists(Npath) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(Npath);
}
if (PicSaveFlag = true)
{
//不能有空格:什么的 不然保存不了
Cv2.ImWrite("C:\\pic\\"+ DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg", src);
//src.SaveImage("C:\\pic\\" + DateTime.Now.ToString("yyyy-MM-dd-hhmmss") + ".jpg");//这两个效果都是一样的
//保存8张图片
}
}
bmp = bitmap;
return true;
}
else
{
CamReOpen();
bmp = null;
return false;
}
}
}
catch (Exception ex)
{
// MessageBox.Show("run_cap:" + ex.ToString());
bmp = null;
return false;
}
}
public static void CamReOpen()
{
VideoObj.Release();
VideoObj = new VideoCapture();
VideoObj.Open(ID);
}
//得到当前时间
static string GetCurTime()
{
System.DateTime currentTime = new System.DateTime();
currentTime = System.DateTime.Now;
string res;
res = (currentTime.Year).ToString() + "-" + (currentTime.Month < 10 ? ("0" + currentTime.Month) : currentTime.Month + "") + "-" + (currentTime.Day < 10 ? ("0" + currentTime.Day) : currentTime.Day + "").ToString()
+ " " + (currentTime.Hour < 10 ? ("0" + currentTime.Hour) : currentTime.Hour + "") + ":" + (currentTime.Minute < 10 ? ("0" + currentTime.Minute) : currentTime.Minute + "") + ":" + (currentTime.Second < 10 ? ("0" + currentTime.Second) : currentTime.Second + "");//+ "-" + (currentTime.Millisecond).ToString();
return res;
}
}
}
关键API
一些问题
分辨率设置
OpenCV直接读取的视频分辨率会被默认压缩成640×1080;
VideoObj.Release();
VideoObj = new VideoCapture();
VideoObj.Open(ID);
//设置之前需要重启一下,如果在使用过程中进行设置,是设置不了的
VideoObj.Set(3, 3840);//但是即时你设置的很大,他也只会取合适他的尺寸
VideoObj.Set(4, 2160);
double width = VideoObj.Get(3);//640 480 这两个就是OpenCV默认读取的尺寸大小
double height = VideoObj.Get(4);
APIClass.SaveLog(@"图像设置" + width.ToString() + ";" + height.ToString());