vendredi 22 mai 2015

Camera surfaceview resizes

I'm making a Camera app that takes a photo in a camera activity and opens up the image in a new picture editor activity. My problem is that when I go back to the camera activity from the picture editor activity, the surfaceView of the camera preview becomes smaller (the aspect ratio is still the same). This only happens after the bitmap is set in the picture editor activity. As long as I don't set the bitmap in the picture editor activity, the surfaceView resolution does not become smaller.

Here is my code in the picture editor activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_picture_editor);

    openCamera = (Button) findViewById(R.id.open_camera_button);
    openCamera
            .setOnClickListener(openCameraButtonClickListener);

    Intent intent = getIntent();
    imgPath = intent.getStringExtra("savedImagePath");
    imgBitmap =(BitmapFactory.decodeFile(imgPath));

    if(imgBitmap!=null) {
        pictureEditorDisplay = (ImageView) findViewById(R.id.pictureEditorDisplay);
        pictureEditorDisplay.setImageBitmap(imgBitmap);
    }   


}

And here is how I set my surfaceView:

public void surfaceCreated(SurfaceHolder holder) {
        chooseHowToSetDisplayOrientation();
        android.view.ViewGroup.LayoutParams params2 = mCameraPreview
                .getLayoutParams();
        //portrait orientation
        params2.width = determineBestPreviewSize(params).height;
        params2.height = determineBestPreviewSize(params).width;
        mCameraPreview.setLayoutParams(params2);
    }

How can I solve this problem? Thanks in advance!

This is my XML code in the Editor layout:

  <ImageView
        android:id="@+id/pictureEditorDisplay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" />

And the code in my Camera layout:

<FrameLayout
    android:id="@+id/camera_frame"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical" >


    <SurfaceView
        android:id="@+id/preview_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" />

</FrameLayout>

I will also try to do some more testing on other devices and see if the problem persists.

Aucun commentaire:

Enregistrer un commentaire