Rotation on image from CameraView

Andrea Farina 21 Reputation points
2021-09-19T08:43:38.667+00:00

How can I rotate the image (is byte[]) captured by CamerView (Xamarin Community Toolkit) since rotation doesn't have a setter?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

7 answers

Sort by: Newest
  1. Sebastian Hoppe 1 Reputation point
    2022-03-27T11:28:24.297+00:00

    Hello,

    I faced exactly the same issue.

    Is there any solution for this out there?

    Thanks.
    Sebastian.

    0 comments No comments

  2. Andrea Farina 21 Reputation points
    2021-09-27T08:18:19+00:00

    135495-immagine-2021-09-27-101703.jpg


  3. Andrea Farina 21 Reputation points
    2021-09-27T08:17:52.073+00:00

    135503-immagine-2021-09-27-101558.jpg

    0 comments No comments

  4. Andrea Farina 21 Reputation points
    2021-09-27T07:41:52.393+00:00

    135390-immagine-2021-09-27-093940.jpg


  5. JarvanZhang 23,951 Reputation points
    2021-09-23T05:44:18.193+00:00

    And using SkiaSharp when I convert SKBitmap back to bytes[] has not undergone any rotation. But I need a byte[].

    @Andrea Farina Try to get the stream for the ImageSource and then use SKBitmap.Decode(stream) command to generate a SKBitmap iobject.

    Here is the related code, you could refer to it:

       private void CameraView_MediaCaptured(object sender, Xamarin.CommunityToolkit.UI.Views.MediaCapturedEventArgs e)  
       {  
           StreamImageSource streamImageSource = (StreamImageSource)e.Image;  
           System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;  
           Task<Stream> task = streamImageSource.Stream(cancellationToken);  
           Stream stream = task.Result;  
         
           SKBitmap photo = RotateBitmap(SKBitmap.Decode(stream));  
           img.Source = ImageSource.FromStream(() => SKImage.FromBitmap(photo).Encode().AsStream());  
       }  
         
       SKBitmap RotateBitmap(SKBitmap bitmap)  
       {  
           SKBitmap rotatedBitmap = new SKBitmap(bitmap.Height, bitmap.Width);  
           using (var surface = new SKCanvas(rotatedBitmap))  
           {  
               surface.Translate(rotatedBitmap.Width, 0);  
               surface.RotateDegrees(90);  
               surface.DrawBitmap(bitmap, 0, 0);  
           }  
           return rotatedBitmap;  
       }