Display a stream from the camera

davidvbdavidvb BEMember

Hello,

I'm new with Xamarin and I'm trying to include this example into a xaml layout so I can add other things like buttons etc.
I tried making a xaml layout with a textureview but no succes so far.

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

Can someone with some more experience tell/show me how to do this?

Thanks a lot!

Posts

  • Misterio_ReeMisterio_Ree USMember

    The XML Side

     >!
    
                <LinearLayout xmlns:android=" This Site is not allowing me to post this line"
                    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>
    

    The C# Side

        >!             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;
                                }
                            };
                        } 
    

    ## And you can call the resource file

    SetContentView(Resource.Layout.CameraLayout);

Sign In or Register to comment.