Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 2c5d97c

Browse files
authored
Use Camera2 on Android. (#672)
1 parent 7bde6d9 commit 2c5d97c

18 files changed

+1075
-421
lines changed

CustomRenderers/View/CustomRenderer/CameraPreview.cs

100755100644
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
namespace CustomRenderer
44
{
5-
public class CameraPreview : View
6-
{
7-
public static readonly BindableProperty CameraProperty = BindableProperty.Create (
8-
propertyName: "Camera",
9-
returnType: typeof(CameraOptions),
10-
declaringType: typeof(CameraPreview),
11-
defaultValue: CameraOptions.Rear);
5+
public class CameraPreview : View
6+
{
7+
public static readonly BindableProperty CameraProperty = BindableProperty.Create(
8+
propertyName: "Camera",
9+
returnType: typeof(CameraOptions),
10+
declaringType: typeof(CameraPreview),
11+
defaultValue: CameraOptions.Rear);
1212

13-
public CameraOptions Camera {
14-
get { return (CameraOptions)GetValue (CameraProperty); }
15-
set { SetValue (CameraProperty, value); }
16-
}
17-
}
13+
public CameraOptions Camera
14+
{
15+
get { return (CameraOptions)GetValue(CameraProperty); }
16+
set { SetValue(CameraProperty, value); }
17+
}
18+
}
1819
}

CustomRenderers/View/CustomRenderer/CustomRenderer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
</EmbeddedResource>
1919
</ItemGroup>
2020

21-
</Project>
21+
</Project>

CustomRenderers/View/CustomRenderer/MainPage.xaml

100755100644
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
44
xmlns:local="clr-namespace:CustomRenderer;assembly=CustomRenderer"
55
x:Class="CustomRenderer.MainPage"
6-
Padding="0,20,0,0"
76
Title="Main Page">
8-
<ContentPage.Content>
9-
<StackLayout>
10-
<Label Text="Camera Preview:" />
11-
<local:CameraPreview Camera="Rear" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
12-
</StackLayout>
13-
</ContentPage.Content>
7+
<StackLayout Margin="20,35,20,20">
8+
<Label Text="Camera Preview:" />
9+
<local:CameraPreview Camera="Rear"
10+
HorizontalOptions="FillAndExpand"
11+
VerticalOptions="FillAndExpand" />
12+
</StackLayout>
1413
</ContentPage>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using Android.Content;
3+
using Android.Runtime;
4+
using Android.Util;
5+
using Android.Views;
6+
7+
namespace CustomRenderer.Droid
8+
{
9+
public class AutoFitTextureView : TextureView
10+
{
11+
int mRatioWidth = 0;
12+
int mRatioHeight = 0;
13+
readonly object locker = new object();
14+
15+
public AutoFitTextureView(Context context) : this(context, null)
16+
{
17+
}
18+
19+
public AutoFitTextureView(Context context, IAttributeSet attrs) : this(context, attrs, 0)
20+
{
21+
}
22+
23+
public AutoFitTextureView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
24+
{
25+
}
26+
27+
protected AutoFitTextureView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
28+
{
29+
}
30+
31+
public void SetAspectRatio(int width, int height)
32+
{
33+
if (width == 0 || height == 0)
34+
{
35+
throw new ArgumentException("Size can't be negative.");
36+
}
37+
38+
mRatioWidth = width;
39+
mRatioHeight = height;
40+
RequestLayout();
41+
}
42+
43+
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
44+
{
45+
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
46+
47+
int width = MeasureSpec.GetSize(widthMeasureSpec);
48+
int height = MeasureSpec.GetSize(heightMeasureSpec);
49+
50+
if (mRatioWidth == 0 || mRatioHeight == 0)
51+
{
52+
SetMeasuredDimension(width, height);
53+
}
54+
else
55+
{
56+
if (width < (float)height * mRatioWidth / mRatioHeight)
57+
{
58+
SetMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
59+
}
60+
else
61+
{
62+
SetMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
63+
}
64+
}
65+
}
66+
67+
public void ClearCanvas(Android.Graphics.Color color)
68+
{
69+
using var canvas = LockCanvas(null);
70+
lock (locker)
71+
{
72+
try
73+
{
74+
canvas.DrawColor(color);
75+
}
76+
finally
77+
{
78+
UnlockCanvasAndPost(canvas);
79+
}
80+
Invalidate();
81+
}
82+
}
83+
}
84+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using Android.Hardware.Camera2;
3+
4+
namespace CustomRenderer.Droid
5+
{
6+
class CameraCaptureStateListener : CameraCaptureSession.StateCallback
7+
{
8+
public Action<CameraCaptureSession> OnConfigureFailedAction;
9+
public Action<CameraCaptureSession> OnConfiguredAction;
10+
11+
public override void OnConfigureFailed(CameraCaptureSession session) => OnConfigureFailedAction?.Invoke(session);
12+
public override void OnConfigured(CameraCaptureSession session) => OnConfiguredAction?.Invoke(session);
13+
}
14+
}

0 commit comments

Comments
 (0)