So, I'm trying to make a login activity with the camera feed as the background. I've tested the example on https://developer.xamarin.com/guides/android/user_interface/textureview/ and got it to work, but I need to it to be part of an activity. So, I tried this:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Login);
_textureView = this.FindViewById<TextureView>(Resource.Id.textureView);
_textureView.SurfaceTextureListener = this;
}
public void OnSurfaceTextureAvailable(
Android.Graphics.SurfaceTexture surface, int w, int h)
{
_cam = Camera.Open();
_textureView.LayoutParameters =
new FrameLayout.LayoutParams(w, h);
try
{
_cam.SetPreviewTexture(surface);
_cam.StartPreview();
}
catch (Java.IO.IOException ex)
{
Console.WriteLine(ex.Message);
}
}
The axml file looks somewhat like this:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="SizeProportional" />
<LinearLayout
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="SizeProportional">
<!-- Some other controls -->
</LinearLayout>
</AbsoluteLayout>
However, the app now crashes after exiting the OnSurfaceTextureAvailable block, giving an unhandled exception. Breaking at the exception doesn't work, it's apparently in a thread that's not running anymore. Would anyone know why it crashes and, more importantly, how to fix it?