12. OpenCvSharp网络摄像头(rtsp、rtmp、http视频流或媒体流)的获取和使用
项目概述
项目实现了基于OpenCvSharp和WPF组件实现了摄像头或相机的读取,并在列表中显示出来并可以截图保存,还可以在画面中显示IP地址、绘制十字准星、ROI区域,还可对画面进行左右和上下的翻转,并且十字准星、ROI区域、左右翻转和上下翻转等功能支持关闭保存功能,还可以自动存储IP地址、端口号、用户名、密码等信息便于下次调用,最后提供了一些公网的rtsp、rtmp、http视频流或媒体流供大家测试。
源代码在本文底部
0基础原理
所实现程序的流程图如下:
1基础步骤和界面设计
1.1引用using OpenCvSharp;using OpenCvSharp.Extensions;
1.2将Picturebox、Label、Button、checkbox等控件进行布局、改名、调整形状和字体,形成如下界面:
2功能实现
2.1初始化变量
private String Url = "";//网络链接(uniform resource location,Url)
private String IPaddress="";//IP地址
private String IPport = "";//端口号
private String Username = "";//用户名
private String Password = "";//密码
private static VideoCapture my_VideoCapture;//摄像头设备
private static bool Vopen_flag; //视频打开关闭状态
private Thread Video_thread; //视频播放线程
private bool Collimator_flag ;//十字准星标识符
private bool ROI_flag ;//ROI区域标识符
private bool FlipX_flag;//左右翻转标识符
private bool FlipY_flag;//上下翻转标识符
private bool IP_flag;//IP显示标识符
2.2初始化信息的读取
private void Cam_mes_load()//初始化信息读取
{
//初始化十字准星标识符
Collimator_flag = Properties.Settings.Default.Collimator_flag;
if (Collimator_flag) checkBox1.Checked = true;
else checkBox1.Checked = false;
//初始化ROI区域标识符
ROI_flag = Properties.Settings.Default.ROI_flag;
if (ROI_flag) checkBox2.Checked = true;
else checkBox2.Checked = false;
//初始化十字准星标识符
FlipX_flag = Properties.Settings.Default.FlipX_flag;
if (FlipX_flag) checkBox3.Checked = true;
else checkBox3.Checked = false;
//初始化ROI区域标识符
FlipY_flag = Properties.Settings.Default.FlipY_flag;
if (FlipY_flag) checkBox4.Checked = true;
else checkBox4.Checked = false;
IPaddress_textBox.Text= Properties.Settings.Default.IPaddress;//文本框获取IP地址
IPport_textBox.Text= Properties.Settings.Default.IPport;//文本框获取端口
Username_textBox.Text= Properties.Settings.Default.Username;//文本框获取用户名
Password_textBox.Text= Properties.Settings.Default.Password;//文本框获取密码
}
2.3 视频流的开启
private void Play_Video()
{
while (Vopen_flag)
{
Mat v_mat = new Mat();
my_VideoCapture.Read(v_mat);
int sleepTime = (int)Math.Round(1000 / my_VideoCapture.Fps);
Cv2.WaitKey(sleepTime);
if (v_mat.Empty())
{
break;
}
if (FlipY_flag) Cv2.Flip(v_mat, v_mat, OpenCvSharp.FlipMode.X); //上下翻转
if (FlipX_flag) Cv2.Flip(v_mat, v_mat, OpenCvSharp.FlipMode.Y); //左右翻转
if (Collimator_flag) //画准星
{
Cv2.Line(v_mat, new OpenCvSharp.Point(v_mat.Cols / 2, v_mat.Rows / 2 + 35), new OpenCvSharp.Point(v_mat.Cols / 2, v_mat.Rows / 2 + 15), new Scalar(0, 0, 255), 2, LineTypes.Link8);
Cv2.Line(v_mat, new OpenCvSharp.Point(v_mat.Cols / 2, v_mat.Rows / 2 - 35), new OpenCvSharp.Point(v_mat.Cols / 2, v_mat.Rows / 2 - 15), new Scalar(0, 0, 255), 2, LineTypes.Link8);
Cv2.Line(v_mat, new OpenCvSharp.Point(v_mat.Cols / 2 + 35, v_mat.Rows / 2), new OpenCvSharp.Point(v_mat.Cols / 2 + 15, v_mat.Rows / 2), new Scalar(0, 0, 255), 2, LineTypes.Link8);
Cv2.Line(v_mat, new OpenCvSharp.Point(v_mat.Cols / 2 - 35, v_mat.Rows / 2), new OpenCvSharp.Point(v_mat.Cols / 2 - 15, v_mat.Rows / 2), new Scalar(0, 0, 255), 2, LineTypes.Link8);
}
if (ROI_flag) //画ROI
{
int V_width = v_mat.Width;
int V_height = v_mat.Height;
int start_x = v_mat.Width / 4;
int start_y = v_mat.Height / 4;
OpenCvSharp.Point strat_point = new OpenCvSharp.Point(v_mat.Width / 2, v_mat.Height / 2);
Rect ROI_rect = new Rect(start_x, start_y, V_width / 2, V_height / 2);
Mat srcImg = new Mat(v_mat, ROI_rect).Clone();
Cv2.Rectangle(v_mat, ROI_rect, new Scalar(255, 255, 0), 2);
}
if (IP_flag) //显示IP地址
{
Scalar color = new Scalar(100, 100, 100);
Cv2.PutText(v_mat, IPaddress, new OpenCvSharp.Point(0, 100), HersheyFonts.Italic, 1, color, 2, LineTypes.AntiAlias, false);//绘制文本
}
this.pictureBox1.Invoke(new Action(() =>
{
pictureBox1.Image = BitmapConverter.ToBitmap(v_mat);//在 pictureBox1上显示
}));
v_mat.Release();//释放内存
}
}
2.4截图功能
private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "图片保存";
saveImageDialog.Filter = "jpg图片|*.JPG|gif图片|*.GIF|png图片|*.PNG|jpeg图片|*.JPEG|BMP图片|*.BMP";//文件类型过滤,只可选择图片的类型
saveImageDialog.FilterIndex = 1;//设置默认文件类型显示顺序
saveImageDialog.FileName = "图片保存"; //设置默认文件名,可为空
saveImageDialog.RestoreDirectory = true; //OpenFileDialog与SaveFileDialog都有RestoreDirectory属性,这个属性默认是false,打开一个文件后,那么系统默认目录就会指向刚才打开的文件。如果设为true就会使用系统默认目录
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{
string fileName = saveImageDialog.FileName.ToString();
if (fileName != "" && fileName != null)
{
string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
System.Drawing.Imaging.ImageFormat imgformat = null;
if (fileExtName != "")
{
switch (fileExtName)
{
case "jpg":
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "png":
imgformat = System.Drawing.Imaging.ImageFormat.Png;
break;
case "gif":
imgformat = System.Drawing.Imaging.ImageFormat.Gif;
break;
case "bmp":
imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
default:
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
}
try
{
MessageBox.Show("保存路径:" + fileName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
pictureBox1.Image.Save(fileName, imgformat);
}
catch
{
MessageBox.Show("图片保存失败!");
}
}
}
}
}
2.5密码保护的设置
在密码TextBox属性中的PasswordChar输入“*”号
视频流测试地址:
伊拉克 Al Sharqiya 电视台:
地址:rtmp://ns8.indexforce.com/home/mystream
韩国GOOD TV:
地址:rtmp://mobliestream.c3tv.com:554/live/goodtv.sdp
苹果计时器:
地址:http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8
苹果广告:
地址:https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1920x1080h.mp4