Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] c# – How to create a button which takes a picture

Posted on November 11, 2022 By

Solution 1 :

You can try MediaPicker to capture picture.And get the filepath in photo.FullPath.
You can refer to the link below to get more details:

https://learn.microsoft.com/en-us/xamarin/essentials/media-picker?tabs=ios

edit:

private void cameraView_MediaCaptured(object sender, Xamarin.CommunityToolkit.UI.Views.MediaCapturedEventArgs e)
 {
     byte[] image = e.ImageData;
     DependencyService.Get<ISaveService>().wirteFile("pic.jpg", image);
 }

 public interface ISaveService
 {
     void SaveFile(string fileName, byte[] data);
 }

Android:

[assembly: Xamarin.Forms.Dependency(typeof(SaveService))]
 namespace cameraviewDemo.Droid
 {
     public class SaveService: ISaveService
     {
         void ISaveService.SaveFile(string fileName, byte[] data)
         {
             string picPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryPictures);
             string filePath = Path.Combine(picPath, fileName);
             File.WriteAllBytes(filePath, data);
         }
     }
 }

iOS:

 [assembly: Xamarin.Forms.Dependency(typeof(SaveService))]
 namespace cameraviewDemo.iOS
 {
     class SaveService: ISaveService
     {
         void ISaveService.SaveFile(string fileName, byte[] data)
         {
             var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
             var filename = Path.Combine(documents, fileName);
             File.WriteAllBytes(filename, data);
         }
     }
 }

Problem :

I’m writing a cross-platform (iOS and Android) app in C# using Xamarin on Visual Studio Community 2022. I have implemented a camera preview on the app using the code found at this website :

https://www.c-sharpcorner.com/article/creating-a-custom-camera-view-basic-concept-create-half-screen-camera-in-xamari/

The code on the website is for iOS, but there is a link at the bottom to download a folder which contains the code for Android as well. My app now displays a camera preview, but there is no way to take a picture with this. I therefore want to create a button which takes a picture without having to open the camera app. I’d also like the resulting photo to not be saved in the photos of the phone. I have looked but have found no way of doing exactly what I want, and therefore I don’t know where to get started. If the answer could give as many details as possible about how to do this for both platforms it would be appreciated. Also, would the resulting photo be of the type FileResult? I have previously worked with the mediapicker in Xamarin which returns the type FileResult, but have come to realize that there are limitations within the mediapicker and I therefore can’t work with it.
Thanks a lot 🙂

READ  [FIXED] c# - AudioTrack couses stutters
Powered by Inline Related Posts

Comments

Comment posted by ToolmakerSteve

google

Comment posted by Ailex 62534

I can’t use the mediapicker. I want to be able to display the device orientation while taking the photo. The mediapicker doesn’t allow for this, from what I gather. I have a preview of the camera already in my app but I’m looking for a way to make a button along with the preview which takes a photo.

Comment posted by learn.microsoft.com/en-us/xamarin/community-toolkit/views/…

maybe you can try cameraview

Comment posted by Ailex 62534

Thanks! I just finished implementing the code and it works! By any chance, do you have any idea on how to get the path to a photo with this? I’m using the Shutter() method to take a picture. Thanks!

Comment posted by Adrain

you can save file with dependency service like edit shows.

Android Tags:c#, camera, xamarin, xamarin.android, xamarin.ios

Post navigation

Previous Post: [FIXED] android – W/Firestore: (24.1.2) [CustomClassMapper]: No setter/field for about found on class
Next Post: [FIXED] android – FusedLocationProviderClient and LocationCallback() object queries

Related Posts

[FIXED] How to restrict Multiline edittext to single line after typing is finished in Android? Android
[FIXED] android – Understanding Command pattern for navigation from ViewModel Android
[FIXED] php – Is there a way of making a flutter webview use android camera for file upload? How to open file picker in webview_flutter flutter? Android
[FIXED] javascript – android studio and sqlite check if username already exists Android
[FIXED] android – Gradle Not Synchronizing : ERROR: Read timed out Android
[FIXED] android – How to get a list of entries for a directory in Google Drive? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme