using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using PaintDotNet.Base.CommTool; using PaintDotNet.Base.SettingModel; using TUCAMAPI; using TUCamera; namespace PaintDotNet.ImageCollect { public class CameraConfigs { private string m_filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\CameraParam\\"; private static CameraConfigs m_cameraConfig = null; private CameraParamModel m_currentCameraParamModel = null; private string m_currentConfigFileName = null; private CameraConfigs() { if (!System.IO.Directory.Exists(m_filePath)) { System.IO.Directory.CreateDirectory(m_filePath);//不存在就创建目录 } } public static CameraConfigs GetInstance() { if (m_cameraConfig == null) { m_cameraConfig = new CameraConfigs(); if (m_cameraConfig.m_currentConfigFileName == null) { m_cameraConfig.m_currentConfigFileName = "CameraParam"; m_cameraConfig.m_currentCameraParamModel = m_cameraConfig.GetCameraParamModel(m_cameraConfig.m_currentConfigFileName); } } return m_cameraConfig; } public string FilePath { get { return this.m_filePath; } } public string CurrentConfigFileName { get { return this.m_currentConfigFileName; } } /// /// 获取所有相机配置文件名称 /// /// public List GetAllConfigFiles() { List configs = new List(); DirectoryInfo TheFolder = new DirectoryInfo(m_filePath); foreach (FileInfo NextFile in TheFolder.GetFiles()) { configs.Add(NextFile.Name.Replace(".xml", "")); } return configs; } public Boolean FileExist(string fileName) { DirectoryInfo TheFolder = new DirectoryInfo(m_filePath); foreach (FileInfo NextFile in TheFolder.GetFiles()) { if (NextFile.Name.ToLower().Equals(fileName.ToLower() + ".xml")) { return true; } } return false; } /// /// 获取配置文件的完整路径 /// /// 文件名 /// public string GetFileFullPath(string fileName) { string filePath = this.m_filePath; if (fileName.ToLower().Contains(".xml")) { filePath += fileName.ToLower(); } else { filePath += fileName + ".xml"; } return filePath; } /// /// 读取配置参数 /// /// 配置文件名称 /// public CameraParamModel GetCameraParamModel(string fileName) { try { string filePath = GetFileFullPath(fileName); m_currentConfigFileName = fileName; m_currentCameraParamModel = XmlSerializeHelper.DESerializer(FileOperationHelper.ReadStringFromFile(filePath, FileMode.Open)); } catch (Exception) { m_currentCameraParamModel = XmlSerializeHelper.DESerializer(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\CameraParam.xml", FileMode.Open)); } return m_currentCameraParamModel; } public void CameraParamInit() { TUCamera.TUCamera m_camera = TUCameraManager.GetInstance().GetCurrentCamera(); int id = m_camera.GetResolutionId(); // 分辨率 if (id != m_currentCameraParamModel.parame.Resolution) { m_camera.SetResolution(m_currentCameraParamModel.parame.Resolution); } //相机旋转 var rotate = m_currentCameraParamModel.parame.Rotate; if (rotate == 0 || rotate == 2) m_camera.CancelRotate(); if (rotate == 1) { m_camera.SetRotateL90(); } if (rotate == 3) { m_camera.SetRotateR90(); } m_camera.SetROI(ref m_currentCameraParamModel.parame.previewRectX, ref m_currentCameraParamModel.parame.previewRectY, ref m_currentCameraParamModel.parame.previewRectW, ref m_currentCameraParamModel.parame.previewRectH); // 曝光 int autoExposure = m_currentCameraParamModel.parame.ATExposure; if (autoExposure == 1) { m_camera.SetExposureMode(ExposureMode.AUTO); m_camera.Light = m_currentCameraParamModel.parame.Light; } else { m_camera.SetExposureMode(ExposureMode.MANUAL); } //白平衡 int whiteBalance = m_currentCameraParamModel.parame.WhiteBalance; if (whiteBalance == 1) { // 自动白平衡 m_camera.SetWhiteBalanceMode(WhiteBalanceMode.AUTO); } else { // 手动白平衡 m_camera.SetWhiteBalanceMode(WhiteBalanceMode.MANUAL); if (m_currentCameraParamModel.parame.FMExposure == 1) { m_camera.SetColorTemperatureByString("3200K"); } else if (m_currentCameraParamModel.parame.FMExposure == 2) { m_camera.SetColorTemperatureByString("5500K"); } else { m_camera.SetRedGain(m_currentCameraParamModel.parame.RedChannel); //usec m_camera.SetGreeGain(m_currentCameraParamModel.parame.GreenChannel); //usec m_camera.SetBlueGain(m_currentCameraParamModel.parame.BlueChannel); //usec } // 全局增益 m_camera.SetGlobalGain(m_currentCameraParamModel.parame.GlobalGain); //usec } // 色彩饱和度 m_camera.SetSaturation(m_currentCameraParamModel.parame.Saturation); //usec // 相机模式 int monochromatic = m_currentCameraParamModel.parame.Monochromatic; if (monochromatic == 1) { m_camera.SetCameraMode(CameraMode.BLACK_WHILE); //usec } else { m_camera.SetCameraMode(CameraMode.COLOR); //usec } //锐化 m_camera.SetSharpness(m_currentCameraParamModel.parame.Sharpness); m_camera.HorizontalMirrored = m_currentCameraParamModel.parame.Horizontal == 1 ^ rotate == 2; m_camera.VerticalMirrored = m_currentCameraParamModel.parame.Vertical == 1 ^ rotate == 2; } /// /// 读取 /// /// public CameraParamModel GetCurrentCameraParamModel() { return GetCameraParamModel(m_currentConfigFileName); } public CameraParamModel GetDefaultCameraParamModel() { return GetCameraParamModel("CameraParam"); } public void SetCameraParamModel(CameraParamModel cameraParam) { m_currentCameraParamModel = cameraParam; } /// /// 保存配置文件 /// /// /// public void Save(CameraParamModel cameraParamModel, string fileName) { string stageModelXml = XmlSerializeHelper.XmlSerialize(cameraParamModel); string filePath = GetFileFullPath(fileName); FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create); } /// /// 保存配置文件 /// /// /// public void Save(CameraParamModel cameraParamModel) { string stageModelXml = XmlSerializeHelper.XmlSerialize(cameraParamModel); string filePath = GetFileFullPath(m_currentConfigFileName); FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create); } public void Delete() { string fileFullPath = GetFileFullPath(m_currentConfigFileName); System.IO.File.Delete(fileFullPath); } } }