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;
using System.Threading;
namespace PaintDotNet.ImageCollect.CameraComponent
{
public partial class AdjustWhiteBalanceControl : UserControl
{
private TUCamera.TUCamera m_camera;
private CameraParamModel m_cameraParamModel;
private bool m_use;
private void InitializeLanguageText()
{
this.groupBox3.Text = PdnResources.GetString("Menu.Image.WhiteBalance.Text");
this.button1.Text = PdnResources.GetString("Menu.Primarywhitebalance.Text");
this.label12.Text = PdnResources.GetString("Menu.Blue.text");
this.label11.Text = PdnResources.GetString("Menu.green.text");
this.label10.Text = PdnResources.GetString("Menu.red.text");
this.label9.Text = PdnResources.GetString("Menu.yellow.text");
this.label8.Text = PdnResources.GetString("Menu.Magenta.text");
this.label7.Text = PdnResources.GetString("Menu.aqua.text");
this.ColourCheckBox.Text = PdnResources.GetString("Menu.Displaycolorvaluechannel.text");
this.button3.Text = PdnResources.GetString("Menu.auto.text");
this.button2.Text = PdnResources.GetString("Menu.Manual.text");
}
public AdjustWhiteBalanceControl(CameraParamModel model, bool use)
{
InitializeComponent();
InitializeLanguageText();
m_use = use;
m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
m_cameraParamModel = model;
InitializeControlData();
}
public void ReLoad(CameraParamModel model, bool use)
{
m_cameraParamModel = model;
m_use = use;
InitializeControlData();
}
private void InitializeControlData()
{
if (m_camera.IsOpen())
{
// Get red channel range
int minVal = 0;
int maxVal = 0;
m_camera.GetColorChannelRang(ColorChannel.RED, ref minVal, ref maxVal);
redChannelTB.SetRange(minVal, maxVal);
// Get green channel range
m_camera.GetColorChannelRang(ColorChannel.GREEN, ref minVal, ref maxVal);
greenChannelTB.SetRange(minVal, maxVal);
// Get blue channel range
m_camera.GetColorChannelRang(ColorChannel.BLUE, ref minVal, ref maxVal);
blueChannelTB.SetRange(minVal, maxVal);
}
// 颜色值
int redChannelValue = (int)m_cameraParamModel.parame.RedChannel;
if (redChannelValue >= this.redChannelTB.Minimum && redChannelValue <= this.redChannelTB.Maximum)
{
this.redChannelTB.Value = redChannelValue;
}
int greenChannelValue = (int)m_cameraParamModel.parame.GreenChannel;
if (greenChannelValue >= this.greenChannelTB.Minimum && greenChannelValue <= this.greenChannelTB.Maximum)
{
this.greenChannelTB.Value = greenChannelValue;
}
int blueChannelValue = (int)m_cameraParamModel.parame.BlueChannel;
if (blueChannelValue >= this.blueChannelTB.Minimum && blueChannelValue <= this.blueChannelTB.Maximum)
{
this.blueChannelTB.Value = blueChannelValue;
}
// 白平衡
if (m_cameraParamModel.parame.WhiteBalance == 1)
{
AutoWhiteBalance(true);
}
else
{
if (m_cameraParamModel.parame.FMExposure == 1)
{
m_camera.SetColorTemperatureByString("3200K");
}
else if (m_cameraParamModel.parame.FMExposure == 2)
{
m_camera.SetColorTemperatureByString("5500K");
}
AutoWhiteBalance(false);
}
// 显示颜色值通道
if (m_cameraParamModel.parame.ShowColorPBoxFlag == 1)
{
this.ColourCheckBox.Checked = true;
}
else
{
this.ColourCheckBox.Checked = false;
}
}
///
/// 是否自动白平衡 修改显示样式
///
///
private void AutoWhiteBalance(Boolean isWhiteBalance)
{
if (isWhiteBalance)
{
this.redChannelTB.Enabled = false;
this.greenChannelTB.Enabled = false;
this.blueChannelTB.Enabled = false;
m_cameraParamModel.parame.WhiteBalance = 1;
if (m_use)
{
// 自动白平衡
m_camera.SetWhiteBalanceMode(WhiteBalanceMode.AUTO);
}
}
else
{
m_cameraParamModel.parame.WhiteBalance = 0;
if (m_use)
{
// 手动白平衡
m_camera.SetWhiteBalanceMode(WhiteBalanceMode.MANUAL);
}
}
try
{
this.Invoke(new Action(UpdateColorTemperature));
}
catch
{ }
}
private void UpdateColorTemperature()
{
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
uint cct = 0;
double redChannel = 0;
double greenChannel = 0;
double blueChannel = 0;
int rst = m_camera.GetColorTemperature(ref redChannel, ref greenChannel, ref blueChannel, ref cct);
m_cameraParamModel.parame.RedChannel = redChannel;
m_cameraParamModel.parame.GreenChannel = greenChannel;
m_cameraParamModel.parame.BlueChannel = blueChannel;
// 颜色值
this.redChannelTB.Value = (int)m_cameraParamModel.parame.RedChannel;
this.greenChannelTB.Value = (int)m_cameraParamModel.parame.GreenChannel;
this.blueChannelTB.Value = (int)m_cameraParamModel.parame.BlueChannel;
////string colorTemperatureString = m_camera.GetColorTemperatureString(cct);
this.redChannelTB.Enabled = false;
this.greenChannelTB.Enabled = false;
this.blueChannelTB.Enabled = false;
if (m_cameraParamModel.parame.WhiteBalance == 1)
{
button3.Enabled = false;
}
else
{
switch (m_cameraParamModel.parame.FMExposure)
{
case 0:
button2.Enabled = false;
this.redChannelTB.Enabled = true;
this.greenChannelTB.Enabled = true;
this.blueChannelTB.Enabled = true;
break;
case 1:
button4.Enabled = false;
break;
case 2:
button5.Enabled = false;
break;
}
}
ColourCheckBox.Focus();
}
///
/// 手动白平衡
///
///
///
private void button2_Click(object sender, EventArgs e)
{
m_cameraParamModel.parame.FMExposure = 0;
AutoWhiteBalance(false);
}
///
/// 自动白平衡按钮点击
///
///
///
private void button3_Click(object sender, EventArgs e)
{
AutoWhiteBalance(true);
Thread.Sleep(1500);
UpdateColorTemperature();
}
///
/// 自动一次按钮点击
///
///
///
private void button1_Click(object sender, EventArgs e)
{
AutoWhiteBalance(true);
new Task(() =>
{
Thread.Sleep(500);
m_cameraParamModel.parame.FMExposure = 0;
AutoWhiteBalance(false);
}).Start();
}
///
/// 色温3200K
///
///
///
private void button4_Click(object sender, EventArgs e)
{
m_cameraParamModel.parame.FMExposure = 1;
m_camera.SetColorTemperatureByString("3200K");
AutoWhiteBalance(false);
}
///
/// 色温5500K
///
///
///
private void button5_Click(object sender, EventArgs e)
{
m_cameraParamModel.parame.FMExposure = 2;
m_camera.SetColorTemperatureByString("5500K");
AutoWhiteBalance(false);
}
private void redChannelTB_ValueChanged(object sender, EventArgs e)
{
m_cameraParamModel.parame.RedChannel = ((TrackBar)sender).Value;
// 设置到相机
if (m_use)
{
m_camera.SetRedGain(m_cameraParamModel.parame.RedChannel); //usec
}
}
private void greenChannelTB_ValueChanged(object sender, EventArgs e)
{
m_cameraParamModel.parame.GreenChannel = ((TrackBar)sender).Value;
// 设置到相机
if (m_use)
{
m_camera.SetGreeGain(m_cameraParamModel.parame.GreenChannel); //usec
}
}
private void blueChannelTB_ValueChanged(object sender, EventArgs e)
{
m_cameraParamModel.parame.BlueChannel = ((TrackBar)sender).Value;
// 设置到相机
if (m_use)
{
m_camera.SetBlueGain(m_cameraParamModel.parame.BlueChannel); //usec
}
}
private void xianshiColourCheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
this.yanghongsepictureBox.Visible = cb.Checked;
this.yanghongsepictureBox.Visible = cb.Checked; ;
this.lansepictureBox.Visible = cb.Checked;
this.hongsepictureBox.Visible = cb.Checked;
this.lansepictureBox.Visible = cb.Checked;
this.huangsepictureBox.Visible = cb.Checked;
this.lanlvpictureBox.Visible = cb.Checked;
this.lvsepictureBox.Visible = cb.Checked;
this.m_cameraParamModel.parame.ShowColorPBoxFlag = cb.Checked ? 1 : 0;
}
}
}