vtserej / Camera2Forms

Xamarin Forms Implementation of the Camera2 namespace
17 stars 9 forks source link

Portrait to landscape #7

Closed MCNorthwood closed 5 years ago

MCNorthwood commented 5 years ago

When I have the preview in portrait view, it's fine, however once it is flipped to landscape it seems to do a 180 flip of the preview while everything else flips just the 90 to the side.

I'm also using a Samsung Galaxy S4, Android 5.0, API 21

vtserej commented 5 years ago

It is strange that works. samsung started implementing camera2 namespace after android 6. Anyways in the project links in the readme you will find the rotation code

MCNorthwood commented 5 years ago

I actually found a way if you want to implement it? I don't have time to unfortunately but using the monodroid sample code I got the Rotation by adding

internal static MainActivity Instance { get; private set; } into MainActivity.cs at the top above OnCreate. and then in the OnCreate

base.OnCreate(savedInstanceState);
Instance = this;

Then in the CameraDroid when you want to get the rotation in the OnSurfaceTextureSizeChanged either in there or a private void you can. var rotation = (int)MainActivity.Instance.WindowManager.DefaultDisplay.Rotation;

and then after that it's pretty much the same from Camera2Basic code, with your implementation.

var matrix = new Matrix();
var viewRect = new RectF(0, 0, width, height);
var bufferRect = new RectF(0, 0, _previewSize.Height, _previewSize.Width);
var centerX = viewRect.CenterX();
var centerY = viewRect.CenterY();

if((int)SurfaceOrientation.Rotation90 == rotation || (int)SurfaceOrientation.Rotation270 == rotation)
{
    bufferRect.Offset(centerX - bufferRect.CenterX(), centerY - bufferRect.CenterY());
    matrix.SetRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.Fill);
    var scale = Math.Max((float)height / _previewSize.Height, (float)width / _previewSize.Width);
    matrix.PostScale(scale, scale, centerX, centerY);
    matrix.PostRotate(90 * (rotation - 2), centerX, centerY);
}
else if((int)SurfaceOrientation.Rotation180 == rotation)
{
    matrix.PostRotate(180, centerX, centerY);
}
_cameraTexture.SetTransform(matrix);