Draw text to camera picture and show in Image control

Hi.
How, in the name of Jesus, can I take a picture, get this picture stream, write some text and show the new picture in an xamarin forms Image control using SkiaSharp?

Posts

  • WashingtonMoraisWashingtonMorais USMember ✭✭

    Done (I'm also using Plugin.Media)!!!

    private async void OnTakePicture(object sender, EventArgs e)
    {
        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            await DisplayAlert(Constants.BASE_TITLE, Constants.ERR002, "OK");
            return;
        }
    
        string dataHora = Constants.GetDateTimeString;
        string nomeArquivo = string.Format("Foto_{0}.jpg", dataHora);
    
        // Bate uma nova foto e salva a mesma no device
        file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
        {
            Directory = Constants.PictureDirectory,
            Name = nomeArquivo,
            AllowCropping = true,
            DefaultCamera = CameraDevice.Front
        });
    
        if (file == null)
            return;
    
        var resizeFactor = 0.5f;
        var bitmap = SKBitmap.Decode(file.GetStream());
        var canvas = new SKCanvas(bitmap);
        var font = SKTypeface.FromFamilyName("Arial");
        var brush = new SKPaint
        {
            Typeface = font,
            TextSize = Convert.ToInt64(Device.GetNamedSize(NamedSize.Default, typeof(Label))),
            IsAntialias = true,
            Color = new SKColor(255, 255, 255, 255)
        };
    
        canvas.DrawText(DateTime.Now.ToString("dd/MM/yyyy HH:mm"), 0, bitmap.Height * resizeFactor / 2.0f, brush);
        var imageSK = SKImage.FromBitmap(bitmap);
        image.Source = (SKImageImageSource)imageSK;
    
        imageSK.Dispose();
        bitmap.Dispose();
        file.Dispose();
    }
    
Sign In or Register to comment.