CameraViewfinder.xaml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Animation;
  12. using System.Windows.Shapes;
  13. using Microsoft.Devices;
  14. using System.Windows.Media.Imaging;
  15. using System.Threading;
  16. using System.Diagnostics;
  17. using System.ComponentModel;
  18. using System.Windows.Threading;
  19. using System.IO.IsolatedStorage;
  20. using System.IO;
  21. namespace cameraforhtml
  22. {
  23. public partial class CameraViewfinder : System.Windows.Controls.UserControl
  24. {
  25. private PhotoCamera _camera = null;
  26. public CameraViewfinder()
  27. {
  28. InitializeComponent();
  29. StartCamera();
  30. }
  31. public void StartCamera()
  32. {
  33. if (_camera == null)
  34. {
  35. _camera = new PhotoCamera(CameraType.Primary);
  36. _camera.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(CameraInitialized);
  37. _camera.CaptureImageAvailable += new EventHandler<ContentReadyEventArgs>(_camera_CaptureImageAvailable);
  38. viewfinderBrush.SetSource(_camera);
  39. }
  40. }
  41. public void CaptureImage()
  42. {
  43. // take picture
  44. _camera.CaptureImage();
  45. }
  46. void _camera_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
  47. {
  48. // Save picture as JPEG to isolated storage.
  49. using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
  50. {
  51. using (IsolatedStorageFileStream targetStream = isStore.OpenFile("html_content/capturedimage.jpg", FileMode.Create, FileAccess.Write))
  52. {
  53. // Initialize the buffer for 4KB disk pages.
  54. byte[] readBuffer = new byte[4096];
  55. int bytesRead = -1;
  56. // Copy the image to isolated storage.
  57. while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
  58. {
  59. targetStream.Write(readBuffer, 0, bytesRead);
  60. }
  61. }
  62. }
  63. }
  64. public void StopCamera()
  65. {
  66. if (_camera != null)
  67. {
  68. // Dispose camera to minimize power consumption and to expedite shutdown.
  69. _camera.Dispose();
  70. _camera = null;
  71. }
  72. }
  73. private void CameraInitialized(object sender, CameraOperationCompletedEventArgs e)
  74. {
  75. if (_camera != null)
  76. {
  77. Dispatcher.BeginInvoke(() =>
  78. {
  79. // Set the orientation of the viewfinder.
  80. //this.viewfinderBrushTransformation.Angle = _camera.Orientation;
  81. });
  82. }
  83. }
  84. }
  85. }