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,294 questions
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. JarvanZhang 23,936 Reputation points
    2021-09-20T01:47:45.367+00:00

    Hello AndreaFarina-0549,​

    Welcome to our Microsoft Q&A platform!

    since rotation doesn't have a setter

    The CamearView class inherits from View class, and View class inherits from VisualElement class. The Rotation property is provided by the VisualElement class, the property has set and get method. You could change the value of the Rotation property to rotate the image captured by the CameraView control directly.

    Here is the related doc about the Rotation property:
    https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.visualelement.rotationproperty?view=xamarin-forms

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Andrea Farina 21 Reputation points
    2021-09-22T14:29:28.647+00:00

    Unfortunately I had already tried these solutions. I would like to avoid native platforms. And using SkiaSharp when I convert SKBitmap back to bytes[] has not undergone any rotation. But I need a byte[].


  3. JarvanZhang 23,936 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;  
       }  
    

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

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


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

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

    0 comments No comments