To take a picture and store it with date and time on it, I used a combination of:
- Plugin.Media
- Plugin.Media.Abstractions
- PCLStorage
- SkiaSharp
I found this Using SkiaSharp, how to save a SKBitmap ? and used it as a base for this:
// I get my bitmap from the camera (Plugin.Media allows to pick it from filesystem too)
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
Directory = "MyFolder",
Name = "MyPic001",
PhotoSize = PhotoSize.Medium,
CompressionQuality = 40,
SaveToAlbum = true
});
// If succesfully taken...
if (file != null)
{
// Start using SkiaSharp to write text on it
var bitmap = SKBitmap.Decode(file.Path);
var canvas = new SKCanvas(bitmap);
var font = SKTypeface.FromFamilyName("Arial");
var brush = new SKPaint
{
Typeface = font,
TextSize = Convert.ToInt64(40),
IsAntialias = true,
Color = new SKColor(255, 255, 255, 255)
};
// Write on top of the image
canvas.DrawText(DateTime.Now.ToString("dd/MM/yyyy HH:mm"), 10, 60, brush);
var imageSK = SKImage.FromBitmap(bitmap);
// Get rootFolder and FileName from Plugin.Media's file.Path
string A = file.Path;
string P = A.Substring(0, A.LastIndexOf("/"));
string F = A.Substring(A.LastIndexOf("/") + 1, A.Length - (A.LastIndexOf("/") + 1));
// Start using PCLStorage
IFolder rootFolder = await FileSystem.Current.GetFolderFromPathAsync(P);
IFile myFile = await rootFolder.GetFileAsync(F);
// Use PCLStorage file opening to create an IO.Stream
using (Stream s = await myFile.OpenAsync(FileAccess.ReadAndWrite))
{
// Use SkiaSharp SKImage and SKData to Save the image+text on file.Path
SKData d = imageSK.Encode(SKEncodedImageFormat.Jpeg, 40);
d.SaveTo(s);
}
}