Xamarin Texture View camera stream size

2019-09-19 01:55发布

问题:

I created an app that streams data from the camera, in order to let the user take a picture, something like SnapChat. but there is a bug, whenever I take a picture I send it to another activity to show it, everything is fine until this point. But when I go BACK to the firts activity it seems that the size of the texture view has changed.

I took some screenshots to show you guys

This picture shows the stream ( Firts Activity ) and then the result of the picture(Second Activity)

But this Happens when you go Back to the firts activity:

why is this happenning?

this is the code I am using:

Android.Hardware.Camera _camera;
        TextureView _textureView;
        Button TakePictureBtn;
        Bitmap UserPicture;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            _textureView = FindViewById<TextureView>(Resource.Id.textureView);
            _textureView.SurfaceTextureListener = this;
            TakePictureBtn = FindViewById<Button>(Resource.Id.takePhotoButton);
            TakePictureBtn.Click += TakePicture_Click;
        }

        private void TakePicture_Click(object sender, EventArgs e)
        {
            _camera.TakePicture(null, null, this);         
        }

        public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int w, int h)
        {
            _camera = Android.Hardware.Camera.Open();
            Android.Hardware.Camera.Parameters param = _camera.GetParameters();
            IList<Android.Hardware.Camera.Size> supportedSizes = param.SupportedPictureSizes;
            Android.Hardware.Camera.Size sizePicture = supportedSizes[10];
            param.SetPictureSize(sizePicture.Width, sizePicture.Height);
            _camera.SetParameters(param);
            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)
        {

        }

Is important to Say That if i take another picture from this wrong firts activity, the second activity shows a good picture ( as always ) and If I go back to the firts One it is working good again, but If i take another one and I go back it is bad again , and so on.

Also I whould like to Say that Im using an AlcaltelPOP2 to debug my app, but when I use other phone (Asus) the Stream look weird , like if the gravity line is not working!

Finally if you notice the black Gap on the top of the second activity is bigger than in the firts one, this is the axml code.

<?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">
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="-30dp"
        android:id="@+id/imageView1" />
    <Button
        android:id="@+id/BtnSendPicture"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="25dp"
        android:layout_marginBottom="25dp"
        android:background="@drawable/SendButton" />
</FrameLayout>

It easy to say that it is due to the line android:layout_marginBottom="-30dp" but the thing is that if I erase this line of code then a little Gray Line appears in the buttom , why is this happening is the Image view has the line

android:layout_width="fill_parent"
android:layout_height="fill_parent"

Thanks.