Hello,
You need to add a button and call cameraView.Shutter();
in the click event of button:
<Button x:Name="doCameraThings" Clicked="DoCameraThings_Clicked" IsEnabled="False" Text="Snap picture" />
void DoCameraThings_Clicked(object sender, EventArgs e)
{
cameraView.Shutter();
}
and i was hoping to trap that image in
private void CameraView_MediaCaptured
.
Please refer to this official sample code CameraViewPage.xaml.cs to get more details about how to use CameraView_MediaCaptured
method.
Update:
Thanks for your sample.
You could use DependencyService to save the photo in the CameraView_MediaCaptured
method.
Create interface:
public interface ISaveService
{
void SaveFile(string fileName, byte[] data);
}
Implement the interface on Android platform:
[assembly: Xamarin.Forms.Dependency(typeof(SaveService))]
namespace AutoCameraViewDemo.Droid
{
public class SaveService : ISaveService
{
void ISaveService.SaveFile(string fileName, byte[] data)
{
ContentValues values = new ContentValues();
values.Put(MediaStore.IMediaColumns.MimeType, "image/jpeg");
values.Put(MediaStore.IMediaColumns.DateAdded, Java.Lang.JavaSystem.CurrentTimeMillis());
values.Put(MediaStore.IMediaColumns.DisplayName, fileName);
var cr = MainActivity.Instance.ContentResolver; // create a public static MainActivity Instance property and use the Instance=this assignment at the end of the OnCreate method in the MainActivity.cs file
var u = cr.Insert(MediaStore.Images.Media.ExternalContentUri, values);
var s = cr.OpenOutputStream(u);
s.Write(data, 0, data.Length);
s.Close();
values.Clear();
values.Put(MediaStore.IMediaColumns.IsPending, false);
cr.Update(u, values, null, null);
}
}
}
Invoke this method:
private void CameraView_MediaCaptured(object sender, Xamarin.CommunityToolkit.UI.Views.MediaCapturedEventArgs e)
{
byte[] image = e.ImageData;
DependencyService.Get<ISaveService>().SaveFile("test.jpg", image);
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.