LuJiaYi/CamManager.cs
2024-08-17 18:00:59 +08:00

225 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using GuideGraspSys;
using picEx;
using OpenCvSharp;
using WindowsFormsApp1;
public class CameraManager
{
private MGSCameraDriver cam1;
private MGSCameraDriver cam2;
private MGSCameraDriver cam3;
private MGSCameraDriver cam4;
private readonly Dictionary<HikCamDriver, string> cameraPaths = new Dictionary<HikCamDriver, string>();
public CameraManager()
{
cam1 = new MGSCameraDriver();
cam2 = new MGSCameraDriver();
cam3 = new MGSCameraDriver();
cam4 = new MGSCameraDriver();
}
public void StartAllCameras()
{
// Create and start threads for each camera
StartCamera(cam1, "Cam1");
StartCamera(cam2, "Cam2");
StartCamera(cam3, "Cam3");
StartCamera(cam4, "Cam4");
}
private void StartCamera(MGSCameraDriver camera, string cameraName)
{
if (cameraName == "Cam1")
{
camera.Start("Cam1");
if (cam1.IfSuccess)
{
}
else { }
camera.OnHImageOutput -= OnCameraMatOutPut1;
camera.OnHImageOutput += OnCameraMatOutPut1;
}
else if(cameraName == "Cam2")
{
camera.Start("Cam2");
if (cam1.IfSuccess)
{
}
else { }
camera.OnHImageOutput -= OnCameraMatOutPut2;
camera.OnHImageOutput += OnCameraMatOutPut2;
}
else if (cameraName == "Cam3")
{
camera.Start("Cam3");
if (cam1.IfSuccess)
{
}
else { }
camera.OnHImageOutput -= OnCameraMatOutPut3;
camera.OnHImageOutput += OnCameraMatOutPut3;
}
else if (cameraName == "Cam4")
{
camera.Start("Cam4");
if (cam1.IfSuccess)
{
}
else { }
camera.OnHImageOutput -= OnCameraMatOutPut4;
camera.OnHImageOutput += OnCameraMatOutPut4;
}
}
private void OnCameraMatOutPut1(DateTime dt, Mat cameraMat)
{
// Save the image or process it
string path = "D:\\Lujiayi\\Cam1";
int num = CountPhotos(path)+1;
if (path == null)
{
return;
}
// Ensure the directory exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// Save the image with a unique filename
string nameCam = $"{path}\\{num}.jpg";
cameraMat.ImWrite(nameCam);
}
private void OnCameraMatOutPut2(DateTime dt, Mat cameraMat)
{
// Save the image or process it
string path = "D:\\Lujiayi\\Cam2";
int num = CountPhotos(path) + 1;
if (path == null)
{
return;
}
// Ensure the directory exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// Save the image with a unique filename
string nameCam = $"{path}\\{num}.jpg";
cameraMat.ImWrite(nameCam);
}
private void OnCameraMatOutPut3(DateTime dt, Mat cameraMat)
{
// Save the image or process it
string path = "D:\\Lujiayi\\Cam3";
int num = CountPhotos(path);
if (path == null)
{
return;
}
// Ensure the directory exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// Save the image with a unique filename
string nameCam = $"{path}\\{num}.jpg";
cameraMat.ImWrite(nameCam);
}
private void OnCameraMatOutPut4(DateTime dt, Mat cameraMat)
{
// Save the image or process it
string path = "D:\\Lujiayi\\Cam4";
int num = CountPhotos(path);
if (path == null)
{
return;
}
// Ensure the directory exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// Save the image with a unique filename
string nameCam = $"{path}\\{num}.jpg";
cameraMat.ImWrite(nameCam);
}
public void takepic1()
{
cam1.Snapshot();
}
public void takepic2()
{
cam2.Snapshot();
}
public void takepic3()
{
cam3.Snapshot();
}
public void takepic4()
{
cam4.Snapshot();
}
public static int CountPhotos(string folderPath)
{
// 图片文件扩展名列表
string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".bmp", ".gif" };
// 获取所有图片文件
var photoFiles = Directory.GetFiles(folderPath)
.Where(file => imageExtensions.Contains(Path.GetExtension(file).ToLower()))
.ToArray();
return photoFiles.Length;
}
}