using PaintDotNet.Base.SettingModel;
using PaintDotNet.Camera;
using System;
using System.Threading;
using System.Windows.Forms;
namespace PaintDotNet.ImageCollect
{
public partial class AdjustExposureControl : UserControl
{
private ICamera m_camera;
private CameraParamModel.ParameterSets _settings;
private void InitializeLanguageText()
{
this.cmpLight.Caption = PdnResources.GetString("Menu.Exposurepercentage.text");
this.lblExp.Text = PdnResources.GetString("Menu.timeofexposure.text");
this.groupBox1.Text = PdnResources.GetString("Menu.timeofexposure.text");
this.ckbAutoExposure.Text = PdnResources.GetString("Menu.auto-exposure.text");
this.btnAutoExposureOnce.Text = PdnResources.GetString("Menu.Oneautomaticexposure.Text");
this.groupBox2.Text = PdnResources.GetString("Menu.Gainvalueadjustment.text");
this.cmpGain.Caption = PdnResources.GetString("Menu.imagecapture.adjust.Gain.text");
}
public AdjustExposureControl(int mode = 3)
{
InitializeComponent();
var height = 8;
if ((mode & 1) > 0)
{
height += groupBox1.Height;
}
else
{
groupBox1.Hide();
}
if ((mode & 2) > 0)
{
height += groupBox2.Height;
}
else
{
groupBox2.Hide();
}
Height = height;
}
public void Initialize(CameraParamModel model)
{
InitializeLanguageText();
m_camera = CameraManager.CurrentCamera;
_settings = model.parame;
if (m_camera.IsOpen())
{
ckbAutoExposure.Checked = m_camera.AutoExposure == 1;// _settings.ATExposure == 1;
// 曝光时间
var range = m_camera.GetExposureTimeRange();
SetExposureBarRange(range.Min, range.Max);
SetExposureBarValue(m_camera.ExposureTime);
txbExposure.Text = m_camera.ExposureTime.ToString("f3");
// 亮度
range = m_camera.GetLightRange();
cmpLight.Max = (int)range.Max;
cmpLight.Min = (int)range.Min;
cmpLight.OnValueChange += CmpLight_OnValueChange;
cmpLight.Tag = range;
cmpLight.Value = m_camera.Light;
//增益
range = m_camera.GetGlobalGainRange();
cmpGain.Max = (int)range.Max;
cmpGain.Min = (int)range.Min;
cmpGain.OnValueChange += CmpGain_OnValueChange;
_settings = model.parame;
if (ckbAutoExposure.Checked)
{
SetAutoExposure();
}
else
{
SetFixedExposure();
}
}
}
private void CmpGain_OnValueChange(double obj)
{
m_camera.Gain = (int)obj;
_settings.GlobalGain = (int)obj;
}
private void CmpLight_OnValueChange(double obj)
{
var value = (int)obj;
_settings.GlobalGain = value;
m_camera.Light = value;
}
public void OnTimerAutoExposure()
{
cmpGain.Value = m_camera.Gain;
txbExposure.Text = m_camera.ExposureTime.ToString("f3");
SetExposureBarValue(m_camera.ExposureTime);
}
private void ckbAutoExposure_Click(object sender, EventArgs e)
{
var isAuto = ckbAutoExposure.Checked;
if (isAuto)
SetAutoExposure();
else
SetFixedExposure();
}
private void btnAutoExposureOnce_Click(object sender, EventArgs e)
{
SetAutoExposure();
ckbAutoExposure.Checked = false;
new Thread(() =>
{
Thread.Sleep(1000);
SetFixedExposure();
this.Invoke(new Action(OnTimerAutoExposure));
}).Start();
}
void SetAutoExposure()
{
_settings.ATExposure = 1;
m_camera.AutoExposure = 1;
}
void SetFixedExposure()
{
_settings.ATExposure = 0;
m_camera.AutoExposure = 0;
}
///
/// 刷新控件状态
///
public void UpdateDisplay()
{
var isAuto = ckbAutoExposure.Checked;
panel1.Enabled = !isAuto;
cmpGain.Enabled = !isAuto;
if (isAuto)//固定曝光
{
OnTimerAutoExposure();
}
}
double _log = 1.1;
int _min;
int _max;
private void SetExposureBarRange(double min, double max)
{
_min = (int)min;
tkbExposure.Minimum = (int)Math.Log(min, _log);
_max = (int)max;
tkbExposure.Maximum = (int)Math.Log(max, _log); ;
}
private void tkbExposure_Scroll(object sender, EventArgs e)
{
double value = tkbExposure.Value;
if (tkbExposure.Value == tkbExposure.Minimum) value = _min;
if (tkbExposure.Value == tkbExposure.Maximum) value = _max;
value = Math.Pow(_log, value);
value = Math.Round(value, 3);
txbExposure.Text = value.ToString();
_settings.LNExposure = value;
m_camera.ExposureTime = value;
}
private void SetExposureBarValue(double value)
{
if (value == 0) return;
var ival = (int)Math.Log(Math.Max(_min, value), _log);
tkbExposure.Value = Math.Max(tkbExposure.Minimum, Math.Min(ival, tkbExposure.Maximum));
}
///
/// 曝光时间内容输入后校验
///
///
///
private void baoGuangTBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Delete)
{
if (txbExposure.Text.Contains("."))
e.Handled = true;
}
else if (!char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Back)) // 非数字键, 放弃该输入
{
e.Handled = true;
return;
}
if (e.KeyChar == (char)Keys.Enter)
{
try
{
double value = Convert.ToDouble(txbExposure.Text);
SetExposureBarValue(value);
_settings.LNExposure = value;
m_camera.ExposureTime = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}