MainPage.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Controls;
  13. using System.IO.IsolatedStorage;
  14. using System.IO;
  15. using System.Windows.Resources;
  16. using System.Windows.Threading;
  17. namespace cameraforhtml
  18. {
  19. public partial class MainPage : PhoneApplicationPage
  20. {
  21. private DispatcherTimer _timer = null;
  22. // Constructor
  23. public MainPage()
  24. {
  25. InitializeComponent();
  26. SaveFilesToIsoStore();
  27. webBrowser.ScriptNotify += new EventHandler<NotifyEventArgs>(webBrowser_ScriptNotify);
  28. }
  29. override protected void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
  30. {
  31. webBrowser.Navigate(new Uri("html_content/index.html", UriKind.Relative));
  32. }
  33. void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
  34. {
  35. string val = e.Value;
  36. System.Diagnostics.Debug.WriteLine(val);
  37. }
  38. private void SaveFilesToIsoStore()
  39. {
  40. //These files must match what is included in the application package,
  41. //or BinaryStream.Dispose below will throw an exception.
  42. string[] files = {
  43. "html_content/index.html",
  44. "html_content/script.js",
  45. "html_content/default.png"
  46. };
  47. IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
  48. foreach (string f in files)
  49. {
  50. StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
  51. using (BinaryReader br = new BinaryReader(sr.Stream))
  52. {
  53. byte[] data = br.ReadBytes((int)sr.Stream.Length);
  54. SaveToIsoStore(f, data);
  55. }
  56. }
  57. }
  58. private void SaveToIsoStore(string fileName, byte[] data)
  59. {
  60. string strBaseDir = string.Empty;
  61. string delimStr = "/";
  62. char[] delimiter = delimStr.ToCharArray();
  63. string[] dirsPath = fileName.Split(delimiter);
  64. //Get the IsoStore.
  65. IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
  66. //Re-create the directory structure.
  67. for (int i = 0; i < dirsPath.Length - 1; i++)
  68. {
  69. strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
  70. isoStore.CreateDirectory(strBaseDir);
  71. }
  72. //Remove the existing file.
  73. if (isoStore.FileExists(fileName))
  74. {
  75. isoStore.DeleteFile(fileName);
  76. }
  77. //Write the file.
  78. using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
  79. {
  80. bw.Write(data);
  81. bw.Close();
  82. }
  83. }
  84. private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  85. {
  86. if (this.CameraViewfinder.Visibility == System.Windows.Visibility.Visible)
  87. {
  88. this.CameraViewfinder.CaptureImage();
  89. this.CameraViewfinder.Visibility = System.Windows.Visibility.Collapsed;
  90. if (_timer == null)
  91. {
  92. _timer = new DispatcherTimer();
  93. _timer.Interval = new TimeSpan(0, 0, 0, 0, 3000);
  94. _timer.Tick += new EventHandler(AlertShowingTimer_Tick);
  95. }
  96. _timer.Stop();
  97. _timer.Start();
  98. }
  99. else
  100. {
  101. webBrowser.InvokeScript("resetImage", new String[] {});
  102. this.CameraViewfinder.Visibility = System.Windows.Visibility.Visible;
  103. }
  104. }
  105. private void AlertShowingTimer_Tick(object sender, EventArgs e)
  106. {
  107. (sender as DispatcherTimer).Stop();
  108. webBrowser.InvokeScript("reloadImage", new String[] { "capturedimage.jpg" });
  109. }
  110. }
  111. }