0

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)

Picture 1

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

Picture 2

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.

Nicole Ramírez
  • 345
  • 3
  • 19
  • `sizePicture = supportedSizes[10]` is very dangerous; you don't know if this list is long enough, or whether this size is good enough. We usually try to find sizes that fit the aspect ratio of the preview. You don't choose preview size at all, and probably because of that the preview is not consistent. – Alex Cohn Mar 26 '17 at 18:17
  • Do you thing that should Implement something like this? http://stackoverflow.com/questions/17804309/android-camera-preview-wrong-aspect-ratio It is okay to set the parameters on public void OnSurfaceTextureAvailable ?? Thanks – Nicole Ramírez Mar 26 '17 at 19:47
  • Hi , I just implemented that code, but it did not work. – Nicole Ramírez Mar 26 '17 at 20:01
  • Yes, that discussion is relevant, and some kind of `getOptimalPreviewSize()` is what you need. But I cannot debug your code for you, especially when I don't see it. – Alex Cohn Mar 27 '17 at 08:26
  • Hi, If you want you can Download the whole project from here: https://drive.google.com/file/d/0BzcqKy6YUUeCT25aTWpONjl1MGM/view – Nicole Ramírez Mar 27 '17 at 13:28
  • I solved it by changing OnSurfaceTextureAvailable method to this: – Nicole Ramírez Apr 01 '17 at 16:01
  • 1
    Globals g = Globals.getInstance; bool flash = g.GetFlash(); _camera = Android.Hardware.Camera.Open(); _camera.SetDisplayOrientation(90); an deleted the lines var previewSize = _camera.GetParameters().PreviewSize; _textureView.LayoutParameters = new FrameLayout.LayoutParams(h, w, GravityFlags.Center) – Nicole Ramírez Apr 01 '17 at 16:03

0 Answers0