600 lines
18 KiB
C#
600 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Windows.Forms;
|
|
using Flir.Atlas.Image;
|
|
using Flir.Atlas.Live.Device;
|
|
using Flir.Atlas.Live.Discovery;
|
|
using Newtonsoft.Json;
|
|
using ThermoCamUtils;
|
|
|
|
namespace ThermalImageStreamerDemo
|
|
{
|
|
public partial class MainForm : Form
|
|
{
|
|
#region Private Fields
|
|
|
|
private const string FileFilter = "IR Image Files(*.jpg;*.img;*.seq;*.fcf)|*.jpg;*.img;*.seq;*.fcf|All files (*.*)|*.*";
|
|
|
|
private readonly Timer _timerRefreshUi = new Timer();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Protected Fields
|
|
|
|
protected int currPoint = -1;
|
|
|
|
/// <summary>
|
|
/// Classe gestione ThermoCam (oggetti Image, metodi processing...) x FlirCam
|
|
/// </summary>
|
|
protected TCContr ThermoCamCont = new TCContr();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
_timerRefreshUi.Interval = 100;
|
|
_timerRefreshUi.Tick += _timerRefreshUi_Tick;
|
|
_timerRefreshUi.Start();
|
|
// collego eventi ImgData
|
|
ThermoCamCont.eh_CameraConnected += ImgData_eh_CameraConnected;
|
|
ThermoCamCont.eh_CameraDisposed += ImgData_eh_CameraDisposed;
|
|
ThermoCamCont.eh_CameraConnStatusChanged += ImgData_eh_CameraConnStatusChanged;
|
|
// cerco se ho un set di parametri già impostati...
|
|
ThermoCamCont.tryReloadConf();
|
|
// avvio l'autoconnessione
|
|
if (!string.IsNullOrEmpty(ThermoCamCont.currConf.CameraName))
|
|
{
|
|
discoveryCamera();
|
|
}
|
|
// sistemo le conf temp
|
|
setRangeTemp();
|
|
// calcolo i currConf.destPoints
|
|
updateDestPt();
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Private Properties
|
|
|
|
private bool liveView
|
|
{
|
|
get
|
|
{
|
|
return checkLive.Checked;
|
|
}
|
|
set
|
|
{
|
|
checkLive.Checked = value;
|
|
}
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Protected Properties
|
|
|
|
protected int dimX
|
|
{
|
|
get
|
|
{
|
|
int answ = 100;
|
|
int.TryParse(txtAB.Text, out answ);
|
|
ThermoCamCont.currConf.TargetSize.X = answ;
|
|
return answ;
|
|
}
|
|
set
|
|
{
|
|
ThermoCamCont.currConf.TargetSize.X = value;
|
|
txtAB.Text = $"{value}";
|
|
}
|
|
}
|
|
|
|
protected int dimY
|
|
{
|
|
get
|
|
{
|
|
int answ = 100;
|
|
int.TryParse(txtBC.Text, out answ);
|
|
ThermoCamCont.currConf.TargetSize.Y = answ;
|
|
return answ;
|
|
}
|
|
set
|
|
{
|
|
ThermoCamCont.currConf.TargetSize.Y = value;
|
|
txtBC.Text = $"{value}";
|
|
}
|
|
}
|
|
|
|
protected double maxRangeTemp
|
|
{
|
|
get
|
|
{
|
|
double answ = 1000;
|
|
double.TryParse(txtMaxScale.Text, out answ);
|
|
return answ;
|
|
}
|
|
set
|
|
{
|
|
txtMaxScale.Text = $"{value:N0}";
|
|
}
|
|
}
|
|
|
|
protected double minRangeTemp
|
|
{
|
|
get
|
|
{
|
|
double answ = 0;
|
|
double.TryParse(txtMinScale.Text, out answ);
|
|
return answ;
|
|
}
|
|
set
|
|
{
|
|
txtMinScale.Text = $"{value:N0}";
|
|
}
|
|
}
|
|
|
|
protected bool pointSetup
|
|
{
|
|
get
|
|
{
|
|
return chkPointSetup.Checked;
|
|
}
|
|
set
|
|
{
|
|
chkPointSetup.Checked = value;
|
|
}
|
|
}
|
|
|
|
protected bool readTemp
|
|
{
|
|
get
|
|
{
|
|
return chkReadTemp.Checked;
|
|
}
|
|
set
|
|
{
|
|
chkReadTemp.Checked = value;
|
|
}
|
|
}
|
|
|
|
protected bool saveEnabled
|
|
{
|
|
get
|
|
{
|
|
return chkSaveAll.Checked;
|
|
}
|
|
set
|
|
{
|
|
chkSaveAll.Checked = value;
|
|
}
|
|
}
|
|
|
|
protected bool showOrigBig
|
|
{
|
|
get
|
|
{
|
|
return chkShowOrig.Checked;
|
|
}
|
|
set
|
|
{
|
|
chkShowOrig.Checked = value;
|
|
}
|
|
}
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Private Methods
|
|
|
|
private void _timerRefreshUi_Tick(object sender, EventArgs e)
|
|
{
|
|
// se è in live view --> continuo
|
|
if (liveView)
|
|
{
|
|
ThermoCamCont.takePicture(saveEnabled);
|
|
processImage();
|
|
refreshDisplay();
|
|
}
|
|
// avanzo prog bar
|
|
progBar.Increment(3);
|
|
if (progBar.Value >= progBar.Maximum)
|
|
{
|
|
progBar.Value = 0;
|
|
}
|
|
}
|
|
|
|
private void btnLoad_Click(object sender, EventArgs e)
|
|
{
|
|
// "" --> carica ultima scattata (altrimenti nome)
|
|
ThermoCamCont.fileLoad("");
|
|
|
|
// aggiorno visualizzazione
|
|
processImage();
|
|
refreshDisplay();
|
|
}
|
|
|
|
private void btnReset_Click(object sender, EventArgs e)
|
|
{
|
|
pointSetup = true;
|
|
ThermoCamCont.currConf.OrigPoints = new SetPoints();
|
|
}
|
|
|
|
private void btnSave_Click(object sender, EventArgs e)
|
|
{
|
|
ThermoCamCont.takePicture(true);
|
|
processImage();
|
|
refreshDisplay();
|
|
}
|
|
|
|
private void checkLive_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
// cambio periodo timer!
|
|
_timerRefreshUi.Interval = checkLive.Checked ? 100 : 500;
|
|
}
|
|
|
|
private void chkPointSetup_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
// deselezione Get Temp
|
|
if (readTemp)
|
|
{
|
|
readTemp = false;
|
|
}
|
|
}
|
|
|
|
private void chkReadTemp_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
// deselezione setup punti
|
|
if (pointSetup)
|
|
{
|
|
pointSetup = false;
|
|
}
|
|
if (!readTemp)
|
|
{
|
|
lblReadTemp.Text = "? °C";
|
|
}
|
|
}
|
|
|
|
private void chkShowOrig_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
refreshDisplay();
|
|
}
|
|
|
|
private void disconnectToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
ThermoCamCont.DisconnectCamera();
|
|
}
|
|
|
|
private void discoveryToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
var discoveryDlg = new DiscoveryForm();
|
|
if (discoveryDlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
ThermoCamCont.ConnectCamera(discoveryDlg.SelectedCameraDevice);
|
|
// salvo nome camera in conf attuale!
|
|
ThermoCamCont.currConf.CameraName = discoveryDlg.SelectedCameraDevice.Name;
|
|
ThermoCamCont.currConf.CameraAddress = discoveryDlg.SelectedCameraDevice.IpSettings.IpAddress;
|
|
ThermoCamCont.saveConf();
|
|
}
|
|
}
|
|
|
|
private void displayImages()
|
|
{
|
|
if (ThermoCamCont.Thermal != null)
|
|
{
|
|
lblImgA.Text = showOrigBig ? "ORIGINAL image" : "FINAL image";
|
|
lblImgB.Text = showOrigBig ? "FINAL image" : "ORIGINAL image";
|
|
|
|
pBoxA.Image = showOrigBig ? ThermoCamCont.ColorTransf : ThermoCamCont.Decorated;
|
|
pBoxB.Image = showOrigBig ? ThermoCamCont.Decorated : ThermoCamCont.ColorTransf;
|
|
pBoxC.Image = ThermoCamCont.GrayTransf;
|
|
}
|
|
}
|
|
|
|
private void ImgData_eh_CameraConnected(object sender, EventArgs e)
|
|
{
|
|
_timerRefreshUi.Interval = 100;
|
|
}
|
|
|
|
private void ImgData_eh_CameraConnStatusChanged(object sender, Flir.Atlas.Live.ConnectionStatusChangedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
BeginInvoke((Action)(() => toolStripConnectionStatus.Text = e.Status.ToString()));
|
|
BeginInvoke((Action)(() => toolStripConnectionStatus.ForeColor = e.Status == ConnectionStatus.Connected ? Color.Green : Color.Gray));
|
|
BeginInvoke((Action)(() => cameraToolStripMenuItem.Enabled = e.Status == ConnectionStatus.Connected));
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
|
|
private void ImgData_eh_CameraDisposed(object sender, EventArgs e)
|
|
{
|
|
_timerRefreshUi.Interval = 500;
|
|
}
|
|
|
|
private void lblOrigA_Click(object sender, EventArgs e)
|
|
{
|
|
lblOrigA.ForeColor = toggleEditPoint(0);
|
|
}
|
|
|
|
private void lblOrigB_Click(object sender, EventArgs e)
|
|
{
|
|
lblOrigB.ForeColor = toggleEditPoint(1);
|
|
}
|
|
|
|
private void lblOrigC_Click(object sender, EventArgs e)
|
|
{
|
|
lblOrigC.ForeColor = toggleEditPoint(2);
|
|
}
|
|
|
|
private void lblOrigD_Click(object sender, EventArgs e)
|
|
{
|
|
lblOrigD.ForeColor = toggleEditPoint(3);
|
|
}
|
|
|
|
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
_timerRefreshUi.Stop();
|
|
ThermoCamCont.DisposeCamera();
|
|
}
|
|
|
|
private void MainForm_Load(object sender, EventArgs e)
|
|
{
|
|
}
|
|
|
|
private void MainForm_Shown(object sender, EventArgs e)
|
|
{
|
|
}
|
|
|
|
private void openToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
using (var dialog = new OpenFileDialog())
|
|
{
|
|
dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\FLIR";
|
|
dialog.Filter = FileFilter;
|
|
if (dialog.ShowDialog() != DialogResult.OK) return;
|
|
var playback = new PlaybackForm(dialog.FileName);
|
|
playback.Show();
|
|
}
|
|
}
|
|
|
|
private void pictureBox1_Click(object sender, EventArgs e)
|
|
{
|
|
// calcolo la ratio img originale --> pictureBox
|
|
double ratio = 1;
|
|
try
|
|
{
|
|
if (ThermoCamCont.Origin != null && ThermoCamCont.Origin.Width > 0)
|
|
{
|
|
ratio = (double)pBoxA.Width / ThermoCamCont.Origin.Width;
|
|
}
|
|
}
|
|
catch
|
|
{ }
|
|
// trasformo punto in equivalente originale
|
|
var mea = (MouseEventArgs)e;
|
|
ThermoCamCont.lastPoint = new Point() { X = (int)(mea.X / ratio), Y = (int)(mea.Y / ratio) };
|
|
lblPoint.Text = $"({mea.X},{mea.Y}) --> ({ThermoCamCont.lastPoint.X},{ThermoCamCont.lastPoint.Y})";
|
|
if (pointSetup)
|
|
{
|
|
// max 4 punti... se sono già FULL --> imposto singolo valore nuovo
|
|
if (ThermoCamCont.currConf.OrigPoints.curr >= 4)
|
|
{
|
|
if (currPoint >= 0)
|
|
{
|
|
ThermoCamCont.currConf.OrigPoints.Coords[currPoint] = ThermoCamCont.lastPoint;
|
|
}
|
|
else
|
|
{
|
|
ThermoCamCont.currConf.OrigPoints = new SetPoints();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// salvo coordinate in base al num corrente...
|
|
try
|
|
{
|
|
ThermoCamCont.currConf.OrigPoints.Coords.Add(ThermoCamCont.lastPoint);
|
|
ThermoCamCont.currConf.OrigPoints.curr++;
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
// salvo conf
|
|
ThermoCamCont.saveConf();
|
|
|
|
// aggiorno visualizzazione
|
|
processImage();
|
|
refreshDisplay();
|
|
}
|
|
else if (readTemp)
|
|
{
|
|
// x ora fisso 1 solo valore...
|
|
ThermoCamCont.saveMeasurePoint(false);
|
|
// aggiorno visualizzazione
|
|
processImage();
|
|
refreshDisplay();
|
|
}
|
|
}
|
|
|
|
private void processImage()
|
|
{
|
|
// disegno immagine
|
|
ThermoCamCont.getTemperatures(readTemp);
|
|
ThermoCamCont.calculateTarget();
|
|
}
|
|
|
|
private void refreshDisplay()
|
|
{
|
|
ThermoCamCont.drawCrossAtPoints();
|
|
displayImages();
|
|
updateDisplay();
|
|
updateTempDisplay();
|
|
updateStatDisplay();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sistema le temp range min/Max
|
|
/// </summary>
|
|
private void setRangeTemp()
|
|
{
|
|
ThermoCamCont.currConf.TargetRange.Min = minRangeTemp;
|
|
ThermoCamCont.currConf.TargetRange.Max = maxRangeTemp;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifica se il punto era in edit, altrimenti segnala e lo attiva... restituendo colore x il controllo
|
|
/// </summary>
|
|
/// <param name="selPoint"></param>
|
|
private Color toggleEditPoint(int selPoint)
|
|
{
|
|
Color answ = Color.Black;
|
|
lblOrigA.ForeColor = answ;
|
|
lblOrigB.ForeColor = answ;
|
|
lblOrigC.ForeColor = answ;
|
|
lblOrigD.ForeColor = answ;
|
|
readTemp = false;
|
|
pointSetup = true;
|
|
if (currPoint == selPoint)
|
|
{
|
|
// deseleziono
|
|
currPoint = -1;
|
|
}
|
|
else
|
|
{
|
|
// indico selezionato
|
|
currPoint = selPoint;
|
|
answ = Color.Green;
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
private void txtAB_TextChanged(object sender, EventArgs e)
|
|
{
|
|
// ricalcolo in proporzione altro valore...
|
|
dimY = (int)((double)dimX * 256 / 320);
|
|
// aggiorno punti dest
|
|
updateDestPt();
|
|
}
|
|
|
|
private void txtBC_TextChanged(object sender, EventArgs e)
|
|
{
|
|
// ricalcolo in proporzione altro valore...
|
|
dimX = (int)((double)dimY * 320 / 256);
|
|
// aggiorno punti dest
|
|
updateDestPt();
|
|
}
|
|
|
|
private void txtMaxScale_TextChanged(object sender, EventArgs e)
|
|
{
|
|
setRangeTemp();
|
|
}
|
|
|
|
private void txtMinScale_TextChanged(object sender, EventArgs e)
|
|
{
|
|
setRangeTemp();
|
|
}
|
|
|
|
private void updateDestPt()
|
|
{
|
|
ThermoCamCont.currConf.DestPoints.Coords = new List<Point>();
|
|
// ciclo i punti A-B-C-D...
|
|
ThermoCamCont.currConf.DestPoints.Coords.Add(new Point() { X = 0, Y = 0 });
|
|
ThermoCamCont.currConf.DestPoints.Coords.Add(new Point() { X = dimX, Y = 0 });
|
|
ThermoCamCont.currConf.DestPoints.Coords.Add(new Point() { X = dimX, Y = dimY });
|
|
ThermoCamCont.currConf.DestPoints.Coords.Add(new Point() { X = 0, Y = dimY });
|
|
// salvo conf
|
|
ThermoCamCont.saveConf();
|
|
}
|
|
|
|
private void updateDisplay()
|
|
{
|
|
lblOrigA.Text = "A";
|
|
lblOrigB.Text = "B";
|
|
lblOrigC.Text = "C";
|
|
lblOrigD.Text = "D";
|
|
// popolo
|
|
if (ThermoCamCont.currConf.OrigPoints.Coords.Count > 0)
|
|
{
|
|
lblOrigA.Text = $"A: ({ThermoCamCont.currConf.OrigPoints.Coords[0].X},{ThermoCamCont.currConf.OrigPoints.Coords[0].Y})";
|
|
}
|
|
if (ThermoCamCont.currConf.OrigPoints.Coords.Count > 1)
|
|
{
|
|
lblOrigB.Text = $"B: ({ThermoCamCont.currConf.OrigPoints.Coords[1].X},{ThermoCamCont.currConf.OrigPoints.Coords[1].Y})";
|
|
}
|
|
if (ThermoCamCont.currConf.OrigPoints.Coords.Count > 2)
|
|
{
|
|
lblOrigC.Text = $"C: ({ThermoCamCont.currConf.OrigPoints.Coords[2].X},{ThermoCamCont.currConf.OrigPoints.Coords[2].Y})";
|
|
}
|
|
if (ThermoCamCont.currConf.OrigPoints.Coords.Count > 3)
|
|
{
|
|
lblOrigD.Text = $"D: ({ThermoCamCont.currConf.OrigPoints.Coords[3].X},{ThermoCamCont.currConf.OrigPoints.Coords[3].Y})";
|
|
ThermoCamCont.calculateTarget();
|
|
}
|
|
}
|
|
|
|
private void updateStatDisplay()
|
|
{
|
|
if (ThermoCamCont.ExTime.Stats.Count > 0)
|
|
{
|
|
try
|
|
{
|
|
lblStats.Text = $"Camera: {ThermoCamCont.lastStatTime("ImageAcquisition")}ms | Persp {ThermoCamCont.lastStatTime("ImageTransf")}ms | Color {ThermoCamCont.lastStatTime("ImageColor")}ms | Points {ThermoCamCont.lastStatTime("AddPoints")}ms | Measures {ThermoCamCont.lastStatTime("GetAllTemperatures")}ms";
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
}
|
|
|
|
private void updateTempDisplay()
|
|
{
|
|
if (ThermoCamCont.Thermal != null)
|
|
{
|
|
if (readTemp)
|
|
{
|
|
try
|
|
{
|
|
// mostro temp da FLIR
|
|
lblReadTemp.Text = $"{ThermoCamCont.lastReadTemp:N2} °C";
|
|
// mostro minimo / massimo
|
|
lblMinTemp.Text = $"{ThermoCamCont.lastTempRange.Minimum:N2}";
|
|
lblMaxTemp.Text = $"{ThermoCamCont.lastTempRange.Maximum:N2}";
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Public Methods
|
|
|
|
public void discoveryCamera()
|
|
{
|
|
var discoveryDialog = new DiscoveryForm();
|
|
// se no connesso così prosegue...
|
|
if (!IRCam.ThermoCamera.IsConnected)
|
|
{
|
|
discoveryDialog.CameraAddress = ThermoCamCont.currConf.CameraAddress;
|
|
discoveryDialog.CameraName = ThermoCamCont.currConf.CameraName;
|
|
if (discoveryDialog.doAutoConnect)
|
|
{
|
|
//discoveryDialog.WindowState = FormWindowState.Minimized;
|
|
if (discoveryDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
ThermoCamCont.ConnectCamera(discoveryDialog.SelectedCameraDevice);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |