diff --git a/TIASshot/CameraBase.cs b/TIASshot/CameraBase.cs index b3703f8..6192892 100644 --- a/TIASshot/CameraBase.cs +++ b/TIASshot/CameraBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.Eventing.Reader; using System.Drawing; using System.IO; using System.Linq; @@ -12,21 +13,25 @@ using OpenCvSharp.Aruco; namespace TIASshot { - internal class CameraBase { + internal abstract class CameraBase { + // インターフェース + public abstract bool Connect(); + public abstract void Disconnect(); + + // プロパティ + public string DeviceName { get; protected set; } = "Unknown"; + public string SerialNumber { get; protected set; } + public string ErrorMsg { get; protected set; } + // 派生クラスで使用するメンバ protected Form1 _form; protected int _calibrating = 0; protected bool _calibrated = false; protected Bitmap[] _bmps = new Bitmap[2]; protected int _bmpIndex = 0; - protected readonly float RefRGB; - readonly float UpdateRate; - protected readonly Mat TCC_SRGB; - protected readonly Mat TCC_XYZ; - - public string DeviceName { get; protected set; } = "Unknown"; - public string SerialNumber { get; protected set; } - public string ErrorMsg { get; protected set; } + protected bool _isPreview = false; + protected List _chartMasks = new List(); + protected Mat _convRGB2SRGB; // プライベートメンバ readonly Dictionary ARDict = CvAruco.GetPredefinedDictionary(PredefinedDictionaryName.Dict4X4_50); @@ -36,11 +41,13 @@ readonly Point2f[] PointsDst41 = new Point2f[]{ new Point2f(345, 1200), new Point2f(465, 1200), new Point2f(465, 1320), new Point2f(345, 1320), }; + readonly Mat TCC_SRGB; + readonly Mat TCC_XYZ; + readonly float UpdateRate; int _detectionCount = 0; Point2f _lastPosition = new Point2f(0, 0); - protected List _chartMasks = new List(); Mat _tccRois; // TCCのROI検出結果画像 - protected Mat _convRGB2SRGB; + /// /// カメラの基本クラス @@ -48,7 +55,6 @@ public CameraBase(Form1 form) { _form = form; Config.Load(); - RefRGB = Config.GetFloat("ReferenceValue"); UpdateRate = Config.GetFloat("UpdateRate"); TCC_SRGB = LoadMatFromCsv(Config.GetString("TccSrgbTableFile")); TCC_XYZ = LoadMatFromCsv(Config.GetString("TccXyzTableFile")); diff --git a/TIASshot/Form1.cs b/TIASshot/Form1.cs index 5209a97..b07aac2 100644 --- a/TIASshot/Form1.cs +++ b/TIASshot/Form1.cs @@ -19,7 +19,7 @@ public static string APP_NAME = "TIAS Shot"; //private Lucam _lucam; - private IScam _iscam; + private CameraBase _camera; private LightSource _light = new LightSource(); private bool _isLightOn = true; @@ -29,9 +29,6 @@ public Form1() { InitializeComponent(); - //_lucam = new Lucam(this, picPreview, picDisplay); - _iscam = new IScam(this, icImagingControl1, "DBK33UX178"); - var version = Assembly.GetExecutingAssembly().GetName().Version; Text = $"{APP_NAME} ver.{version.Major}.{version.Minor}"; #if DEBUG @@ -45,24 +42,31 @@ /// /// private void Form1_Load(object sender, EventArgs e) { - //if (!_lucam.Connect()) { - // MessageBox.Show(_lucam.ErrorMsg, APP_NAME, - // MessageBoxButtons.OK, MessageBoxIcon.Warning); - // Close(); - // return; - //} - if (!_iscam.Connect()) { - MessageBox.Show("IScamの接続に失敗しました。", APP_NAME, + _camera = new Lucam(this, picPreview); + if (!_camera.Connect()) { + Debug.WriteLine(_camera.ErrorMsg); + Debug.WriteLine("Lucamの接続に失敗しました"); + _camera = null; + } + + if (_camera == null) { + _camera = new IScam(this, icImagingControl1, "DBK33UX178"); + if (!_camera.Connect()) { + Debug.WriteLine(_camera.ErrorMsg); + Debug.WriteLine("DBK33UX178の接続に失敗しました"); + _camera = null; + } + } + + if (_camera == null) { + MessageBox.Show("カメラが見つかりません.終了します.", APP_NAME, MessageBoxButtons.OK, MessageBoxIcon.Warning); Close(); return; } - - txtDeviceName.Text = _iscam.DeviceName; - txtSerialNo.Text = _iscam.SerialNumber; - //txtDeviceName.Text = _lucam.DeviceName; - //txtSerialNo.Text = _lucam.SerialNumber; + txtDeviceName.Text = _camera.DeviceName; + txtSerialNo.Text = _camera.SerialNumber; txtSaveFolder.Text = Config.GetString("SaveFolder"); EnableShots(false); //_lucam.StartStopPreview(); @@ -78,8 +82,7 @@ /// /// private void Form1_FormClosing(object sender, FormClosingEventArgs e) { - //_lucam.Disconnect(); - _iscam.Disconnect(); + _camera.Disconnect(); } /// diff --git a/TIASshot/IScam.cs b/TIASshot/IScam.cs index 216f3f1..2a8b7a5 100644 --- a/TIASshot/IScam.cs +++ b/TIASshot/IScam.cs @@ -35,7 +35,7 @@ /// IScam接続 /// /// - public bool Connect() { + public override bool Connect() { if (!BootCheck()) return false; //if (!_ic.LoadShowSaveDeviceState("lastSelectedDeviceState.xml")) { @@ -44,8 +44,7 @@ //_ic.SaveDeviceStateToFile("DBK33UX178.xml"); _ic.LoadDeviceStateFromFile($"{DeviceName}.xml", true); if (!_ic.DeviceValid) { - MessageBox.Show("IScamの設定ファイルが見つかりません。", Form1.APP_NAME, - MessageBoxButtons.OK, MessageBoxIcon.Warning); + ErrorMsg = "IScamの設定ファイルが見つかりません"; return false; } @@ -81,7 +80,7 @@ /// /// IScam切断 /// - public void Disconnect() { + public override void Disconnect() { _ic.LiveStop(); } diff --git a/TIASshot/Lucam.cs b/TIASshot/Lucam.cs index 7b22873..3b619c1 100644 --- a/TIASshot/Lucam.cs +++ b/TIASshot/Lucam.cs @@ -35,7 +35,7 @@ IntPtr _hCam = IntPtr.Zero; PictureBox _picPreview; - bool _isPreview = false; + dll.LucamSnapshot _snap; dll.LucamConversion _convert; dll.LucamRgbPreviewCallback _callbackHandler; @@ -86,16 +86,22 @@ /// カメラ接続 /// /// - public bool Connect() { + public override bool Connect() { if (!BootCheck()) return false; - var numCam = dll.LucamNumCameras(); + int numCam = 0; + try { + numCam = dll.LucamNumCameras(); + } catch (Exception ex) { + ErrorMsg = ex.Message; + return false; + } if ( numCam < 1 ) { - ErrorMsg = "カメラが見つかりません.\r\n終了します."; + ErrorMsg = "Lucamが見つかりません"; return false; } if (numCam > 1) { - ErrorMsg = "複数のカメラが見つかりました.\r\n正しいカメラを1つ接続してください.\r\n終了します."; + ErrorMsg = "複数のLucamが見つかりました.\r\n正しいカメラを1つ接続してください."; return false; } @@ -109,7 +115,7 @@ // カメラを開く _hCam = api.CameraOpen(1); if (_hCam == IntPtr.Zero) { - ErrorMsg = "カメラの接続に失敗しました.\r\n他のアプリケーションでカメラを使用していないか確認してください.\r\n終了します."; + ErrorMsg = "カメラの接続に失敗しました.\r\n他のアプリケーションでカメラを使用していないか確認してください."; return false; } @@ -117,7 +123,7 @@ _callbackHandler = new dll.LucamRgbPreviewCallback(PreviewCallback); _callbackId = dll.LucamAddRgbPreviewCallback(_hCam, _callbackHandler, IntPtr.Zero, dll.LucamPixelFormat.PF_24); if (_callbackId == -1) { - ErrorMsg = "コールバックの登録に失敗しました.\r\n終了します."; + ErrorMsg = "コールバックの登録に失敗しました"; return false; } @@ -283,7 +289,7 @@ /// /// カメラ切断 /// - public void Disconnect() { + public override void Disconnect() { if (_hCam != IntPtr.Zero) { if (_isPreview) { StartStopPreview();