using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media.Media3D;
using System.Xml.Serialization;
using TIS.Imaging;
namespace TIASshot {
public partial class Form1 : Form {
private CameraBase _camera;
public static string APP_NAME = "TIAS Shot";
private LightSource _light = new LightSource();
private bool _isLightOn = true;
private PreviewMonitor _previewMonitor;
private Bitmap _dispBuf = null;
private bool _isUpdateBuf = false;
/// <summary>
/// コンストラクタ
/// </summary>
public Form1() {
InitializeComponent();
var version = Assembly.GetExecutingAssembly().GetName().Version;
Text = $"{APP_NAME} ver.{version.Major}.{version.Minor}";
#if DEBUG
Text += " [DEBUG]";
#endif
}
/// <summary>
/// フォームロード
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e) {
Config.Load();
_camera = new Lucam(this, picPreview);
if (!_camera.Connect()) {
Debug.WriteLine(_camera.ErrorMsg);
Debug.WriteLine("Lucamの接続に失敗しました");
_camera = null;
}
if (_camera == null) {
var cameraList = icImagingControl1.Devices.Select(d => d.Name.Replace(" ", ""));
foreach (var cam in cameraList) {
_camera = new IScam(this, icImagingControl1, cam);
if (_camera.Connect()) break;
Debug.WriteLine(_camera.ErrorMsg);
Debug.WriteLine($"{cam}の接続に失敗しました");
_camera = null;
}
}
if (_camera == null) {
MessageBox.Show("カメラが見つかりません.終了します.", APP_NAME,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
Close();
return;
}
txtDeviceName.Text = _camera.DeviceName;
txtSerialNo.Text = _camera.SerialNumber;
txtNumMultiShots.Text = Config.GetString("Shot/MultiShotCount");
txtMultiShotsInterval.Text = Config.GetString("Shot/MultiShotInterval");
txtDataName.Text = Config.GetString("Shot/SubjectName");
txtSaveFolder.Text = Config.GetString("File/SaveFolder");
txtConvElemNum.Text = Config.GetString("Calib/ConversionChannels");
EnableShots(false);
//_lucam.StartStopPreview();
if (_light.Open()) {
_light.SetLight();
} else {
Debug.WriteLine("光源の接続に失敗しました");
btnLightSW.Enabled = false;
}
_previewMonitor = new PreviewMonitor();
if (_previewMonitor.IsShown) _previewMonitor.Show();
}
/// <summary>
/// フォームクローズ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
if (_camera == null) return;
_camera.Disconnect();
}
/// <summary>
/// 画像撮影1枚
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnShot1_Click(object sender, EventArgs e) {
EnableShots(false);
_camera.ShotOne();
}
/// <summary>
/// 複数画像撮影
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnShotMulti_Click(object sender, EventArgs e) {
EnableShots(false);
_camera.ShotMulti();
}
/// <summary>
/// 校正
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCalib_Click(object sender, EventArgs e) {
//_lucam.Calibration();
}
/// <summary>
/// 画像表示
/// ワーカースレッドから呼び出される
/// </summary>
/// <param name="bmp"></param>
public void ShowImage(Bitmap bmp) {
var bmp2 = new Bitmap(bmp);
_dispBuf = bmp;
_isUpdateBuf = true;
Invalidate();
if (_previewMonitor!=null && _previewMonitor.IsShown) {
_previewMonitor.UpdateImage(bmp2);
}
}
/// <summary>
/// プレビューウィンドウの再描画
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Paint(object sender, PaintEventArgs e) {
if (!_isUpdateBuf || _dispBuf == null) return;
picDisplay.Image?.Dispose();
picDisplay.Image = _dispBuf;
_isUpdateBuf = false;
}
/// <summary>
/// メッセージ表示
/// </summary>
/// <param name="msg"></param>
public void ShowMessage(string msg) {
if (InvokeRequired) {
Invoke((MethodInvoker)delegate { ShowMessage(msg); });
return;
}
txtMessage.Text = msg;
txtMessage.Update();
}
/// <summary>
/// データ名取得
/// </summary>
/// <returns></returns>
public string GetDataName() {
if (InvokeRequired) {
return (string)Invoke((MethodInvoker)delegate { GetDataName(); });
}
return txtDataName.Text;
}
/// <summary>
/// マルチショット撮影の枚数取得
/// </summary>
/// <returns></returns>
public int GetNumMultiShots() {
if (InvokeRequired) {
return (int)Invoke((MethodInvoker)delegate { GetNumMultiShots(); });
}
if (int.TryParse(txtNumMultiShots.Text, out int result)) return result;
return 1;
}
/// <summary>
/// マルチショット撮影の間隔取得
/// </summary>
/// <returns></returns>
public int GetMultiShotsInterval() {
if (InvokeRequired) {
return (int)Invoke((MethodInvoker)delegate { GetMultiShotsInterval(); });
}
if (int.TryParse(txtMultiShotsInterval.Text, out int result)) return result;
return 1000;
}
/// <summary>
/// 撮影ボタン有効化
/// </summary>
/// <param name="enable"></param>
public void EnableShots(bool enable = true) {
if (InvokeRequired) {
Invoke((MethodInvoker)delegate { EnableShots(enable); });
return;
}
btnShotOne.Enabled = enable;
btnShotMulti.Enabled = enable;
}
/// <summary>
/// データフォルダを開く
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpenDataFolder_Click(object sender, EventArgs e) {
System.Diagnostics.Process.Start(txtSaveFolder.Text);
}
/// <summary>
/// 光源のON/OFF切り替え
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLightSW_Click(object sender, EventArgs e) {
if (!_light.IsOpen || _camera.IsCalibrating) return;
if (_isLightOn) {
_light.TurnOff();
btnLightSW.Text = "照明 ON";
_isLightOn = false;
} else {
_light.SetLight();
btnLightSW.Text = "照明 OFF";
_isLightOn = true;
}
}
#if DEBUG
/// <summary>
/// [DEBUG] 白板撮影ボタン(照明均一性評価用)
/// 校正ゲートを通さず IScam の白板撮影パスを呼び出す.
/// Release ビルドには含まれない.
/// </summary>
private void btnWhiteBoard_Click(object sender, EventArgs e) {
int numImages = GetNumMultiShots();
if (numImages < 1) numImages = 1;
btnWhiteBoard.Enabled = false;
ShowMessage($"白板撮影開始({numImages} 枚)...");
// 撮影は UI スレッドをブロックしないよう別スレッドで実行
var thread = new System.Threading.Thread(() => {
try {
_camera.ShotWhiteBoard(numImages);
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine($"[WhiteBoard] 撮影スレッドエラー: {ex.Message}");
ShowMessage($"白板撮影エラー: {ex.Message}");
} finally {
if (InvokeRequired) {
Invoke((MethodInvoker)delegate {
btnWhiteBoard.Enabled = true;
});
} else {
btnWhiteBoard.Enabled = true;
}
}
});
thread.Name = "WhiteBoardShotThread";
thread.Start();
}
#endif
}
}