Xamarin Android Display a stream from the camera

2019-02-28 23:46发布

问题:

I'm new in Xamarin and I try to implement my camera stream into a xaml layout. This example of Xamarin will set the complete textureview as layout so I'm not able to add some extra features like buttons etc.

https://developer.xamarin.com/recipes/android/other_ux/textureview/display_a_stream_from_the_camera/

using System;

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Hardware;
using static Android.App.ActionBar;

namespace TextureViewCameraStream
{
    [Activity (Label = "TextureViewCameraStream", MainLauncher = true)]
    public class Activity1 : Activity, TextureView.ISurfaceTextureListener
    {
        Camera _camera;
        TextureView _textureView;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            _textureView = new TextureView(this);
            _textureView.SurfaceTextureListener = this;            

            SetContentView(_textureView);
        }

        public void OnSurfaceTextureAvailable (Android.Graphics.SurfaceTexture surface, int w, int h)
        {
            _camera = Camera.Open ();

            _textureView.LayoutParameters = new FrameLayout.LayoutParams (w, h);

            try {
                _camera.SetPreviewTexture (surface);
                _camera.StartPreview ();

            } catch (Java.IO.IOException ex) {
                Console.WriteLine (ex.Message);
            }
        }

        public bool OnSurfaceTextureDestroyed (Android.Graphics.SurfaceTexture surface)
        {
            _camera.StopPreview ();
            _camera.Release ();

            return true;
        }

        public void OnSurfaceTextureSizeChanged (Android.Graphics.SurfaceTexture surface, int width, int height)
        {
            // camera takes care of this
        }

        public void OnSurfaceTextureUpdated (Android.Graphics.SurfaceTexture surface)
        {

        }

    }
}

For example my layout must be:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1" />
    <TextureView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textureView1"
        android:layout_marginTop="0.0dp" />
</LinearLayout>

Can anyone help me to add the camera preview in the "textureView1" of the layout xaml?

Thanks in advance!

回答1:

You can load your axml via a call to SetContentView, i.e.

SetContentView(Resource.Layout.CameraLayout);

Note: this assumes that your axml within the Layout folder is name CameraLayout.xml (.axml).

Example usage:

public class Activity1 : Activity 
{
    bool _previewing;
    Camera _camera;
    TextureView _textureView;
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView(Resource.Layout.CameraLayout);
        Button button = FindViewById<Button>(Resource.Id.button1);
        _textureView = FindViewById<TextureView>(Resource.Id.textureView1);
        button.Click += delegate {
            try
            {
                if (!_previewing)
                {
                    _camera = Camera.Open();
                    _camera.SetPreviewTexture(_textureView.SurfaceTexture);
                    _camera.StartPreview();
                }
                else
                {
                    _camera.StopPreview();
                    _camera.Release();
                }
            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                _previewing = !_previewing;
            }
        };
    }

Output: