Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using EDSDKLib;
- using PhotoBooth.Functions;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace PhotoBooth.Pages
- {
- /// <summary>
- /// Interaction logic for Picture.xaml
- /// </summary>
- public partial class Picture : Page
- {
- ImageSource EvfImage;
- JpegBitmapDecoder dec;
- int secondsToWait = 4;
- static SDKHandler CameraHandler;
- static DispatcherTimer dispatcherTimer;
- public Picture()
- {
- InitializeComponent();
- // Define steps
- Global.CreateSteps(Global.SelectedMenuOrder, this, ((MasterPage)System.Windows.Application.Current.MainWindow).StepsWindow);
- // Create TempLocation
- Directory.CreateDirectory(Settings.TempLocation);
- // Handle the Canon EOS camera
- CameraHandler = new SDKHandler();
- CameraHandler.LiveViewUpdated += new SDKHandler.StreamUpdate(CameraHandler_LiveViewUpdated);
- CameraHandler.ProgressChanged += new SDKHandler.ProgressHandler(CameraHandler_ProgressChanged);
- CameraHandler.CameraHasShutdown += CameraHandler_CameraHasShutdown;
- CameraHandler.ImageSaveDirectory = Settings.TempLocation;
- // Configure the camera timer
- dispatcherTimer = new DispatcherTimer();
- dispatcherTimer.Tick += DispatcherTimer_Tick;
- dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 800);
- // Debugging
- Global.WriteToLog("INFO", "Starting camera...");
- Global.MemoryStats();
- }
- private void Page_Loaded(object sender, RoutedEventArgs e)
- {
- // Enable the camera
- EnableCameraHandler(true);
- }
- private void CameraTrigger_Click(object sender, RoutedEventArgs e)
- {
- // The user has clicked the trigger, change the layout
- CameraTrigger.Visibility = System.Windows.Visibility.Collapsed;
- CameraCountDown.Visibility = System.Windows.Visibility.Visible;
- CameraTrigger.IsEnabled = false;
- // Start the countdown
- secondsToWait = 4;
- dispatcherTimer.Start();
- Global.WriteToLog("INFO", "Camera shutter pressed... waiting for camera to take picture!");
- }
- private void DispatcherTimer_Tick(object sender, EventArgs e)
- {
- // Handles the countdown
- switch (secondsToWait)
- {
- case 4:
- CameraTimer3.Foreground = new SolidColorBrush(Colors.White);
- Global.PlaySound("pack://application:,,,/Resources/Audio/camera_beep.wav");
- break;
- case 3:
- CameraTimer2.Foreground = new SolidColorBrush(Colors.White);
- Global.PlaySound("pack://application:,,,/Resources/Audio/camera_beep.wav");
- break;
- case 2:
- CameraTimer1.Foreground = new SolidColorBrush(Colors.White);
- Global.PlaySound("pack://application:,,,/Resources/Audio/camera_beep.wav");
- break;
- case 1:
- CameraTimer0.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/icon_cameraWhite.png"));
- // Perform a nice "Flash"-effect when the image is taken
- Global.CameraFlashEffect(((MasterPage)System.Windows.Application.Current.MainWindow).CameraFlash);
- if (Settings.CameraAf)
- {
- // If EnableAutoFocus is enabled, use it
- bool result = CameraHandler.TakePhotoAF();
- if (result == false)
- {
- // Reset cameraTrigger for taking another photo
- CameraTrigger.Visibility = System.Windows.Visibility.Visible;
- CameraCountDown.Visibility = System.Windows.Visibility.Collapsed;
- CameraTrigger.IsEnabled = true;
- // Restart LiveView
- if (!CameraHandler.IsFilming)
- {
- if (!CameraHandler.IsLiveViewOn)
- {
- CameraHandler.StartLiveView();
- }
- else
- {
- CameraHandler.StopLiveView();
- CameraHandler.StartLiveView();
- }
- }
- }
- }
- else
- {
- // Take the picture
- CameraHandler.TakePhoto();
- }
- break;
- case 0:
- CameraTimer0.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/icon_cameraRed.png"));
- CameraTimer1.Foreground = (SolidColorBrush)(new BrushConverter().ConvertFrom("#e8234a"));
- CameraTimer2.Foreground = (SolidColorBrush)(new BrushConverter().ConvertFrom("#e8234a"));
- CameraTimer3.Foreground = (SolidColorBrush)(new BrushConverter().ConvertFrom("#e8234a"));
- dispatcherTimer.Stop();
- break;
- default:
- break;
- }
- secondsToWait--;
- }
- private void Page_Unloaded(object sender, RoutedEventArgs e)
- {
- // Stop and dispose the CameraHandler
- if (!CameraHandler.IsFilming && CameraHandler.IsLiveViewOn)
- {
- CameraHandler.StopLiveView();
- }
- CameraHandler.Dispose();
- }
- #region CameraHandler
- private void CameraHandler_LiveViewUpdated(Stream img)
- {
- // Handle the liveview from the camera and output it
- img.Position = 0;
- dec = new JpegBitmapDecoder(img, BitmapCreateOptions.None, BitmapCacheOption.None);
- EvfImage = dec.Frames[0];
- Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
- {
- CameraLiveView.Source = null;
- CameraLiveView.Source = EvfImage;
- }));
- }
- private void CameraHandler_ProgressChanged(int Progress)
- {
- // If the progress is 100% and the file exists we want to load it into preview
- if (Progress == 100 && File.Exists(Global.PersonImagePath))
- {
- Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
- {
- BitmapImage image = new BitmapImage();
- image.BeginInit();
- image.CacheOption = BitmapCacheOption.OnLoad;
- image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
- image.UriSource = new Uri(Global.PersonImagePath);
- image.EndInit();
- Global.PersonImage = image;
- NavigationService.Navigate(Global.FindPageByString(Global.NavigateManager(this, Functions.Enums.Navigation.Forward)));
- }));
- }
- }
- private void CameraHandler_CameraHasShutdown(object sender, EventArgs e)
- {
- // The camera has shutdown, let's close the session
- CameraHandler.CloseSession();
- }
- private bool DetectCanonCamera()
- {
- // Detect Canon camera and return true/false
- List<Camera> CamList = CameraHandler.GetCameraList();
- if (CamList.Count > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private void EnableCameraHandler(bool status)
- {
- // If the session is already open or we want to turn if off
- if (CameraHandler.CameraSessionOpen && status == false)
- {
- CameraHandler.CloseSession();
- return;
- }
- // If the camera is active and we want it active, just return
- if (CameraHandler.CameraSessionOpen && status == true)
- {
- return;
- }
- // If it's off and we want it to be on...
- if (CameraHandler.GetCameraList().Count > 0)
- {
- if (status)
- {
- CameraHandler.OpenSession(CameraHandler.GetCameraList()[0]);
- // Configure the camera
- switch (Settings.CameraWb)
- {
- case "Auto": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Auto); break;
- case "Daylight": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Daylight); break;
- case "Cloudy": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Cloudy); break;
- case "Tangsten": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Tangsten); break;
- case "Fluorescent": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Fluorescent); break;
- case "Strobe": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Strobe); break;
- case "WhitePaper": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_WhitePaper); break;
- case "Shade": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Shade); break;
- default:
- break;
- }
- CameraHandler.SetSetting(EDSDK.PropID_Av, CameraValues.AV(Settings.CameraAv));
- CameraHandler.SetSetting(EDSDK.PropID_Tv, CameraValues.TV(Settings.CameraTv));
- CameraHandler.SetSetting(EDSDK.PropID_ISOSpeed, CameraValues.ISO(Settings.CameraISOSpeed));
- CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Auto);
- CameraHandler.SetSetting(EDSDK.PropID_SaveTo, (uint)EDSDK.EdsSaveTo.Host);
- CameraHandler.SetCapacity();
- // Restart the camera
- if (!CameraHandler.IsFilming)
- {
- if (!CameraHandler.IsLiveViewOn)
- {
- CameraHandler.StartLiveView();
- }
- else
- {
- CameraHandler.StopLiveView();
- CameraHandler.StartLiveView();
- }
- }
- }
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement