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 PaintDotNet.Camera;
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 CameraParamModel.ParameterSets Settings
{
get
{
return GetInstance().m_currentCameraParamModel.parame;
}
}
public static CameraConfigs GetInstance()
{
if (m_cameraConfig == null)
{
m_cameraConfig = new CameraConfigs();
try
{
var name = Startup.instance.configModel.CameraConfig;
m_cameraConfig.m_currentConfigFileName = name == null ? "CameraParam" : name;
m_cameraConfig.m_currentCameraParamModel = m_cameraConfig.GetCameraParamModel(m_cameraConfig.m_currentConfigFileName);
}
catch
{
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()
{
ICamera m_camera = CameraManager.CurrentCamera;
int id = m_camera.ResolutionId;
// 分辨率
if (id != m_currentCameraParamModel.parame.Resolution)
{
m_camera.ResolutionId = m_currentCameraParamModel.parame.Resolution;
}
//相机旋转
var rotate = m_currentCameraParamModel.parame.Rotate;
m_camera.Rotate = rotate;
m_camera.SetROI(m_currentCameraParamModel.parame.previewRectX, m_currentCameraParamModel.parame.previewRectY, m_currentCameraParamModel.parame.previewRectW, m_currentCameraParamModel.parame.previewRectH);
// 曝光
int autoExposure = m_currentCameraParamModel.parame.ATExposure;
if (autoExposure == 1)
{
m_camera.AutoExposure = 1;
}
else
{
m_camera.AutoExposure = 0;
m_camera.ExposureTime = m_currentCameraParamModel.parame.LNExposure;
}
m_camera.Target = m_currentCameraParamModel.parame.Brightness;
m_camera.Light = m_currentCameraParamModel.parame.Light;
//白平衡
int whiteBalance = m_currentCameraParamModel.parame.WhiteBalance;
if (whiteBalance == 1)
{
// 自动白平衡
m_camera.WhiteBalanceMode = 2;
}
else
{
// 手动白平衡
m_camera.WhiteBalanceMode = 0;
m_camera.RedGain = m_currentCameraParamModel.parame.RedChannel; //usec
m_camera.GreenGain = m_currentCameraParamModel.parame.GreenChannel; //usec
m_camera.BlueGain = m_currentCameraParamModel.parame.BlueChannel; //usec
// 全局增益
m_camera.Gain = m_currentCameraParamModel.parame.GlobalGain; //usec
}
// 色彩饱和度
m_camera.Saturation = m_currentCameraParamModel.parame.Saturation; //usec
// 相机模式
int monochromatic = m_currentCameraParamModel.parame.Monochromatic;
//锐化
m_camera.Sharpness = m_currentCameraParamModel.parame.Sharpness;
m_camera.HorizontalMirrored = m_currentCameraParamModel.parame.Horizontal == 1 ^ rotate == 2;
m_camera.VerticalMirrored = m_currentCameraParamModel.parame.Vertical == 1 ^ rotate == 2;
m_camera.GrayMode = monochromatic; //usec
}
///
/// 读取
///
///
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()
{
Save(m_currentCameraParamModel, m_currentConfigFileName);
}
public void SaveAs(string fileName)
{
Save(m_currentCameraParamModel, fileName);
}
public void Delete(string name)
{
string fileFullPath = GetFileFullPath(name);
System.IO.File.Delete(fileFullPath);
}
}
}