Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Navigation;
- using Microsoft.Phone.Controls;
- using Microsoft.Phone.Shell;
- using Microsoft.Devices;
- using Microsoft.Xna.Framework.Media;
- using System.IO.IsolatedStorage;
- using System.IO;
- namespace Find_My_Car
- {
- public partial class takePhoto : PhoneApplicationPage
- {
- private int savedCounter = 0;
- PhotoCamera cam;
- MediaLibrary library = new MediaLibrary();
- public takePhoto()
- {
- InitializeComponent();
- BuildLocalizedApplicationBar();
- }
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- if(PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true)
- {
- cam = new PhotoCamera(CameraType.Primary);
- //Esemény amikor a PhotoCamera objektum inicializálta magát
- cam.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(cam_Initialized);
- //Esemény amikor a felvétel befejeződött
- cam.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_CaptureCompleted);
- cam.CaptureImageAvailable += new EventHandler<ContentReadyEventArgs>(cam_CaptureImageAvailable);
- cam.CaptureThumbnailAvailable += new EventHandler<ContentReadyEventArgs>(cam_CaptureThumbnailAvailable);
- cam.AutoFocusCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_AutoFocusCompleted);
- viewfinderCanvas.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(focus_tapped);
- CameraButtons.ShutterKeyHalfPressed += OnButtonHalfPress;
- CameraButtons.ShutterKeyPressed += OnButtonFullPress;
- CameraButtons.ShutterKeyReleased += OnButtonRelease;
- viewfinderBrush.SetSource(cam);
- }
- else
- {
- this.Dispatcher.BeginInvoke(delegate()
- {
- MessageBox.Show("Your device doesn't support this feature.");
- });
- NavigationService.Navigate(new Uri("MainPage.xaml", UriKind.Relative));
- }
- }
- protected override void OnNavigatedFrom(NavigationEventArgs e)
- {
- if(cam != null)
- {
- cam.Dispose();
- cam.Initialized -= cam_Initialized;
- cam.CaptureCompleted -= cam_CaptureCompleted;
- cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
- cam.CaptureThumbnailAvailable -= cam_CaptureThumbnailAvailable;
- CameraButtons.ShutterKeyHalfPressed -= OnButtonHalfPress;
- CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
- CameraButtons.ShutterKeyReleased -= OnButtonRelease;
- }
- }
- private void OnButtonRelease(object sender, EventArgs e)
- {
- }
- private void OnButtonFullPress(object sender, EventArgs e)
- {
- if (cam != null)
- {
- try
- {
- cam.CaptureImage();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- private void OnButtonHalfPress(object sender, EventArgs e)
- {
- }
- private void focus_tapped(object sender, System.Windows.Input.GestureEventArgs e)
- {
- //MessageBox.Show("Focus tapped");
- if (cam != null)
- {
- if (cam.IsFocusAtPointSupported == true)
- {
- try
- {
- // Megszerezzük az érintés pozícióját
- Point tapLocation = e.GetPosition(viewfinderCanvas);
- // Position focus brackets with estimated offsets.
- focusBrackets.SetValue(Canvas.LeftProperty, tapLocation.X - 30);
- focusBrackets.SetValue(Canvas.TopProperty, tapLocation.Y - 28);
- // Determine focus point.
- double focusXPercentage = tapLocation.X / viewfinderCanvas.Width;
- double focusYPercentage = tapLocation.Y / viewfinderCanvas.Height;
- // Show focus brackets and focus at point
- focusBrackets.Visibility = Visibility.Visible;
- cam.FocusAtPoint(focusXPercentage, focusYPercentage);
- // Write a message to the UI.
- this.Dispatcher.BeginInvoke(delegate()
- {
- //A kamera fókuszál
- });
- }
- catch (Exception focusError)
- {
- // Cannot focus when a capture is in progress.
- this.Dispatcher.BeginInvoke(delegate()
- {
- // Write a message to the UI.
- MessageBox.Show(focusError.Message);
- // Hide focus brackets.
- focusBrackets.Visibility = Visibility.Collapsed;
- });
- }
- }
- else
- {
- // Write a message to the UI.
- this.Dispatcher.BeginInvoke(delegate()
- {
- MessageBox.Show("Camera does not support FocusAtPoint().");
- });
- }
- }
- }
- private void cam_AutoFocusCompleted(object sender, CameraOperationCompletedEventArgs e)
- {
- Deployment.Current.Dispatcher.BeginInvoke(delegate()
- {
- //Az autofókusz folyamata végetért
- //A fókuszáló szögl. zárózjelek eltűntetése
- focusBrackets.Visibility = Visibility.Collapsed;
- });
- }
- public void cam_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e)
- {
- //thumbnail nekünk nem kell
- }
- void cam_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
- {
- string fileName = savedCounter + ".jpg";
- try
- {
- Deployment.Current.Dispatcher.BeginInvoke(delegate()
- {
- //Fénykép készen áll, mentés kezdése
- });
- // kép lementése a filmtekercsbe
- Deployment.Current.Dispatcher.BeginInvoke(delegate()
- {
- //a kép lementve a filmtekercsbe
- });
- //A stream helyét visszaállítjuk az elejére
- e.ImageStream.Seek(0, System.IO.SeekOrigin.Begin);
- //izolált tárhelyre mentés
- using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
- {
- using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
- {
- // Buffer inicializálása
- byte[] readBuffer = new byte[4096];
- int bytesRead = -1;
- // A kép másolása az izolált tárhelyre
- while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
- {
- targetStream.Write(readBuffer, 0, bytesRead);
- }
- }
- }
- Deployment.Current.Dispatcher.BeginInvoke(delegate()
- {
- //A kép el lett mentve az izolált tárhelyre
- });
- }
- finally
- {
- e.ImageStream.Close();
- }
- }
- void cam_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)
- {
- savedCounter++;
- }
- private void cam_Initialized(object sender, CameraOperationCompletedEventArgs e)
- {
- if(!e.Succeeded)
- {
- MessageBox.Show("Camera initialization error");
- }
- }
- private void BuildLocalizedApplicationBar()
- {
- ApplicationBar = new ApplicationBar();
- // yo mann
- ApplicationBarIconButton savePhotoButton = new ApplicationBarIconButton(new Uri("/Assets/appbar.save.png", UriKind.Relative));
- savePhotoButton.Text = "save";
- savePhotoButton.Click += savePhotoButton_Click;
- ApplicationBar.Buttons.Add(savePhotoButton);
- }
- void savePhotoButton_Click(object sender, EventArgs e)
- {
- //NavigationService.Navigate(new Uri("/SetLocation.xaml", UriKind.Relative));
- throw new NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement