123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- 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);
- }
- /// <summary>
- /// 1:自动 ,0:手动
- /// </summary>
- 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);
- }
- /// <summary>
- /// 曝光百分比滑块滑动事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Light_ValueChanged(object sender, System.EventArgs e)
- {
- m_camera.Light = lightTBar.Value;
- m_cameraParamModel.parame.Light = lightTBar.Value;
- LightBox.Text = lightTBar.Value + "";
- }
- /// <summary>
- /// 曝光时间滑块滑动事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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;
- }
- }
- /// <summary>
- /// 一次自动曝光
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// 曝光时间内容输入后校验
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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;
- }
- }
- }
|