C#显示摄像头内容,OpenCvSharp4
OpenCvSharp4
·
刚学习C#记录一下。
1、下载NuGet程序包,OpenCvSharp4, OpenCvSharp4.Extensions, OpenCvSharp4.runtime.win。
2、然后就可以愉快的图片处理了。
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.IO;
using FFmpeg.AutoGen;
using System.Threading.Tasks;
using Add;
using System.Drawing;
namespace WinFormsApp1
{
public unsafe sealed partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
capture_pic();
on_screen();
}
LinkedList<Bitmap> list_bit = new LinkedList<Bitmap>();
int pic_flag = 1;
private void capture_pic()
{
VideoCapture video = new VideoCapture();
video.Open(0); //打开摄像头
if (!video.IsOpened())
{
MessageBox.Show("无法打开摄像头!!!", "提示:");
}
else
{
Task task = new Task(() =>
{
while (true)
{
if (pic_flag == 1)
{
Mat mat = new Mat();
Mat newMat = new Mat();
bool bSuccess = video.Read(mat);
if (!bSuccess)
{
MessageBox.Show("无法读取摄像头的帧!!!", "提示:");
}
else
{
Cv2.Resize(mat, newMat, new OpenCvSharp.Size(pictureBox1.Width, pictureBox1.Height));
list_bit.AddLast(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(newMat));
}
Thread.Sleep(100);
}
}
});
task.Start();
}
}
private void on_screen()
{
Task taskShow = new Task(() =>
{
while (true)
{
if (pic_flag == 1)
{
if (list_bit.Count == 0)
continue;
Bitmap bit = list_bit.First();
Invoke(new Action(() =>
{
pictureBox1.Image = bit;
pictureBox1.Show();
pictureBox1.Refresh();
}));
list_bit.RemoveFirst();
}
}
});
taskShow.Start();
}
private void btn_start_Click(object sender, EventArgs e)
{
string pic_path = @"a.jpg";
pic_flag = 1;
/* Mat mat = new Mat("a.jpg", ImreadModes.Color);
Mat newMat = new Mat();
Cv2.Resize(mat, newMat, new OpenCvSharp.Size(pictureBox1.Width, pictureBox1.Height));
Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(newMat);
Cv2.ImShow("src", mat);
Cv2.WaitKey(0);
Invoke(new Action(()=>
{
pictureBox1.Image = bitmap;
pictureBox1.Show();
pictureBox1.Refresh();
}));*/
}
private void btn_end_Click(object sender, EventArgs e)
{
pic_flag = 0;
}
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)