1

I am working in my app, it should open a camera stream and take a photo when the user press a button, this is my axml file.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextureView
        android:id="@+id/textureView"
        android:layout_marginTop="-95dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/takePhotoButton"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_marginBottom="15dp"
        android:layout_gravity="center|bottom"
        android:background="@drawable/TakePhotoButton" />
</FrameLayout>

And this is my code:

using System;

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Hardware;
using Android.Graphics;

namespace CameraStream
{
    [Activity(Label = "CameraStream", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity, TextureView.ISurfaceTextureListener
    {
        Android.Hardware.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 = Android.Hardware.Camera.Open();

            var previewSize = _camera.GetParameters().PreviewSize;
            _textureView.LayoutParameters =
                new FrameLayout.LayoutParams(h, w, GravityFlags.Center);
            try
            {
                _camera.SetPreviewTexture(surface);
                _camera.StartPreview();
            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            _textureView.Rotation = 90.0f;

        }

        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)
        {

        }
    }
}

as you can notice, it wont show the main axml, how can I merge the code that opens the camera and starts the stream with my buttons? I tryed this:

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

            _textureView = FindViewById<TextureView>(Resource.Id.textureView);
            _textureView.SurfaceTextureListener = this;

            SetContentView(Resource.Layout.Main);
        }

but an exception message shows up, I found a code similar to the one I need on the answer of this question: Xamarin Android Display a stream from the camera

but there the button starts and finishes the stream, and I want the stream always working, how can I do that ? Thanks.

Community
  • 1
  • 1

1 Answers1

0

You have to load & inflate (SetContentView) the layout before accessing (FindViewById) any elements in it.

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

    SetContentView(Resource.Layout.Main);
    _textureView = FindViewById<TextureView>(Resource.Id.textureView);
    _textureView.SurfaceTextureListener = this;
}

Assuming you have the Camera permissions set in your manifest, you should now have a live back camera stream in your TextureView.

SushiHangover
  • 73,120
  • 10
  • 106
  • 165