Someone helped me get this code for taking a picture using xamarin forms labs camera:
picker = DependencyService.Get<IMediaPicker> ();
task = picker.TakePhotoAsync (new CameraMediaStorageOptions {
DefaultCamera = CameraDevice.Rear,
MaxPixelDimension = 800,
});
img.BackgroundColor = Color.Gray;
Device.StartTimer (TimeSpan.FromMilliseconds (250), () => {
if (task != null) {
if (task.Status == TaskStatus.RanToCompletion) {
Device.BeginInvokeOnMainThread (async () => {
//img.Source = ImageSource.FromStream (() => task.Result.Source);
var fileAccess = Resolver.Resolve<IFileAccess> ();
string imageName = "img_user_" + User.CurrentUser().id + "_" + DateTime.Now.ToString ("yy_MM_dd_HH_mm_ss") + ".jpg";
fileName = imageName;
fileAccess.WriteStream (imageName, task.Result.Source);
fileLocation = fileAccess.FullPath(imageName);
FileStream fileStream = new FileStream(fileAccess.FullPath(imageName), FileMode.Open, System.IO.FileAccess.Read);
imageUrl = (string)test[0]["url"];
img.Source = imageUrl;
});
}
return task.Status != TaskStatus.Canceled
&& task.Status != TaskStatus.Faulted
&& task.Status != TaskStatus.RanToCompletion;
}
return true;
});
It saves the image, but the actual size of the phone picture taken is huge, is there a way to resize it.
@Sten's answer might encounter out-of-memory problem on some android devices. Here's my solution to implement the
ResizeImage
function , which is according to google's "Loading Large Bitmaps Efficiently" document:UPDATE: The original answer is not useful, see below for updated answer. The issue was the PCL library was very slow and consumed too much memory.
ORIGINAL ANSWER (do not use):
I found an image I/O library, ImageTools-PCL, which I forked on github and trimmed down what wouldn't compile in Xamarin, keeping the modifications to minimum and the result seems to work.
To use it download the linked repository, compile it with Xamarin and add the DLLs from
Build
folder to your Forms project.To resize an image you can do this (should fit the context of your question)
UPDATED ANSWER:
Get Xamarin.XLabs from nuget, learn about using Resolver, create an IImageService interface with
Resize
method.Implementation for iOS:
Implementation of the service for Android:
An update from the Xamarin Media Plugin allows you to resize the image https://github.com/jamesmontemagno/MediaPlugin ... barring that, and you need a more generic resize option (say the image comes from a web call, and not the device, then have a look at: https://github.com/InquisitorJax/Wibci.Xamarin.Images
You can do this natively for each platform and use an interface. Heres an example for IOS
In your PCL project you need to add an interface
Then to resize an image in your code, you can load the IOS implementation of that interface using the DependencyService and run the ResizeImage method
IOS Implementation, add this class to your IOS project.