Advertisement
Guest User

Windows Phone 8 SDK kamerás alkalmazás

a guest
Jan 7th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Navigation;
  8. using Microsoft.Phone.Controls;
  9. using Microsoft.Phone.Shell;
  10. using Microsoft.Devices;
  11. using Microsoft.Xna.Framework.Media;
  12. using System.IO.IsolatedStorage;
  13. using System.IO;
  14.  
  15. namespace Find_My_Car
  16. {
  17.     public partial class takePhoto : PhoneApplicationPage
  18.     {
  19.         private int savedCounter = 0;
  20.         PhotoCamera cam;
  21.         MediaLibrary library = new MediaLibrary();
  22.  
  23.         public takePhoto()
  24.         {
  25.             InitializeComponent();
  26.             BuildLocalizedApplicationBar();
  27.         }
  28.  
  29.         protected override void OnNavigatedTo(NavigationEventArgs e)
  30.         {
  31.             if(PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true)
  32.             {
  33.                 cam = new PhotoCamera(CameraType.Primary);
  34.  
  35.                 //Esemény amikor a PhotoCamera objektum inicializálta magát
  36.                 cam.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(cam_Initialized);
  37.  
  38.                 //Esemény amikor a felvétel befejeződött
  39.                 cam.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_CaptureCompleted);
  40.  
  41.                 cam.CaptureImageAvailable += new EventHandler<ContentReadyEventArgs>(cam_CaptureImageAvailable);
  42.  
  43.                 cam.CaptureThumbnailAvailable += new EventHandler<ContentReadyEventArgs>(cam_CaptureThumbnailAvailable);
  44.  
  45.                 cam.AutoFocusCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_AutoFocusCompleted);
  46.  
  47.                 viewfinderCanvas.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(focus_tapped);
  48.  
  49.                 CameraButtons.ShutterKeyHalfPressed += OnButtonHalfPress;
  50.  
  51.                 CameraButtons.ShutterKeyPressed += OnButtonFullPress;
  52.  
  53.                 CameraButtons.ShutterKeyReleased += OnButtonRelease;
  54.  
  55.                 viewfinderBrush.SetSource(cam);
  56.  
  57.             }
  58.             else
  59.             {
  60.                 this.Dispatcher.BeginInvoke(delegate()
  61.                 {
  62.                     MessageBox.Show("Your device doesn't support this feature.");
  63.                 });
  64.                 NavigationService.Navigate(new Uri("MainPage.xaml", UriKind.Relative));
  65.             }
  66.            
  67.         }
  68.  
  69.         protected override void OnNavigatedFrom(NavigationEventArgs e)
  70.         {
  71.             if(cam != null)
  72.             {
  73.                 cam.Dispose();
  74.  
  75.                 cam.Initialized -= cam_Initialized;
  76.                 cam.CaptureCompleted -= cam_CaptureCompleted;
  77.                 cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
  78.                 cam.CaptureThumbnailAvailable -= cam_CaptureThumbnailAvailable;
  79.                 CameraButtons.ShutterKeyHalfPressed -= OnButtonHalfPress;
  80.                 CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
  81.                 CameraButtons.ShutterKeyReleased -= OnButtonRelease;
  82.             }
  83.         }
  84.  
  85.         private void OnButtonRelease(object sender, EventArgs e)
  86.         {
  87.         }
  88.  
  89.         private void OnButtonFullPress(object sender, EventArgs e)
  90.         {
  91.             if (cam != null)
  92.             {
  93.                 try
  94.                 {
  95.                     cam.CaptureImage();
  96.                 }
  97.                 catch (Exception ex)
  98.                 {
  99.                     MessageBox.Show(ex.Message);
  100.                 }
  101.             }
  102.         }
  103.  
  104.         private void OnButtonHalfPress(object sender, EventArgs e)
  105.         {
  106.  
  107.         }
  108.  
  109.         private void focus_tapped(object sender, System.Windows.Input.GestureEventArgs e)
  110.         {
  111.             //MessageBox.Show("Focus tapped");
  112.             if (cam != null)
  113.             {
  114.                 if (cam.IsFocusAtPointSupported == true)
  115.                 {
  116.                     try
  117.                     {
  118.                         // Megszerezzük az érintés pozícióját
  119.                         Point tapLocation = e.GetPosition(viewfinderCanvas);
  120.  
  121.                         // Position focus brackets with estimated offsets.
  122.                         focusBrackets.SetValue(Canvas.LeftProperty, tapLocation.X - 30);
  123.                         focusBrackets.SetValue(Canvas.TopProperty, tapLocation.Y - 28);
  124.  
  125.                         // Determine focus point.
  126.                         double focusXPercentage = tapLocation.X / viewfinderCanvas.Width;
  127.                         double focusYPercentage = tapLocation.Y / viewfinderCanvas.Height;
  128.  
  129.                         // Show focus brackets and focus at point
  130.                         focusBrackets.Visibility = Visibility.Visible;
  131.                         cam.FocusAtPoint(focusXPercentage, focusYPercentage);
  132.                         // Write a message to the UI.
  133.                         this.Dispatcher.BeginInvoke(delegate()
  134.                         {
  135.                             //A kamera fókuszál
  136.                         });
  137.                     }
  138.                     catch (Exception focusError)
  139.                     {
  140.                         // Cannot focus when a capture is in progress.
  141.                         this.Dispatcher.BeginInvoke(delegate()
  142.                         {
  143.                             // Write a message to the UI.
  144.                             MessageBox.Show(focusError.Message);
  145.                             // Hide focus brackets.
  146.                             focusBrackets.Visibility = Visibility.Collapsed;
  147.                         });
  148.                     }
  149.                 }
  150.                 else
  151.                 {
  152.                     // Write a message to the UI.
  153.                     this.Dispatcher.BeginInvoke(delegate()
  154.                     {
  155.                         MessageBox.Show("Camera does not support FocusAtPoint().");
  156.                     });
  157.                 }
  158.             }
  159.         }
  160.  
  161.         private void cam_AutoFocusCompleted(object sender, CameraOperationCompletedEventArgs e)
  162.         {
  163.             Deployment.Current.Dispatcher.BeginInvoke(delegate()
  164.             {
  165.                 //Az autofókusz folyamata végetért
  166.  
  167.                 //A fókuszáló szögl. zárózjelek eltűntetése
  168.                 focusBrackets.Visibility = Visibility.Collapsed;
  169.  
  170.             });
  171.         }
  172.  
  173.         public void cam_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e)
  174.         {
  175.             //thumbnail nekünk nem kell
  176.         }
  177.  
  178.         void cam_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
  179.         {
  180.             string fileName = savedCounter + ".jpg";
  181.  
  182.             try
  183.             {
  184.                 Deployment.Current.Dispatcher.BeginInvoke(delegate()
  185.                 {
  186.                     //Fénykép készen áll, mentés kezdése
  187.                 });
  188.  
  189.                 // kép lementése a filmtekercsbe
  190.  
  191.                 Deployment.Current.Dispatcher.BeginInvoke(delegate()
  192.                 {
  193.                     //a kép lementve a filmtekercsbe
  194.                 });
  195.  
  196.                 //A stream helyét visszaállítjuk az elejére
  197.                 e.ImageStream.Seek(0, System.IO.SeekOrigin.Begin);
  198.  
  199.                 //izolált tárhelyre mentés
  200.                 using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
  201.                 {
  202.                     using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
  203.                     {
  204.                         // Buffer inicializálása
  205.                         byte[] readBuffer = new byte[4096];
  206.                         int bytesRead = -1;
  207.  
  208.                         // A kép másolása az izolált tárhelyre
  209.                         while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
  210.                         {
  211.                             targetStream.Write(readBuffer, 0, bytesRead);
  212.                         }
  213.                     }
  214.                 }
  215.  
  216.                 Deployment.Current.Dispatcher.BeginInvoke(delegate()
  217.                 {
  218.                     //A kép el lett mentve az izolált tárhelyre
  219.  
  220.                 });
  221.             }
  222.             finally
  223.             {
  224.                 e.ImageStream.Close();
  225.             }
  226.         }
  227.  
  228.         void cam_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)
  229.         {
  230.             savedCounter++;
  231.         }
  232.  
  233.         private void cam_Initialized(object sender, CameraOperationCompletedEventArgs e)
  234.         {
  235.             if(!e.Succeeded)
  236.             {
  237.                 MessageBox.Show("Camera initialization error");
  238.             }
  239.         }
  240.  
  241.         private void BuildLocalizedApplicationBar()
  242.         {
  243.             ApplicationBar = new ApplicationBar();
  244.             // yo mann
  245.             ApplicationBarIconButton savePhotoButton = new ApplicationBarIconButton(new Uri("/Assets/appbar.save.png", UriKind.Relative));
  246.             savePhotoButton.Text = "save";
  247.             savePhotoButton.Click += savePhotoButton_Click;
  248.             ApplicationBar.Buttons.Add(savePhotoButton);
  249.         }
  250.  
  251.         void savePhotoButton_Click(object sender, EventArgs e)
  252.         {
  253.             //NavigationService.Navigate(new Uri("/SetLocation.xaml", UriKind.Relative));
  254.             throw new NotImplementedException();
  255.         }
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement