3

If I change left-side-base to right-side-base (both are landscape view modes for user) the application (activity) doesn't recreate. Hense, the camera is shown as horizontaly flipped image. The same result when I try to change position of my device for two portrait modes (normal and inverse). The device detects 90 degree rotation normaly. That is, left-side => portrait => right-size => inverse-portrait => left-side works correctly!

Is it possible to fix this?

I am using Android 4.4.4.

Code:

    Display display = ((WindowManager)context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

    if(display.getRotation() == Surface.ROTATION_0){//0

        mCamera.setDisplayOrientation(90);
    }else if(display.getRotation() == Surface.ROTATION_90){//1
        mCamera.setDisplayOrientation(0);
    }else if(display.getRotation() == Surface.ROTATION_180){//2
        mCamera.setDisplayOrientation(270);
    }else if(display.getRotation() == Surface.ROTATION_270){//3
        mCamera.setDisplayOrientation(180);
    }
    Log.d(TAG, "ROTATION:" + display.getRotation() + " ");

EDIT I mean, onResume() and SurfaceCreate() are never call when I rotate the device by 180 degs.

EDIT CODE OF FUNCTIONS

SurfaceView sfv;
FrameLayout fl0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //some code to initialize
    fl0 = (FrameLayout) findViewById(R.id.fl0);
    sfv = new SurfaceView(this);
    fl0.addView(sfv, 0);
    addSurfaceHolder();
}
private void addSurfaceHolder() {
    SurfaceHolder holder = sfv.getHolder();

    holder.addCallback(new SurfaceHolder.Callback() {


        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, TAG + " surfaceCreated");
            //this function never calls for 180 deg rotation.
            cameraStart(MainActivity.this, false);
            //some code for camera holder connection
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
            Log.d(TAG, TAG + " surfaceChanged");
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            ///Some code to destroy camera
            Log.d(TAG, TAG + " surfaceDestroyed");
        }
    });
}
@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, TAG + " onResume"); //call for 90 deg rotation. not for 180 deg rotation.
}

public static void cameraStart(Context context, boolean startStopPreview) {

    if (mCamera == null) {
        mCamera = Camera.open(CAMERA_ID); //0
    }

    Camera.Parameters cameraParam = mCamera.getParameters();

    Camera.Size previewSize = cameraParam.getPreviewSize();
    previewFormat = cameraParam.getPreviewFormat();
    frameWidth = previewSize.width;
    frameHeight = previewSize.height;
    frameRect = new Rect(0, 0, frameWidth, frameHeight);

    Display display = ((WindowManager) context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

    if (display.getRotation() == Surface.ROTATION_0) { //0

        mCamera.setDisplayOrientation(90);
    } else if (display.getRotation() == Surface.ROTATION_90) { //1

        mCamera.setDisplayOrientation(0);
    } else if (display.getRotation() == Surface.ROTATION_180) { //2

        mCamera.setDisplayOrientation(270);
    } else if (display.getRotation() == Surface.ROTATION_270) { //3

        mCamera.setDisplayOrientation(180);
    }
    Log.d(TAG, "ROTATION:" + display.getRotation() + " "); //just calles for 90 deg rotation. not for 180 deg.
}
AndyBoy
  • 564
  • 6
  • 29
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194

1 Answers1

5

You need to get the camera's supporting parameters like this:

Camera.Parameters parameters = camera.getParameters();

and add this line before before mCamera.setDisplayOrientation(int);

parameters.setRotation(int);

for example:

if(display.getRotation() == Surface.ROTATION_0){//0
    parameters.setRotation(90);
    mCamera.setDisplayOrientation(90);
}else if(display.getRotation() == Surface.ROTATION_90){//1
    parameters.setRotation(0);
    mCamera.setDisplayOrientation(0);
}else if(display.getRotation() == Surface.ROTATION_180){//2
    parameters.setRotation(270);
    mCamera.setDisplayOrientation(270);
}else if(display.getRotation() == Surface.ROTATION_270){//3
    parameters.setRotation(180);
    mCamera.setDisplayOrientation(180);
}

hope this answers your question

EDIT

The above code needs to be present also in onResume() method because each time orientation is changed onDestroy() is called and then onCreate() is called so onResume() needs to have this piece of code as well as onCreate().

EDIT 2

Take a look at OrientationEventListener

Further more take a look at this question for hint regarding OrientationEventListener

Hope you will find this answer a little bit useful.

Community
  • 1
  • 1
Sharp Edge
  • 4,144
  • 2
  • 27
  • 41