Advertisement
Guest User

Untitled

a guest
Aug 9th, 2015
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.47 KB | None | 0 0
  1. using EDSDKLib;
  2. using PhotoBooth.Functions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Windows.Threading;
  18.  
  19. namespace PhotoBooth.Pages
  20. {
  21.     /// <summary>
  22.     /// Interaction logic for Picture.xaml
  23.     /// </summary>
  24.     public partial class Picture : Page
  25.     {
  26.         ImageSource EvfImage;
  27.         JpegBitmapDecoder dec;
  28.         int secondsToWait = 4;
  29.         static SDKHandler CameraHandler;
  30.         static DispatcherTimer dispatcherTimer;
  31.  
  32.         public Picture()
  33.         {
  34.             InitializeComponent();
  35.  
  36.             // Define steps
  37.             Global.CreateSteps(Global.SelectedMenuOrder, this, ((MasterPage)System.Windows.Application.Current.MainWindow).StepsWindow);
  38.            
  39.             // Create TempLocation
  40.             Directory.CreateDirectory(Settings.TempLocation);
  41.            
  42.             // Handle the Canon EOS camera
  43.             CameraHandler = new SDKHandler();
  44.             CameraHandler.LiveViewUpdated += new SDKHandler.StreamUpdate(CameraHandler_LiveViewUpdated);
  45.             CameraHandler.ProgressChanged += new SDKHandler.ProgressHandler(CameraHandler_ProgressChanged);
  46.             CameraHandler.CameraHasShutdown += CameraHandler_CameraHasShutdown;
  47.             CameraHandler.ImageSaveDirectory = Settings.TempLocation;
  48.  
  49.             // Configure the camera timer
  50.             dispatcherTimer = new DispatcherTimer();
  51.             dispatcherTimer.Tick += DispatcherTimer_Tick;
  52.             dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 800);
  53.  
  54.             // Debugging
  55.             Global.WriteToLog("INFO", "Starting camera...");
  56.             Global.MemoryStats();
  57.         }
  58.  
  59.         private void Page_Loaded(object sender, RoutedEventArgs e)
  60.         {
  61.             // Enable the camera
  62.             EnableCameraHandler(true);
  63.         }
  64.  
  65.         private void CameraTrigger_Click(object sender, RoutedEventArgs e)
  66.         {
  67.             // The user has clicked the trigger, change the layout
  68.             CameraTrigger.Visibility = System.Windows.Visibility.Collapsed;
  69.             CameraCountDown.Visibility = System.Windows.Visibility.Visible;
  70.             CameraTrigger.IsEnabled = false;
  71.  
  72.             // Start the countdown
  73.             secondsToWait = 4;
  74.             dispatcherTimer.Start();
  75.             Global.WriteToLog("INFO", "Camera shutter pressed... waiting for camera to take picture!");
  76.         }
  77.  
  78.         private void DispatcherTimer_Tick(object sender, EventArgs e)
  79.         {
  80.             // Handles the countdown
  81.             switch (secondsToWait)
  82.             {
  83.                 case 4:
  84.                     CameraTimer3.Foreground = new SolidColorBrush(Colors.White);
  85.                     Global.PlaySound("pack://application:,,,/Resources/Audio/camera_beep.wav");
  86.                     break;
  87.                 case 3:
  88.                     CameraTimer2.Foreground = new SolidColorBrush(Colors.White);
  89.                     Global.PlaySound("pack://application:,,,/Resources/Audio/camera_beep.wav");
  90.                     break;
  91.                 case 2:
  92.                     CameraTimer1.Foreground = new SolidColorBrush(Colors.White);
  93.                     Global.PlaySound("pack://application:,,,/Resources/Audio/camera_beep.wav");
  94.                     break;
  95.                 case 1:
  96.                     CameraTimer0.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/icon_cameraWhite.png"));
  97.  
  98.                     // Perform a nice "Flash"-effect when the image is taken
  99.                     Global.CameraFlashEffect(((MasterPage)System.Windows.Application.Current.MainWindow).CameraFlash);
  100.  
  101.                     if (Settings.CameraAf)
  102.                     {
  103.                         // If EnableAutoFocus is enabled, use it
  104.                         bool result = CameraHandler.TakePhotoAF();
  105.                         if (result == false)
  106.                         {
  107.                             // Reset cameraTrigger for taking another photo
  108.                             CameraTrigger.Visibility = System.Windows.Visibility.Visible;
  109.                             CameraCountDown.Visibility = System.Windows.Visibility.Collapsed;
  110.                             CameraTrigger.IsEnabled = true;
  111.  
  112.                             // Restart LiveView
  113.                             if (!CameraHandler.IsFilming)
  114.                             {
  115.                                 if (!CameraHandler.IsLiveViewOn)
  116.                                 {
  117.                                     CameraHandler.StartLiveView();
  118.                                 }
  119.                                 else
  120.                                 {
  121.                                     CameraHandler.StopLiveView();
  122.                                     CameraHandler.StartLiveView();
  123.                                 }
  124.                             }
  125.                         }
  126.                     }
  127.                     else
  128.                     {
  129.                         // Take the picture
  130.                         CameraHandler.TakePhoto();
  131.                     }
  132.                     break;
  133.                 case 0:
  134.                     CameraTimer0.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/icon_cameraRed.png"));
  135.                     CameraTimer1.Foreground = (SolidColorBrush)(new BrushConverter().ConvertFrom("#e8234a"));
  136.                     CameraTimer2.Foreground = (SolidColorBrush)(new BrushConverter().ConvertFrom("#e8234a"));
  137.                     CameraTimer3.Foreground = (SolidColorBrush)(new BrushConverter().ConvertFrom("#e8234a"));
  138.  
  139.                     dispatcherTimer.Stop();
  140.                     break;
  141.                 default:
  142.                     break;
  143.             }
  144.  
  145.             secondsToWait--;
  146.         }
  147.  
  148.         private void Page_Unloaded(object sender, RoutedEventArgs e)
  149.         {
  150.             // Stop and dispose the CameraHandler
  151.             if (!CameraHandler.IsFilming && CameraHandler.IsLiveViewOn)
  152.             {
  153.                 CameraHandler.StopLiveView();
  154.             }
  155.             CameraHandler.Dispose();
  156.         }
  157.  
  158.         #region CameraHandler
  159.         private void CameraHandler_LiveViewUpdated(Stream img)
  160.         {
  161.             // Handle the liveview from the camera and output it
  162.             img.Position = 0;
  163.             dec = new JpegBitmapDecoder(img, BitmapCreateOptions.None, BitmapCacheOption.None);
  164.             EvfImage = dec.Frames[0];
  165.  
  166.             Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
  167.             {
  168.                 CameraLiveView.Source = null;
  169.                 CameraLiveView.Source = EvfImage;
  170.             }));
  171.         }
  172.  
  173.         private void CameraHandler_ProgressChanged(int Progress)
  174.         {
  175.             // If the progress is 100% and the file exists we want to load it into preview
  176.             if (Progress == 100 && File.Exists(Global.PersonImagePath))
  177.             {
  178.                 Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
  179.                 {
  180.                     BitmapImage image = new BitmapImage();
  181.                     image.BeginInit();
  182.                     image.CacheOption = BitmapCacheOption.OnLoad;
  183.                     image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
  184.                     image.UriSource = new Uri(Global.PersonImagePath);
  185.                     image.EndInit();
  186.  
  187.                     Global.PersonImage = image;
  188.                     NavigationService.Navigate(Global.FindPageByString(Global.NavigateManager(this, Functions.Enums.Navigation.Forward)));
  189.                 }));
  190.             }
  191.         }
  192.  
  193.         private void CameraHandler_CameraHasShutdown(object sender, EventArgs e)
  194.         {
  195.             // The camera has shutdown, let's close the session
  196.             CameraHandler.CloseSession();
  197.         }
  198.  
  199.         private bool DetectCanonCamera()
  200.         {
  201.             // Detect Canon camera and return true/false
  202.             List<Camera> CamList = CameraHandler.GetCameraList();
  203.             if (CamList.Count > 0)
  204.             {
  205.                 return true;
  206.             }
  207.             else
  208.             {
  209.                 return false;
  210.             }
  211.         }
  212.        
  213.         private void EnableCameraHandler(bool status)
  214.         {
  215.             // If the session is already open or we want to turn if off
  216.             if (CameraHandler.CameraSessionOpen && status == false)
  217.             {
  218.                 CameraHandler.CloseSession();
  219.                 return;
  220.             }
  221.  
  222.             // If the camera is active and we want it active, just return
  223.             if (CameraHandler.CameraSessionOpen && status == true)
  224.             {
  225.                 return;
  226.             }
  227.  
  228.             // If it's off and we want it to be on...
  229.             if (CameraHandler.GetCameraList().Count > 0)
  230.             {
  231.                 if (status)
  232.                 {
  233.                     CameraHandler.OpenSession(CameraHandler.GetCameraList()[0]);
  234.  
  235.                     // Configure the camera
  236.                     switch (Settings.CameraWb)
  237.                     {
  238.                         case "Auto": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Auto); break;
  239.                         case "Daylight": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Daylight); break;
  240.                         case "Cloudy": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Cloudy); break;
  241.                         case "Tangsten": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Tangsten); break;
  242.                         case "Fluorescent": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Fluorescent); break;
  243.                         case "Strobe": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Strobe); break;
  244.                         case "WhitePaper": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_WhitePaper); break;
  245.                         case "Shade": CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Shade); break;
  246.                         default:
  247.                             break;
  248.                     }
  249.                     CameraHandler.SetSetting(EDSDK.PropID_Av, CameraValues.AV(Settings.CameraAv));
  250.                     CameraHandler.SetSetting(EDSDK.PropID_Tv, CameraValues.TV(Settings.CameraTv));
  251.                     CameraHandler.SetSetting(EDSDK.PropID_ISOSpeed, CameraValues.ISO(Settings.CameraISOSpeed));
  252.                     CameraHandler.SetSetting(EDSDK.PropID_WhiteBalance, EDSDK.WhiteBalance_Auto);
  253.                     CameraHandler.SetSetting(EDSDK.PropID_SaveTo, (uint)EDSDK.EdsSaveTo.Host);
  254.                     CameraHandler.SetCapacity();
  255.  
  256.                     // Restart the camera
  257.                     if (!CameraHandler.IsFilming)
  258.                     {
  259.                         if (!CameraHandler.IsLiveViewOn)
  260.                         {
  261.                             CameraHandler.StartLiveView();
  262.                         }
  263.                         else
  264.                         {
  265.                             CameraHandler.StopLiveView();
  266.                             CameraHandler.StartLiveView();
  267.                         }
  268.                     }
  269.                 }
  270.             }
  271.         }
  272.         #endregion
  273.  
  274.        
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement