2

To get the camera reference, I do this:

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "Surface created!");
        mCamera = Camera.open();
        if (mCamera == null) {
            // try to open front facing camera
            try {
                mCamera = Camera.open(0);
            } catch (RuntimeException e) {
                Toast.makeText(getApplicationContext(),
                        "No camera available!", Toast.LENGTH_LONG).show();
            }
        }
        try {
            mCamera.setPreviewDisplay(mCameraHolder);
            setCameraDisplayOrientation(CameraActivity.this, 0, mCamera);
        } catch (Exception e) {
            e.printStackTrace();
            if (mCamera != null)
                mCamera.release();
            mCamera = null;
        }
    }

Where setCameraDisplayOrientation is this (which I found on stackoverflow somewhere):

public static void setCameraDisplayOrientation(Activity activity,
        int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

When I take a picture with my Nexus 4, it has the right orientation. When I try it with my Nexus 7, which only has a front facing camera, it is mirrored (upside down) (was it even without this piece of code actually, so it kind of does nothing I guess.).

Is there any reliable way to get the camera orientation and to make this work? I've searched on Google and Stackoverflow but did not find anything that works so far.

damian
  • 2,001
  • 20
  • 38
  • I'm pretty sure that's normal (and expected behavior) and all "web cams" intended for video-chat do this. If they weren't mirrored, then moving to screen-left would make your image move screen-right, and it would leave the user somewhat disoriented. Does the image get stored without mirror? – 323go Apr 15 '13 at 12:53
  • Ok, now I'm not sure whether vertically is the right one. So when I take a picture of my face, it's upside down.. – damian Apr 15 '13 at 12:58
  • Hmm, ok, that is *not* right. I have the nexi 4, 7, 10, and a few Samsung tabs. If I have time later this afternoon, I'll check it out. – 323go Apr 15 '13 at 14:49
  • did you have the time to check it out? because I did not make any progress with this – damian Apr 22 '13 at 12:35
  • Sorry, Damian, I've been buried in other work. – 323go Apr 22 '13 at 17:21
  • 1
    It's okay, my "temporary" solution is to flip every picture received from front-facing cameras. But thanks anways. If anyone has a "better" solution, ... :) – damian Apr 23 '13 at 08:52
  • See http://stackoverflow.com/questions/5157984/android-camera-surfaceview-orientation/5183690#5183690, specifically the update about landscape based devices. – Alex Cohn Nov 03 '13 at 14:15

1 Answers1

1

This is the solution that I found worked with the nexus 7 and majority other devices I tested like HTC One M8, Lenovo Yoga, Lg G4 Nexus 4, Note 4 and the Oneplus One.

camera.setDisplayOrientation(90);

However, this solution doesn't work with the Nexus 6 which flips the preview, so if anyone has a better solution than this please share.

Princeps Polycap
  • 220
  • 2
  • 16