Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Android.App;
- using Android.Widget;
- using Android.OS;
- namespace cameraapp
- {
- using System;
- using System.Collections.Generic;
- using Android.App;
- using Android.Content;
- using Android.Content.PM;
- using Android.Graphics;
- using Android.OS;
- using Android.Provider;
- using Android.Widget;
- using System.Linq;
- using System.Text;
- using Android.Locations;
- using Android.Util;
- using Java.IO;
- using Environment = Android.OS.Environment;
- using Uri = Android.Net.Uri;
- namespace Chapter21_2DistanceToLondon
- {
- [Activity(Label = "Chapter21_2DistanceToLondon", MainLauncher = true)]
- public class MainActivity : Activity, ILocationListener
- {
- TextView addressText;
- TextView locationText;
- TextView distanceText;
- Location currentLocation;
- LocationManager locationManager;
- string locationProvider = string.Empty;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- Log.Debug("Tag", locationProvider);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- addressText = FindViewById<TextView>(Resource.Id.adressText);
- locationText = FindViewById<TextView>(Resource.Id.gpsLocationText);
- distanceText = FindViewById<TextView>(Resource.Id.distanceText);
- // initialise the location manager
- locationManager = (LocationManager)GetSystemService(LocationService);
- // define its Criteria
- Criteria criteriaForLocationService = new Criteria
- {
- Accuracy = Accuracy.Coarse,
- PowerRequirement = Power.Medium
- };
- // find a location provider (GPS, wi-fi, etc.)
- IList<string> acceptableLocationProviders =
- locationManager.GetProviders(criteriaForLocationService, true);
- // if we have any, use the first one
- if (acceptableLocationProviders.Any())
- locationProvider = acceptableLocationProviders.First();
- else
- locationProvider = string.Empty;
- } // end OnCreate
- protected override void OnResume()
- {
- base.OnResume();
- if (locationProvider != string.Empty) locationManager.RequestLocationUpdates(locationProvider, 0, 0, this);
- } // end OnResume
- protected override void OnPause()
- {
- base.OnPause();
- locationManager.RemoveUpdates(this);
- } // end OnPause
- public void OnLocationChanged(Location location)
- {
- try
- {
- currentLocation = location;
- if (currentLocation == null)
- locationText.Text = "Location not found";
- else
- {
- locationText.Text = String.Format("{0},{1}",
- currentLocation.Latitude, currentLocation.Longitude);
- Android.Locations.Geocoder geocoder = new Geocoder(this);
- // this gets the address from the Internet
- IList<Address> addressList =
- geocoder.GetFromLocation(currentLocation.Latitude,
- currentLocation.Longitude, 10);
- Address address = addressList.FirstOrDefault();
- // Found location - don't need address to calc distance
- // Trafalgar Square is at: 51.50 -0.13
- double lat1 = currentLocation.Latitude;
- double theta = currentLocation.Longitude - (-0.13);
- double distance = Math.Sin(Math.PI / 180.0 * (lat1))
- * Math.Sin(Math.PI / 180.0 * (51.50)) +
- Math.Cos(Math.PI / 180.0 * (lat1)) *
- Math.Cos(Math.PI / 180.0 * (51.50)) *
- Math.Cos(Math.PI / 180.0 * (theta));
- distance = Math.Acos(distance);
- distance = distance / Math.PI * 180.0;
- distance = distance * 60 * 1.15;
- distanceText.Text = "Distance: " +
- distance.ToString() + " miles";
- // display address
- if (address != null)
- {
- StringBuilder deviceAddress = new StringBuilder();
- for (int i = 0; i < address.MaxAddressLineIndex; i++)
- {
- deviceAddress.Append(address.GetAddressLine(i)).AppendLine(",");
- }
- addressText.Text = deviceAddress.ToString();
- } // end if
- else addressText.Text = "address not found";
- }
- }
- catch
- {
- addressText.Text = "address not found";
- } // end catch
- } // end onLocationChanged
- public void OnStatusChanged(string provider, Availability status, Bundle extras)
- {
- }
- public void OnProviderDisabled(string provider)
- {
- }
- public void OnProviderEnabled(string provider)
- {
- }
- }
- }
- public static class App
- {
- public static File _file;
- public static File _dir;
- public static Bitmap bitmap;
- }
- [Activity(Label = "cameraapp", MainLauncher = true)]
- public class MainActivity : Activity
- {
- private ImageView _imageView;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- //
- var edtFirstName = FindViewById<EditText>(Resource.Id.edtFirstName);
- var btnDisplay = FindViewById<Button>(Resource.Id.btnDisplay);
- var txtResult = FindViewById<TextView>(Resource.Id.txtResult);
- btnDisplay.Click += (e, o) =>
- {
- string firstName = edtFirstName.Text;
- txtResult.Text = "Nuotraukos komentaras: " + firstName;
- };
- //
- if (IsThereAnAppToTakePictures())
- {
- CreateDirectoryForPictures();
- Button button = FindViewById<Button>(Resource.Id.myButton);
- _imageView = FindViewById<ImageView>(Resource.Id.imageView1);
- button.Click += TakeAPicture;
- }
- }
- protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
- {
- base.OnActivityResult(requestCode, resultCode, data);
- Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
- Uri contentUri = Uri.FromFile(App._file);
- mediaScanIntent.SetData(contentUri);
- SendBroadcast(mediaScanIntent);
- int height = Resources.DisplayMetrics.HeightPixels;
- int width = _imageView.Height;
- App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height);
- if (App.bitmap != null)
- {
- _imageView.SetImageBitmap(App.bitmap);
- App.bitmap = null;
- }
- GC.Collect();
- }
- private void CreateDirectoryForPictures()
- {
- App._dir = new File(
- Environment.GetExternalStoragePublicDirectory(
- Environment.DirectoryPictures), "CameraAppDemo");
- if (!App._dir.Exists())
- {
- App._dir.Mkdirs();
- }
- }
- private bool IsThereAnAppToTakePictures()
- {
- Intent intent = new Intent(MediaStore.ActionImageCapture);
- IList<ResolveInfo> availableActivities =
- PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
- return availableActivities != null && availableActivities.Count > 0;
- }
- private void TakeAPicture(object sender, EventArgs eventArgs)
- {
- Intent intent = new Intent(MediaStore.ActionImageCapture);
- App._file = new File(App._dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));
- intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(App._file));
- StartActivityForResult(intent, 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement