/// <summary> /// Make call to camera /// </summary> /// <returns></returns> public async System.Threading.Tasks.Task <string[]> GetImageFromCameraCallAsync() { if (!(Plugin.Media.CrossMedia.Current.IsCameraAvailable && Plugin.Media.CrossMedia.Current.IsTakePhotoSupported)) { // <!-- If photo taking isn't supported, return with blank array --> await Acr.UserDialogs.UserDialogs.Instance.AlertAsync(""); } // <!-- Options related to image storage and formatting --> Plugin.Media.Abstractions.StoreCameraMediaOptions mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "SGDPhotos", Name = $"{System.DateTime.UtcNow}.png", PhotoSize = Plugin.Media.Abstractions.PhotoSize.Small, CompressionQuality = 50, // <!-- These below largely have to be handled explicitly in Android SaveToAlbum = true, AllowCropping = true, RotateImage = true // --> }; return(await GetImageAndCrop(mediaOptions)); }
private async void TomarFoto() { try { string rutaFoto; await CrossMedia.Current.Initialize(); if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { Plugin.Media.Abstractions.StoreCameraMediaOptions opciones = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); var foto = await CrossMedia.Current.TakePhotoAsync(opciones); if (foto != null) { rutaFoto = foto.Path; byte[] array = File.ReadAllBytes(rutaFoto); Imagen = Convert.ToBase64String(array); } } } catch (Exception ex) { MostrarMensaje("No fue posible tomar la foto!"); } }
private async void pictureButton_Clicked(object sender, System.EventArgs e) { await CrossMedia.Current.Initialize(); if (!Plugin.Media.CrossMedia.Current.IsCameraAvailable || !Plugin.Media.CrossMedia.Current.IsTakePhotoSupported) { return; } var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "PictureTest", // 保存先ディレクトリ Name = $"{DateTime.UtcNow}.jpg" // 保存ファイル名 }; var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); if (file == null) { return; } image.Source = ImageSource.FromStream(() => { var stream = file.GetStream(); return(stream); }); //試しにローカルのファイルパスを指定してみる string filepath = "samplephoto/bill.png"; ReadText(filepath); }
private async void ExecuteTirarFotoCommandAsync() { if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { // Supply media options for saving our photo after it's taken. var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Pictures", Name = $"{DateTime.UtcNow}.jpg" }; // Take a photo of the business receipt. var foto = await CrossMedia.Current.TakePhotoAsync(mediaOptions); if (foto == null) { return; } ImagemOferta = ImageSource.FromStream(() => { var stream = foto.GetStream(); foto.Dispose(); return(stream); }); } }
public async void TakePhoto() { if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { System.Diagnostics.Debug.WriteLine("Camera available!"); var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { SaveToAlbum = false, SaveMetaData = false }; var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); if (file != null) { System.Diagnostics.Debug.WriteLine(file.Path); imageFile = file; UserPic.Source = ImageSource.FromStream(() => { return(file.GetStream()); }); return; } else { return; } } }
public async Task TakePicture() { if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "FaultPictures", Name = $"{DateTime.UtcNow}.jpg" }; try { var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); await InsertFaultPicture(file); } catch (TaskCanceledException) { IsBusy = false; } catch (AggregateException) { IsBusy = false; } } }
private async void Button_Clicked(object sender, System.EventArgs e) { // Alternatively choose picture from gallery //{ // if (CrossMedia.Current.IsPickPhotoSupported) // { // var image = await CrossMedia.Current.PickPhotoAsync(); // Image.Source = ImageSource.FromStream(() => // { // return image.GetStream(); // }); // MakeAnalysisRequest(image.Path); // } Plugin.Media.Abstractions.StoreCameraMediaOptions options = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); var image = await CrossMedia.Current.TakePhotoAsync(options); // Image.Source = ImageSource.FromStream(() => // { // return image.GetStream(); // }); Image.Source = image.Path; ResultStack.IsVisible = true; MakeAnalysisRequest(image.Path); }
private async Task AddNewImage() { try { Plugin.Media.Abstractions.StoreCameraMediaOptions options = new Plugin.Media.Abstractions.StoreCameraMediaOptions { AllowCropping = true, Name = Guid.NewGuid().ToString() }; // Get photo from camera Plugin.Media.Abstractions.MediaFile?photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(options); if (!String.IsNullOrWhiteSpace(photo?.Path) && File.Exists(photo.Path)) { byte[] data = await File.ReadAllBytesAsync(photo.Path); ViewModel.AddNewImage(data); File.Delete(photo.Path); } } catch (Exception exc) { Debug.WriteLine($"Error taking picture: {exc.Message}"); } }
private async void Button_Clicked(object sender, EventArgs e) { Plugin.Media.Abstractions.StoreCameraMediaOptions options = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); var image = await CrossMedia.Current.TakePhotoAsync(options); pic.Source = image.Path; }
// Take picture with camera, transform it to grayscale and show it on canvas async void TakeAndShowPicture(object sender, EventArgs args) { if (flagguidelinesphoto == false) { await DisplayAlert("To achieve suitable results...", "- write on white background\n" + "- use thick stroke\n" + "- center with camera zoom\n" + "- avoid shadows\n" + "- fill 80% of height", "OK"); flagguidelinesphoto = true; } // Initialite cam handling await CrossMedia.Current.Initialize(); // Check if cam is available if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await DisplayAlert("Error", "No camera available.", "OK"); return; } // Define options for camera plugin var options = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); // On iOS it's possible to take a squared pic rendered to 28x28 directly if (Device.RuntimePlatform == Device.iOS) { options.AllowCropping = true; options.PhotoSize = Plugin.Media.Abstractions.PhotoSize.MaxWidthHeight; options.MaxWidthHeight = 28; } // Take picture var photo = await CrossMedia.Current.TakePhotoAsync(options); // Convert to resized (28x28) grayscaled bitmap if (photo != null) { // Generate bitmap from media object using (SKManagedStream skstream = new SKManagedStream(photo.GetStream())) { bitmap = SKBitmap.Decode(skstream); } // Convert bitmap to 28x28 pixel grayscale bitmap = ConvertBitmap(bitmap); } // Update canvas view canvasview.InvalidateSurface(); }
async Task TakePhoto() { var opts = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(opts); Image = ImageSource.FromStream(() => photo.GetStream()); var d = await _visionApi.DescribeImageInStreamAsync(photo.GetStream()); Caption = d.Captions.FirstOrDefault().Text; }
public async Task <ImageSource> TakePicture() { var cmo = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(cmo); if (photo != null) { return(ImageSource.FromStream(() => { return photo.GetStream(); })); } return(null); }
private async void TakePhoto() { await CrossMedia.Current.Initialize(); if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "MenuAnalyzer", Name = $"{DateTime.UtcNow}.jpg", PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium }; var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); myActivityIndicator.IsRunning = true; myActivityIndicator.IsVisible = true; using (var client = new HttpClient()) { using (var content = new MultipartFormDataContent()) { byte[] filedata = await(new StreamContent(file.GetStream())).ReadAsByteArrayAsync(); var fileContent = new ByteArrayContent(filedata); using (var message = await client.PostAsync(SettingsReader.GetKey("url"), fileContent)) { try { string input = await message.Content.ReadAsStringAsync(); input = input.Substring(1, input.Length - 2); input = input.Replace("\\", ""); MenuItems mi = Newtonsoft.Json.JsonConvert.DeserializeObject <MenuItems>(input); MyList.ItemsSource = mi.menus; MyList.IsVisible = true; myActivityIndicator.IsRunning = false; myActivityIndicator.IsVisible = false; } catch { myActivityIndicator.IsRunning = false; myActivityIndicator.IsVisible = false; await DisplayAlert("Error", "No text was detected or there was a parsing error, please try again!", "Ok"); } } } } } }
private async void CameraButton_Clicked(object sender, EventArgs e) { Plugin.Media.Abstractions.StoreCameraMediaOptions options = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); options.SaveToAlbum = true; photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(options); if (photo != null) { PhotoImage.Source = ImageSource.FromStream(() => { return(photo.GetStream()); }); } }
/// <summary> /// Get saved image /// </summary> /// <param name="mediaOptions"></param> /// <returns></returns> public static async System.Threading.Tasks.Task <string[]> GetImageAndCrop(Plugin.Media.Abstractions.StoreCameraMediaOptions mediaOptions) { try { using (Plugin.Media.Abstractions.MediaFile file = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(mediaOptions)) { string newPath = ""; System.Diagnostics.Debug.WriteLineIf(App.OutputVerbose, "In Taker"); if (file == null || file.Path == null || file.Path == "") { return(null); } if (System.IO.File.Exists(@file.Path)) { var path = System.IO.Path.GetDirectoryName(@file.Path); if (Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.Android) { newPath = System.IO.Path.Combine(path, System.IO.Path.GetFileNameWithoutExtension(@file.Path) + "crop.jpg"); // <!-- Note: this crops an image to square, since not a default in Android --> Xamarin.Forms.DependencyService.Get <Interfaces.InterfaceBitmapResize>().ResizeBitmaps(@file.Path, @newPath); } else if (Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.iOS) { // <!-- Note: iOS has a center crop option built in --> newPath = System.IO.Path.Combine(path, System.IO.Path.GetFileName(@file.Path)); } var getNamedImageInfo = await Acr.UserDialogs.UserDialogs.Instance.PromptAsync("Name picture"); var getNamedImage = getNamedImageInfo.Text; byte[] imageArray = System.IO.File.ReadAllBytes(@newPath); string base64ImageRepresentation = System.Convert.ToBase64String(imageArray); imageArray = null; return(new string[] { getNamedImage, base64ImageRepresentation }); } return(null); } } catch { return(null); } }
private async void tirarFoto() { if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { var nomeArquivo = $"{DateTime.UtcNow}.jpg"; var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { //Directory = "Cupons", Name = nomeArquivo //SaveToAlbum = true }; // Take a photo of the business receipt. var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); if (file == null) { return; } GastoInfo gasto = (GastoInfo)this.BindingContext; gasto.FotoPath = nomeArquivo; var fs = file.GetStream(); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Dispose(); fs = null; gasto.FotoBase64 = Convert.ToBase64String(buffer); //DisplayAlert("Salvar em", file.Path, "OK"); var path = file.Path; _FotoImage.Source = ImageSource.FromStream(() => { var stream = file.GetStream(); file.Dispose(); return(stream); }); _FotoImage.Source = path; _FotoImage.WidthRequest = TelaUtils.LarguraSemPixel * 0.5; _FotoImage.HeightRequest = TelaUtils.LarguraSemPixel * 0.5; //var gasto = (GastoInfo)BindingContext; //gasto.Foto = Path.GetFileName(path); } else { await DisplayAlert("Dispositivo não possiu camera ou camera desativada", null, "OK"); } }
public async void TakePhoto() // takePhoto.Clicked += async(sender, args) => { try { if (!DoneWashing) { return; } await CrossMedia.Current.Initialize(); if (!CrossMedia.IsSupported || !CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await App.NavPage.CurrentPage.DisplayAlert("Warning", "Camera is not available", "OK"); return; } App.IsCameraUsed = true; var x = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); App.UpdateLoading(false); await Task.Delay(200); var file = await CrossMedia.Current.TakePhotoAsync(x); if (file == null) { return; } var src = ImageSource.FromFile(file.Path); if (src != null) { Photo = src; PhotoTaken = true; file.Dispose(); } } catch (Exception e) { await App.NavPage.CurrentPage.DisplayAlert("Error", e.Message, "OK"); } finally { App.IsCameraUsed = false; App.UpdateLoading(false); } }
private async Task TakePictureAsync() { var options = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); options.PhotoSize = Plugin.Media.Abstractions.PhotoSize.MaxWidthHeight; options.MaxWidthHeight = 1000; var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(options); if (photo != null) { using (var imageStream = photo.GetStream()) { await RecognizeImageWritingAsync(imageStream); } } }
private async void TakePictureAsync() { await RequestStorageWritePermission(); if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { // Supply media options for saving our photo after it's taken. var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Pictures", Name = $"{DateTime.UtcNow}.jpg" }; // Take a photo of the business receipt. var file = await CrossMedia.Current.PickPhotoAsync();// mediaOptions); await MakeRequest(file); } }
async void btnMake_ClickAsync(object sender, System.EventArgs e) { imageve.SetImageDrawable(null); if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Drinks", Name = $"{DateTime.UtcNow}.jpg" }; var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); var filePath = file.Path; bitmap = BitmapFactory.DecodeFile(filePath); imageve.SetImageBitmap(Bitmap.CreateScaledBitmap(bitmap, 300, 500, false)); } Calculate(); }
private async void TakePhoto(object sender, System.EventArgs e) { ResultFrame.IsVisible = false; Plugin.Media.Abstractions.StoreCameraMediaOptions options = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); var image = await CrossMedia.Current.TakePhotoAsync(options); CameraImage.Source = ImageSource.FromStream(() => { return(image.GetStream()); }); CameraImage.HeightRequest = TwitterImage.Height; CameraImageFrame.IsVisible = true; GetFaceIdImage(image.Path); }
async void TakeAPhotoButton_OnClicked(object sender, EventArgs e) { if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await DisplayAlert("No Camera", ":( No camera avaialble.", "OK"); return; } //var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions //{ // PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium, // AllowCropping = true, // Name = "test.jpg" //}); //var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()); string FileName = "photo" + DateTime.Now.Ticks.ToString() + ".png"; var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Name = FileName, PhotoSize = Plugin.Media.Abstractions.PhotoSize.Small, CompressionQuality = 92 }; var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); if (file == null) { return; } await DisplayAlert("File Location", file.Path, "OK"); image.Source = ImageSource.FromStream(() => { var stream = file.GetStream(); file.Dispose(); return(stream); }); }
async void Button_Clicked(System.Object sender, System.EventArgs e) { var options = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); options.DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Rear; this.image.IsVisible = false; using (var result = await CrossMedia.Current.TakePhotoAsync(options)) { if (result != null && result.GetStream() != null) { Debug.WriteLine(result.Path); Debug.WriteLine(result.AlbumPath); Debug.WriteLine(result.GetStream() == null); this.image.Source = ImageSource.FromStream(result.GetStream); this.image.IsVisible = true; } } }
private async void tirarFoto() { if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Cupons", Name = $"{DateTime.UtcNow}.jpg", //SaveToAlbum = true }; // Take a photo of the business receipt. var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); if (file == null) { return; } //DisplayAlert("Salvar em", file.Path, "OK"); var path = file.Path; _fotoPessoal.Source = ImageSource.FromStream(() => { var stream = file.GetStream(); file.Dispose(); return(stream); }); _fotoPessoal.Source = path; _fotoPessoal.WidthRequest = TelaUtils.LarguraSemPixel * 0.5; _fotoPessoal.HeightRequest = TelaUtils.LarguraSemPixel * 0.5; } else { DisplayAlert("Dispositivo não possiu camera ou camera desativada", null, "OK"); } }
public async void TakePhoto() { await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsCameraAvailable) { return; } var options = new Plugin.Media.Abstractions.StoreCameraMediaOptions(); var photo = await CrossMedia.Current.TakePhotoAsync(options); if (photo != null) { Images.Insert(0, ImageSource.FromStream(() => { var stream = photo.GetStream(); photo.Dispose(); return(stream); })); RaisePropertyChanged(nameof(Images)); } }
public override async void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); Console.WriteLine(CrossMedia.IsSupported); if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { // Supply media options for saving our photo after it's taken. var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Receipts", Name = $"{DateTime.UtcNow}.jpg" }; // Take a photo of the business receipt. var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); if (CrossMedia.Current.IsPickPhotoSupported) { var photo = await CrossMedia.Current.PickPhotoAsync(); } } }
//TakePic method for button public async void TakePic(object sender, EventArgs args) { if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported) { //Supply media options for saving our photo after it's taken. var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Receipts", Name = $"{DateTime.UtcNow}.jpg" }; //Get chosen file stream var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); var fileStream = file.GetStream(); //Display loading await DisplayAlert("Loading Result", "Please wait", "OK"); //Send file to ComputerVision var result = await GetImageDescriptionAsync(fileStream); await DisplayAlert("Detection Result", "I think it's " + result.Description.Captions[0].Text, "OK"); } }
public virtual async void LoadImages(String[] imageNames, String[] labels = null) { #if __ANDROID__ || __IOS__ await CrossMedia.Current.Initialize(); #endif #if (__UNIFIED__ && !__IOS__) //NETFX or Xamarin Mac //use default images InvokeOnImagesLoaded(imageNames); #else String[] mats = new String[imageNames.Length]; for (int i = 0; i < mats.Length; i++) { String pickImgString = "Use Image from"; if (labels != null && labels.Length > i) { pickImgString = labels[i]; } bool haveCameraOption = (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported); bool havePickImgOption = CrossMedia.Current.IsPickVideoSupported; String action; if (haveCameraOption & havePickImgOption) { action = await DisplayActionSheet(pickImgString, "Cancel", null, "Default", "Photo Library", "Camera"); } else if (havePickImgOption) { action = await DisplayActionSheet(pickImgString, "Cancel", null, "Default", "Photo Library"); } else { action = "Default"; } if (action.Equals("Default")) { #if __ANDROID__ FileInfo fi = Emgu.TF.Util.AndroidFileAsset.WritePermanantFileAsset(Forms.Context, imageNames[i], "tmp", Emgu.TF.Util.AndroidFileAsset.OverwriteMethod.AlwaysOverwrite); mats[i] = fi.FullName; #else mats[i] = imageNames[i]; #endif } else if (action.Equals("Photo Library")) { var photoResult = await CrossMedia.Current.PickPhotoAsync(); if (photoResult == null) //cancelled { return; } mats[i] = photoResult.Path; } else if (action.Equals("Camera")) { var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Emgu", Name = $"{DateTime.UtcNow}.jpg" }; var takePhotoResult = await CrossMedia.Current.TakePhotoAsync(mediaOptions); if (takePhotoResult == null) //cancelled { return; } mats[i] = takePhotoResult.Path; } } InvokeOnImagesLoaded(mats); #endif }
public virtual async void LoadImages(String[] imageNames, String[] labels = null) { #if __MACOS__ Mat[] mats = new Mat[imageNames.Length]; for (int i = 0; i < mats.Length; i++) { mats[i] = CvInvoke.Imread(imageNames[i], ImreadModes.Color); } InvokeOnImagesLoaded(mats); #else #if __ANDROID__ || __IOS__ await CrossMedia.Current.Initialize(); #endif Mat[] mats = new Mat[imageNames.Length]; for (int i = 0; i < mats.Length; i++) { String pickImgString = "Use Image from"; if (labels != null && labels.Length > i) { pickImgString = labels[i]; } bool haveCameraOption; bool havePickImgOption; if (Emgu.Util.Platform.OperationSystem == Emgu.Util.Platform.OS.Windows) { //CrossMedia is not implemented on Windows. haveCameraOption = false; havePickImgOption = true; //We will provide our implementation of pick image option } else { haveCameraOption = (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported); havePickImgOption = CrossMedia.Current.IsPickVideoSupported; } String action; List <String> options = new List <string>(); options.Add("Default"); if (havePickImgOption) { options.Add("Photo Library"); } if (haveCameraOption) { options.Add("Photo from Camera"); } #if __IOS__ if (this.HasCameraOption && haveCameraOption) { options.Add("Camera"); } #endif if (Emgu.Util.Platform.OperationSystem == Emgu.Util.Platform.OS.Windows && Emgu.Util.Platform.ClrType != Emgu.Util.Platform.Clr.NetFxCore) { options.Add("Camera"); } if (options.Count == 1) { action = "Default"; } else { action = await DisplayActionSheet(pickImgString, "Cancel", null, options.ToArray()); if (action == null) //user clicked outside of action sheet { return; } } /* * if (haveCameraOption & havePickImgOption) * { * action = await DisplayActionSheet(pickImgString, "Cancel", null, "Default", "Photo Library", * "Photo from Camera"); * if (action == null) //user clicked outside of action sheet * return; * } * else if (havePickImgOption) * { * action = await DisplayActionSheet(pickImgString, "Cancel", null, "Default", "Photo Library"); * if (action == null) //user clicked outside of action sheet * return; * } * else * { * action = "Default"; * }*/ if (action.Equals("Default")) { #if __ANDROID__ mats[i] = Android.App.Application.Context.Assets.GetMat(imageNames[i]); #else if (!File.Exists(imageNames[i])) { throw new FileNotFoundException(String.Format("File {0} do not exist.", imageNames[i])); } mats[i] = CvInvoke.Imread(imageNames[i], ImreadModes.AnyColor); #endif } else if (action.Equals("Photo Library")) { #if __ANDROID__ || __IOS__ || NETFX_CORE var photoResult = await CrossMedia.Current.PickPhotoAsync(); if (photoResult == null) //canceled { return; } mats[i] = CvInvoke.Imread(photoResult.Path); #else if (Emgu.Util.Platform.OperationSystem == OS.Windows) { // our implementation of pick image using (System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog()) { dialog.Multiselect = false; dialog.Title = "Select an Image File"; dialog.Filter = "Image | *.jpg;*.jpeg;*.png;*.bmp;*.gif | All Files | *"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { mats[i] = CvInvoke.Imread(dialog.FileName, ImreadModes.AnyColor); } else { return; } } } else { var file = await CrossMedia.Current.PickPhotoAsync(); using (Stream s = file.GetStream()) using (MemoryStream ms = new MemoryStream()) { s.CopyTo(ms); byte[] data = ms.ToArray(); Mat m = new Mat(); CvInvoke.Imdecode(data, ImreadModes.Color, m); mats[i] = m; } } #endif } else if (action.Equals("Photo from Camera")) { #if __ANDROID__ || __IOS__ || NETFX_CORE var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Emgu", Name = $"{DateTime.UtcNow}.jpg" }; var takePhotoResult = await CrossMedia.Current.TakePhotoAsync(mediaOptions); if (takePhotoResult == null) //cancelled { return; } mats[i] = CvInvoke.Imread(takePhotoResult.Path); #else var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Emgu", Name = $"{DateTime.UtcNow}.jpg" }; var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions); using (Stream s = file.GetStream()) using (MemoryStream ms = new MemoryStream()) { s.CopyTo(ms); byte[] data = ms.ToArray(); Mat m = new Mat(); CvInvoke.Imdecode(data, ImreadModes.Color, m); mats[i] = m; } #endif } else if (action.Equals("Camera")) { mats = new Mat[0]; } } InvokeOnImagesLoaded(mats); #endif }
public async Task ExecuteTakePictureCommand(string isSelfie) { if (IsBusy) { return; } IsBusy = true; TakePictureCommand.ChangeCanExecute(); var error = false; try { if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { var page = new ContentPage(); IsBusy = false; TakePictureCommand.ChangeCanExecute(); await page.DisplayAlert("No Camera", ":( No camera avaialble.", "OK"); return; } var opts = new Plugin.Media.Abstractions.StoreCameraMediaOptions() { Directory = "Sample", Name = "test.jpg" }; if (isSelfie.Equals("true")) { opts.DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front; } var file = await CrossMedia.Current.TakePhotoAsync(opts); if (file == null) { IsBusy = false; TakePictureCommand.ChangeCanExecute(); return; } MyImageSource = ImageSource.FromStream(() => { var stream = file.GetStream(); return(stream); }); } catch { error = true; } if (error) { var page = new ContentPage(); await page.DisplayAlert("Error", "Unable to Take Picture", "OK"); } IsBusy = false; TakePictureCommand.ChangeCanExecute(); }