I am new in Mobile Development. I am trying create a cross platform app using Xamarin Forms. So far I am doing good, but I am having a problem trying to get a way to start a Camera. I understand a Camera is a Native function. Can someone please give me a step by step example on how to do this. Treat me like an idiot. The examples I came across are very vague and some don't even have finished code. if you have a link/s that you believe might help. I'm really struggling with this. Any help appreciated.
2 Answers
You can use the Media Plugin.
Media Plugin for Xamarin and Windows is a Simple cross platform plugin to take photos and video or pick them from a gallery from shared code.
You can add it via Nuget to the solution from here.
takePhoto.Clicked += async (sender, args) =>
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
DisplayAlert("No Camera", ":( No camera available.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg"
});
if (file == null)
return;
await DisplayAlert("File Location", file.Path, "OK");
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
};
You can read more about it's usage here.
You can use the XLabs project for Xamarin Forms:
https://github.com/XLabs/Xamarin-Forms-Labs
This is a short installation guide for registering the services XLabs offers, including the camera: http://www.matrixguide.ch/Datenablage/diverses/How_to_Install_and_Setup_XLabs.pdf
Here is the XLabs camera api: https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera
And here is a short code example: https://bizmonger.wordpress.com/2015/10/19/xamarin-forms-camera-integration/
The advantage of using XLabs is that it allows you to handle other native components as well like the GPS, Accelerometer etc.