Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/CommunityToolkit.Maui.Camera/CameraManager.android.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Runtime.Versioning;
using Android.Content;
using Android.Content;
using Android.Views;
using AndroidX.Camera.Core;
using AndroidX.Camera.Core.Impl.Utils.Futures;
using AndroidX.Camera.Core.ResolutionSelector;
Expand All @@ -10,6 +10,7 @@
using CommunityToolkit.Maui.Extensions;
using Java.Lang;
using Java.Util.Concurrent;
using System.Runtime.Versioning;
using static Android.Media.Image;
using Math = System.Math;

Expand All @@ -30,6 +31,7 @@ partial class CameraManager
Preview? cameraPreview;
ResolutionSelector? resolutionSelector;
ResolutionFilter? resolutionFilter;
OrientationListener? orientationListener;

public void Dispose()
{
Expand All @@ -47,6 +49,8 @@ public NativePlatformCameraPreviewView CreatePlatformView()
previewView.SetScaleType(NativePlatformCameraPreviewView.ScaleType.FitCenter);
}
cameraExecutor = Executors.NewSingleThreadExecutor() ?? throw new CameraException($"Unable to retrieve {nameof(IExecutorService)}");
orientationListener = new OrientationListener(SetImageCaptureTargetRotation, context);
orientationListener.Enable();

return previewView;
}
Expand Down Expand Up @@ -134,6 +138,10 @@ protected virtual void Dispose(bool disposing)

resolutionFilter?.Dispose();
resolutionFilter = null;

orientationListener?.Disable();
orientationListener?.Dispose();
orientationListener = null;
}
}

Expand Down Expand Up @@ -251,6 +259,20 @@ protected virtual partial ValueTask PlatformTakePicture(CancellationToken token)
return ValueTask.CompletedTask;
}

void SetImageCaptureTargetRotation(int rotation)
{
if (imageCapture is not null)
{
imageCapture.TargetRotation = rotation switch
{
>= 45 and < 135 => (int)SurfaceOrientation.Rotation270,
>= 135 and < 225 => (int)SurfaceOrientation.Rotation180,
>= 225 and < 315 => (int)SurfaceOrientation.Rotation90,
_ => (int)SurfaceOrientation.Rotation0
};
}
}

sealed class FutureCallback(Action<Java.Lang.Object?> action, Action<Throwable?> failure) : Java.Lang.Object, IFutureCallback
{
public void OnSuccess(Java.Lang.Object? value)
Expand Down Expand Up @@ -343,4 +365,19 @@ public void OnChanged(Java.Lang.Object? value)
observerAction.Invoke(value);
}
}

sealed class OrientationListener(Action<int> callback, Context context) : OrientationEventListener(context)
{
readonly Action<int> callback = callback;

public override void OnOrientationChanged(int orientation)
{
if (orientation == OrientationUnknown)
{
return;
}

callback.Invoke(orientation);
}
}
}
Loading