Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. using Android.App;
  2. using Android.Widget;
  3. using Android.OS;
  4.  
  5. namespace cameraapp
  6. {
  7. using System;
  8. using System.Collections.Generic;
  9. using Android.App;
  10. using Android.Content;
  11. using Android.Content.PM;
  12. using Android.Graphics;
  13. using Android.OS;
  14. using Android.Provider;
  15. using Android.Widget;
  16. using System.Linq;
  17. using System.Text;
  18. using Android.Locations;
  19. using Android.Util;
  20.  
  21. using Java.IO;
  22. using Environment = Android.OS.Environment;
  23. using Uri = Android.Net.Uri;
  24. namespace Chapter21_2DistanceToLondon
  25.  
  26. {
  27.  
  28. [Activity(Label = "Chapter21_2DistanceToLondon", MainLauncher = true)]
  29.  
  30. public class MainActivity : Activity, ILocationListener
  31.  
  32. {
  33.  
  34. TextView addressText;
  35.  
  36. TextView locationText;
  37.  
  38. TextView distanceText;
  39.  
  40. Location currentLocation;
  41.  
  42. LocationManager locationManager;
  43.  
  44. string locationProvider = string.Empty;
  45.  
  46.  
  47.  
  48.  
  49.  
  50. protected override void OnCreate(Bundle bundle)
  51.  
  52. {
  53.  
  54. base.OnCreate(bundle);
  55.  
  56.  
  57.  
  58. Log.Debug("Tag", locationProvider);
  59.  
  60. // Set our view from the "main" layout resource
  61.  
  62. SetContentView(Resource.Layout.Main);
  63.  
  64. addressText = FindViewById<TextView>(Resource.Id.adressText);
  65.  
  66. locationText = FindViewById<TextView>(Resource.Id.gpsLocationText);
  67.  
  68. distanceText = FindViewById<TextView>(Resource.Id.distanceText);
  69.  
  70.  
  71.  
  72. // initialise the location manager
  73.  
  74. locationManager = (LocationManager)GetSystemService(LocationService);
  75.  
  76.  
  77.  
  78. // define its Criteria
  79.  
  80. Criteria criteriaForLocationService = new Criteria
  81.  
  82. {
  83.  
  84. Accuracy = Accuracy.Coarse,
  85.  
  86. PowerRequirement = Power.Medium
  87.  
  88. };
  89.  
  90.  
  91.  
  92. // find a location provider (GPS, wi-fi, etc.)
  93.  
  94. IList<string> acceptableLocationProviders =
  95.  
  96. locationManager.GetProviders(criteriaForLocationService, true);
  97.  
  98.  
  99.  
  100. // if we have any, use the first one
  101.  
  102. if (acceptableLocationProviders.Any())
  103.  
  104. locationProvider = acceptableLocationProviders.First();
  105.  
  106. else
  107.  
  108. locationProvider = string.Empty;
  109.  
  110.  
  111.  
  112. } // end OnCreate
  113.  
  114.  
  115.  
  116. protected override void OnResume()
  117.  
  118. {
  119.  
  120. base.OnResume();
  121.  
  122. if (locationProvider != string.Empty) locationManager.RequestLocationUpdates(locationProvider, 0, 0, this);
  123.  
  124. } // end OnResume
  125.  
  126.  
  127.  
  128. protected override void OnPause()
  129.  
  130. {
  131.  
  132. base.OnPause();
  133.  
  134. locationManager.RemoveUpdates(this);
  135.  
  136. } // end OnPause
  137.  
  138.  
  139.  
  140. public void OnLocationChanged(Location location)
  141.  
  142. {
  143.  
  144. try
  145.  
  146. {
  147.  
  148. currentLocation = location;
  149.  
  150. if (currentLocation == null)
  151.  
  152. locationText.Text = "Location not found";
  153.  
  154. else
  155.  
  156. {
  157.  
  158. locationText.Text = String.Format("{0},{1}",
  159.  
  160. currentLocation.Latitude, currentLocation.Longitude);
  161.  
  162.  
  163.  
  164. Android.Locations.Geocoder geocoder = new Geocoder(this);
  165.  
  166. // this gets the address from the Internet
  167.  
  168. IList<Address> addressList =
  169.  
  170. geocoder.GetFromLocation(currentLocation.Latitude,
  171.  
  172. currentLocation.Longitude, 10);
  173.  
  174. Address address = addressList.FirstOrDefault();
  175.  
  176.  
  177.  
  178. // Found location - don't need address to calc distance
  179.  
  180. // Trafalgar Square is at: 51.50 -0.13
  181.  
  182. double lat1 = currentLocation.Latitude;
  183.  
  184. double theta = currentLocation.Longitude - (-0.13);
  185.  
  186. double distance = Math.Sin(Math.PI / 180.0 * (lat1))
  187.  
  188. * Math.Sin(Math.PI / 180.0 * (51.50)) +
  189.  
  190. Math.Cos(Math.PI / 180.0 * (lat1)) *
  191.  
  192. Math.Cos(Math.PI / 180.0 * (51.50)) *
  193.  
  194. Math.Cos(Math.PI / 180.0 * (theta));
  195.  
  196. distance = Math.Acos(distance);
  197.  
  198. distance = distance / Math.PI * 180.0;
  199.  
  200. distance = distance * 60 * 1.15;
  201.  
  202.  
  203.  
  204. distanceText.Text = "Distance: " +
  205.  
  206. distance.ToString() + " miles";
  207.  
  208.  
  209.  
  210. // display address
  211.  
  212.  
  213.  
  214. if (address != null)
  215.  
  216. {
  217.  
  218. StringBuilder deviceAddress = new StringBuilder();
  219.  
  220. for (int i = 0; i < address.MaxAddressLineIndex; i++)
  221.  
  222. {
  223.  
  224. deviceAddress.Append(address.GetAddressLine(i)).AppendLine(",");
  225.  
  226. }
  227.  
  228. addressText.Text = deviceAddress.ToString();
  229.  
  230. } // end if
  231.  
  232. else addressText.Text = "address not found";
  233.  
  234. }
  235.  
  236. }
  237.  
  238. catch
  239.  
  240. {
  241.  
  242. addressText.Text = "address not found";
  243.  
  244. } // end catch
  245.  
  246. } // end onLocationChanged
  247.  
  248.  
  249.  
  250. public void OnStatusChanged(string provider, Availability status, Bundle extras)
  251.  
  252. {
  253.  
  254. }
  255.  
  256.  
  257.  
  258. public void OnProviderDisabled(string provider)
  259.  
  260. {
  261.  
  262. }
  263.  
  264.  
  265.  
  266. public void OnProviderEnabled(string provider)
  267.  
  268. {
  269.  
  270. }
  271.  
  272.  
  273.  
  274. }
  275.  
  276. }
  277. public static class App
  278. {
  279. public static File _file;
  280. public static File _dir;
  281. public static Bitmap bitmap;
  282. }
  283.  
  284.  
  285. [Activity(Label = "cameraapp", MainLauncher = true)]
  286. public class MainActivity : Activity
  287. {
  288. private ImageView _imageView;
  289. protected override void OnCreate(Bundle bundle)
  290. {
  291.  
  292.  
  293. base.OnCreate(bundle);
  294. // Set our view from the "main" layout resource
  295. SetContentView(Resource.Layout.Main);
  296. //
  297. var edtFirstName = FindViewById<EditText>(Resource.Id.edtFirstName);
  298. var btnDisplay = FindViewById<Button>(Resource.Id.btnDisplay);
  299. var txtResult = FindViewById<TextView>(Resource.Id.txtResult);
  300.  
  301. btnDisplay.Click += (e, o) =>
  302. {
  303. string firstName = edtFirstName.Text;
  304. txtResult.Text = "Nuotraukos komentaras: " + firstName;
  305. };
  306. //
  307. if (IsThereAnAppToTakePictures())
  308. {
  309. CreateDirectoryForPictures();
  310. Button button = FindViewById<Button>(Resource.Id.myButton);
  311. _imageView = FindViewById<ImageView>(Resource.Id.imageView1);
  312. button.Click += TakeAPicture;
  313. }
  314. }
  315. protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
  316. {
  317. base.OnActivityResult(requestCode, resultCode, data);
  318. Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
  319. Uri contentUri = Uri.FromFile(App._file);
  320. mediaScanIntent.SetData(contentUri);
  321. SendBroadcast(mediaScanIntent);
  322. int height = Resources.DisplayMetrics.HeightPixels;
  323. int width = _imageView.Height;
  324. App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height);
  325. if (App.bitmap != null)
  326. {
  327. _imageView.SetImageBitmap(App.bitmap);
  328. App.bitmap = null;
  329. }
  330. GC.Collect();
  331. }
  332. private void CreateDirectoryForPictures()
  333. {
  334. App._dir = new File(
  335. Environment.GetExternalStoragePublicDirectory(
  336. Environment.DirectoryPictures), "CameraAppDemo");
  337. if (!App._dir.Exists())
  338. {
  339. App._dir.Mkdirs();
  340. }
  341. }
  342. private bool IsThereAnAppToTakePictures()
  343. {
  344. Intent intent = new Intent(MediaStore.ActionImageCapture);
  345. IList<ResolveInfo> availableActivities =
  346. PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
  347. return availableActivities != null && availableActivities.Count > 0;
  348. }
  349. private void TakeAPicture(object sender, EventArgs eventArgs)
  350. {
  351. Intent intent = new Intent(MediaStore.ActionImageCapture);
  352. App._file = new File(App._dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));
  353. intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(App._file));
  354. StartActivityForResult(intent, 0);
  355. }
  356. }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement