289 lines
6.9 KiB
C#
289 lines
6.9 KiB
C#
using System.IO;
|
|
using System.Windows;
|
|
|
|
using CamBooth.App.Core.AppSettings;
|
|
using CamBooth.App.Core.Logging;
|
|
using CamBooth.App.Features.PictureGallery;
|
|
|
|
using EOSDigital.API;
|
|
using EOSDigital.SDK;
|
|
|
|
namespace CamBooth.App.Features.Camera;
|
|
|
|
public class CameraService : IDisposable
|
|
{
|
|
private readonly AppSettingsService _appSettings;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
private readonly PictureGalleryService _pictureGalleryService;
|
|
|
|
private readonly CanonAPI APIHandler;
|
|
|
|
private CameraValue[] AvList;
|
|
|
|
private int BulbTime = 30;
|
|
|
|
private List<EOSDigital.API.Camera> CamList;
|
|
|
|
private int ErrCount;
|
|
|
|
private object ErrLock = new();
|
|
|
|
private bool IsInit;
|
|
|
|
private CameraValue[] ISOList;
|
|
|
|
public EOSDigital.API.Camera MainCamera;
|
|
|
|
private CameraValue[] TvList;
|
|
|
|
|
|
public CameraService(Logger logger, AppSettingsService appSettings, PictureGalleryService pictureGalleryService)
|
|
{
|
|
this._logger = logger;
|
|
this._appSettings = appSettings;
|
|
this._pictureGalleryService = pictureGalleryService;
|
|
try
|
|
{
|
|
this.APIHandler = new CanonAPI();
|
|
this.IsInit = true;
|
|
}
|
|
catch (DllNotFoundException)
|
|
{
|
|
this.ReportError("Canon DLLs not found!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.ReportError(ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary> Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. </summary>
|
|
public void Dispose()
|
|
{
|
|
this.CloseSession();
|
|
this.IsInit = false;
|
|
this.APIHandler.Dispose();
|
|
this.MainCamera.Dispose();
|
|
}
|
|
|
|
|
|
public void ConnectCamera()
|
|
{
|
|
ErrorHandler.SevereErrorHappened += this.ErrorHandler_SevereErrorHappened;
|
|
ErrorHandler.NonSevereErrorHappened += this.ErrorHandler_NonSevereErrorHappened;
|
|
|
|
this.RefreshCamera();
|
|
List<EOSDigital.API.Camera> cameraList = this.APIHandler.GetCameraList();
|
|
if (cameraList.Any())
|
|
{
|
|
this.OpenSession();
|
|
this.SetSettingSaveToComputer();
|
|
this.StarLiveView();
|
|
}
|
|
|
|
string cameraDeviceNames = string.Join(", ", cameraList.Select(cam => cam.DeviceName));
|
|
this._logger.Info(cameraDeviceNames == string.Empty ? "No cameras / devices found" : cameraDeviceNames);
|
|
}
|
|
|
|
|
|
private void SetSettingSaveToComputer()
|
|
{
|
|
this.MainCamera.SetSetting(PropertyID.SaveTo, (int)SaveTo.Host);
|
|
this.MainCamera.SetCapacity(4096, int.MaxValue);
|
|
}
|
|
|
|
|
|
public void CloseSession()
|
|
{
|
|
this.MainCamera.CloseSession();
|
|
|
|
// AvCoBox.Items.Clear();
|
|
// TvCoBox.Items.Clear();
|
|
// ISOCoBox.Items.Clear();
|
|
// SettingsGroupBox.IsEnabled = false;
|
|
// LiveViewGroupBox.IsEnabled = false;
|
|
// SessionButton.Content = "Open Session";
|
|
// SessionLabel.Content = "No open session";
|
|
// StarLVButton.Content = "Start LV";
|
|
}
|
|
|
|
|
|
private void RefreshCamera()
|
|
{
|
|
// CameraListBox.Items.Clear();
|
|
this.CamList = this.APIHandler.GetCameraList();
|
|
|
|
// foreach (Camera cam in CamList) CameraListBox.Items.Add(cam.DeviceName);
|
|
// if (MainCamera?.SessionOpen == true) CameraListBox.SelectedIndex = CamList.FindIndex(t => t.ID == MainCamera.ID);
|
|
// else if (CamList.Count > 0) CameraListBox.SelectedIndex = 0;
|
|
}
|
|
|
|
|
|
private void OpenSession()
|
|
{
|
|
this.MainCamera = this.CamList[0];
|
|
this.MainCamera.OpenSession();
|
|
|
|
//MainCamera.ProgressChanged += MainCamera_ProgressChanged;
|
|
this.MainCamera.StateChanged += this.MainCamera_StateChanged;
|
|
this.MainCamera.DownloadReady += this.MainCamera_DownloadReady;
|
|
|
|
//SessionLabel.Content = MainCamera.DeviceName;
|
|
this.AvList = this.MainCamera.GetSettingsList(PropertyID.Av);
|
|
this.TvList = this.MainCamera.GetSettingsList(PropertyID.Tv);
|
|
this.ISOList = this.MainCamera.GetSettingsList(PropertyID.ISO);
|
|
|
|
// foreach (var Av in AvList) AvCoBox.Items.Add(Av.StringValue);
|
|
// foreach (var Tv in TvList) TvCoBox.Items.Add(Tv.StringValue);
|
|
// foreach (var ISO in ISOList) ISOCoBox.Items.Add(ISO.StringValue);
|
|
// AvCoBox.SelectedIndex = AvCoBox.Items.IndexOf(AvValues.GetValue(MainCamera.GetInt32Setting(PropertyID.Av)).StringValue);
|
|
// TvCoBox.SelectedIndex = TvCoBox.Items.IndexOf(TvValues.GetValue(MainCamera.GetInt32Setting(PropertyID.Tv)).StringValue);
|
|
// ISOCoBox.SelectedIndex = ISOCoBox.Items.IndexOf(ISOValues.GetValue(MainCamera.GetInt32Setting(PropertyID.ISO)).StringValue);
|
|
// SettingsGroupBox.IsEnabled = true;
|
|
// LiveViewGroupBox.IsEnabled = true;
|
|
}
|
|
|
|
|
|
private void ReportError(string message)
|
|
{
|
|
this._logger.Info(message);
|
|
}
|
|
|
|
|
|
private void StarLiveView()
|
|
{
|
|
try
|
|
{
|
|
if (!this.MainCamera.IsLiveViewOn)
|
|
{
|
|
this.MainCamera.StartLiveView();
|
|
}
|
|
else
|
|
{
|
|
this.MainCamera.StopLiveView();
|
|
|
|
//LVCanvas.Background = Brushes.LightGray;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.ReportError(ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
public void TakePhoto()
|
|
{
|
|
try
|
|
{
|
|
this.MainCamera.TakePhoto();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.ReportError(ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
|
|
#region API Events
|
|
|
|
// private void APIHandler_CameraAdded(CanonAPI sender)
|
|
// {
|
|
// try
|
|
// {
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// ReportError(ex.Message, false);
|
|
// }
|
|
// }
|
|
|
|
|
|
private void MainCamera_StateChanged(EOSDigital.API.Camera sender, StateEventID eventID, int parameter)
|
|
{
|
|
try
|
|
{
|
|
if (eventID == StateEventID.Shutdown && this.IsInit)
|
|
{
|
|
Application.Current.Dispatcher.Invoke(() => this.CloseSession());
|
|
|
|
//Dispatcher.Invoke((Action)delegate { CloseSession(); });
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.ReportError(ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
// private void MainCamera_ProgressChanged(object sender, int progress)
|
|
// {
|
|
// try
|
|
// {
|
|
// //MainProgressBar.Dispatcher.Invoke((Action)delegate { MainProgressBar.Value = progress; });
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// ReportError(ex.Message, false);
|
|
// }
|
|
// }
|
|
|
|
// private void MainCamera_LiveViewUpdated(EOSDigital.API.Camera sender, Stream img)
|
|
// {
|
|
// try
|
|
// {
|
|
// using (WrapStream s = new WrapStream(img))
|
|
// {
|
|
// img.Position = 0;
|
|
// BitmapImage EvfImage = new BitmapImage();
|
|
// EvfImage.BeginInit();
|
|
// EvfImage.StreamSource = s;
|
|
// EvfImage.CacheOption = BitmapCacheOption.OnLoad;
|
|
// EvfImage.EndInit();
|
|
// EvfImage.Freeze();
|
|
// Application.Current.Dispatcher.BeginInvoke(SetImageAction, EvfImage);
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// ReportError(ex.Message, false);
|
|
// }
|
|
// }
|
|
|
|
|
|
private void MainCamera_DownloadReady(EOSDigital.API.Camera sender, DownloadInfo Info)
|
|
{
|
|
this._logger.Info("MainCamera_DownloadReady called");
|
|
try
|
|
{
|
|
string dir = this._appSettings.PictureLocation;
|
|
Info.FileName = $"img_{Guid.NewGuid().ToString()}.jpg";
|
|
sender.DownloadFile(Info, dir);
|
|
this._logger.Info("Download complete: " + Path.Combine(dir, Info.FileName));
|
|
Application.Current.Dispatcher.Invoke(() => { this._pictureGalleryService.LoadThumbnailsToCache(); });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.ReportError(ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
private void ErrorHandler_NonSevereErrorHappened(object sender, ErrorCode ex)
|
|
{
|
|
this.ReportError($"SDK Error code: {ex} ({((int)ex).ToString("X")})");
|
|
}
|
|
|
|
|
|
private void ErrorHandler_SevereErrorHappened(object sender, Exception ex)
|
|
{
|
|
this.ReportError(ex.Message);
|
|
}
|
|
|
|
#endregion
|
|
} |