using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PaintDotNet.Base.SettingModel;
using TUCamera;
namespace PaintDotNet.ImageCollect.CameraComponent
{
public partial class AdjustExposureControl : UserControl
{
private TUCamera.TUCamera m_camera;
private CameraParamModel m_cameraParamModel;
private System.Timers.Timer m_aeTimer;
private bool m_use;
private bool m_autoExposureOnce;
private bool m_autoExposureComplete;
private void InitializeLanguageText()
{
this.groupBox1.Text = PdnResources.GetString("Menu.timeofexposure.text");
this.checkBoxAutoExposure.Text = PdnResources.GetString("Menu.auto-exposure.text");
this.label6.Text = PdnResources.GetString("Menu.Exposurepercentage.text") + ":";
this.label5.Text = PdnResources.GetString("Menu.timeofexposure.text") + ":";
this.btnAutoExposureOnce.Text = PdnResources.GetString("Menu.Oneautomaticexposure.Text");
}
public AdjustExposureControl(CameraParamModel model, bool use)
{
m_use = use;
m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
m_cameraParamModel = model;
m_aeTimer = new System.Timers.Timer(1000);
m_aeTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimerAutoExposure);
m_aeTimer.AutoReset = true;
m_aeTimer.SynchronizingObject = this;
m_aeTimer.Start();
InitializeComponent();
InitializeLanguageText();
InitializeControlData();
}
public void ReLoad(CameraParamModel model, bool use)
{
m_cameraParamModel = model;
m_use = use;
UpdateConfigMode();
}
private void InitializeControlData()
{
if (m_camera.IsOpen())
{
// 曝光时间
double aeMinVal = 0;
double aeMaxVal = 0;
double aeDftVal = 0;
m_camera.GetExposureTimeRange(ref aeMinVal, ref aeMaxVal, ref aeDftVal);
SetExposureBarRange(aeMinVal, aeMaxVal);
lblBaoGuangMinVal.Text = ((int)aeMinVal).ToString() + "μs";
lblBaoGuangMaxVal.Text = UpdateExposureTime((UInt64)aeMaxVal);
}
}
private void UpdateConfigMode()
{
// 曝光时间
int value = (int)(m_cameraParamModel.parame.LNExposure);
SetExposureBarValue(value);
DisplayExposureText(value);
// 曝光百分比
int preExposure = m_cameraParamModel.parame.Light;
preExposure = preExposure < 0 ? 0 : preExposure > 255 ? 255 : preExposure;
lightTBar.Value = preExposure;
LightBox.Text = preExposure.ToString();
UpdateExposureUI(m_cameraParamModel.parame.ATExposure);
}
///
/// 1:自动 ,0:手动
///
private void UpdateExposureUI(int autoExposure)
{
ExposureTBar.Enabled = autoExposure != 1;
ExposureBox.Enabled = autoExposure != 1;
checkBoxAutoExposure.Checked = autoExposure == 1;
m_camera.SetExposureMode((ExposureMode)autoExposure);
m_cameraParamModel.parame.ATExposure = autoExposure;
}
double _log = 1.1;
int _min;
int _max;
private int GetExposureBarValue()
{
var value = ExposureTBar.Value;
if (ExposureTBar.Value == ExposureTBar.Minimum) return _min;
if (ExposureTBar.Value == ExposureTBar.Maximum) return _max;
return (int)Math.Pow(_log, value);
}
private void SetExposureBarValue(double value)
{
var ival = (int)Math.Log(Math.Max(_min, value), _log);
ExposureTBar.Value = Math.Min(ival, ExposureTBar.Maximum);
}
private void SetExposureBarRange(double min, double max)
{
_min = (int)min;
ExposureTBar.Minimum = (int)Math.Log(min, _log);
_max = (int)max;
ExposureTBar.Maximum = (int)Math.Log(max, _log); ;
}
public void OnTimerAutoExposure(object source, System.Timers.ElapsedEventArgs e)
{
if (m_autoExposureOnce)
{
checkBoxAutoExposure.Checked = false;
m_autoExposureOnce = false;
UpdateExposureUI(0);
// checkBoxAutoExposure_CheckedChanged(null, null);
}
else if (!checkBoxAutoExposure.Checked)
{
return;
}
double us = m_camera.GetExposureTime() * 1000;
SetExposureBarValue(us);
DisplayExposureText((int)us);
}
///
/// 曝光百分比滑块滑动事件
///
///
///
private void Light_ValueChanged(object sender, System.EventArgs e)
{
m_camera.Light = lightTBar.Value;
m_cameraParamModel.parame.Light = lightTBar.Value;
LightBox.Text = lightTBar.Value + "";
}
///
/// 曝光时间滑块滑动事件
///
///
///
private void Exposure_ValueChanged(object sender, System.EventArgs e)
{
int exposureTimeUSec = GetExposureBarValue();
this.m_cameraParamModel.parame.LNExposure = exposureTimeUSec;
DisplayExposureText(exposureTimeUSec);
m_camera.SetExposureTime((ulong)(exposureTimeUSec));
}
private void checkBoxAutoExposure_CheckedChanged(object sender, EventArgs e)
{
int autoExposure = checkBoxAutoExposure.Checked ? 1 : 0;
UpdateExposureUI(autoExposure);
if (autoExposure == 0)
{
Light_ValueChanged(null, null);
}
else
{
m_autoExposureOnce = false;
}
}
///
/// 一次自动曝光
///
///
///
private void btnAutoExposureOnce_Click(object sender, EventArgs e)
{
if (m_autoExposureComplete && checkBoxAutoExposure.Checked)
{
checkBoxAutoExposure.Checked = false;
}
else
{
if (checkBoxAutoExposure.Checked)
{
checkBoxAutoExposure.Checked = false;
}
m_autoExposureOnce = true;
UpdateExposureUI(1);
}
}
///
/// 曝光时间内容输入后校验
///
///
///
private void baoGuangTBox_KeyPress(object sender, KeyPressEventArgs e)
{
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(ExposureBox.Text);
switch (unitLabel.Text)
{
case "s":
value = value * 1000 * 1000;
break;
case "ms":
value = value * 1000;
break;
}
SetExposureBarValue(value);
Exposure_ValueChanged(null, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void DisplayExposureText(int exposureTimeUSec)
{
decimal txtVale = 0;
int sec = 0;
int msec = 0;
int usec = 0;
m_camera.UpdateExposureTime(ref sec, ref msec, ref usec, (UInt64)exposureTimeUSec);
if (sec > 0)
{
this.unitLabel.Text = "s";
txtVale += sec;
}
if (msec > 0)
{
if (txtVale > 0)
{
txtVale += ((decimal)msec / 1000);
this.unitLabel.Text = "s";
usec = 0; // 微秒舍掉不显示了
}
else
{
this.unitLabel.Text = "ms";
txtVale += msec;
}
}
if (usec > 0)
{
if (txtVale > 0)
{
txtVale += ((decimal)usec / 1000);
}
else
{
this.unitLabel.Text = "μs";
txtVale = usec;
}
}
this.ExposureBox.Text = txtVale.ToString();
}
private string UpdateExposureTime(UInt64 exposureTime)
{
string str = "";
int sec = 0;
int msec = 0;
int usec = 0;
m_camera.UpdateExposureTime(ref sec, ref msec, ref usec, exposureTime);
if (sec > 0)
{
str += sec + "s";
}
else if (msec > 0)
{
str += msec + "ms";
}
else if (usec > 0)
{
str += usec + "μs";
}
return str;
}
}
}