Laura
Laura

Reputation: 2773

Android camera rotate

I have a Motorola Defy OS Android 2.1 and I make an application with camera Preview. The problem is that the camera works fine on Samsung Galaxy S with Android 2.1, but on Motorola the camera is rotated with 90 degrees. I have tried to do this:

Parameters parameters = camera.getParameters();
parameters.setRotation(90);

but it's not working. I didn't find any solution yet.

Upvotes: 9

Views: 51443

Answers (6)

Crossle Song
Crossle Song

Reputation: 10174

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);
}

Upvotes: 1

Peter
Peter

Reputation: 2500

if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        camera.setDisplayOrientation(90);
        lp.height = previewSurfaceHeight;
        lp.width = (int) (previewSurfaceHeight / aspect);
    } else {
        camera.setDisplayOrientation(0);
        lp.width = previewSurfaceWidth;
        lp.height = (int) (previewSurfaceWidth / aspect);
    }

Upvotes: 16

Timmmm
Timmmm

Reputation: 96547

There is official example code for this in the Android docs now (under setDisplayOrientation()):

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);
}

Upvotes: 8

None
None

Reputation: 19

I think that you can not make any setting to support the API 2.2 to 2.1. The API doesn't have in your current device lib. You must change to 2.2 to support API level 8. By the way, I also try to use the API level 7:

Parameters parameters = camera.getParameters();
parameters.setRotation(90);

This function works well on the Samsung Galaxy Tab but Nexus one. The Samsung Galaxy Tab uses OS 2.2.0 and the Nexus one uses OS 2.2.1. When I try to use API level 8:

camera.setDisplayOrientation(90);

both of them work well. So I think that the API level 7 has problem when we use on Android OS 2.2.1.

Upvotes: 0

Williew
Williew

Reputation: 156

I found this code that works in Android 1.6 and over (works for me using 2.1 and present preview in portrait mode without rotating)

public void surfaceCreated(SurfaceHolder holder){
        try{
            camera = Camera.open();
            setDisplayOrientation(camera, 90);
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        }catch(IOException e){
            Log.d("CAMERA", e.getMessage());
        }

}

protected void setDisplayOrientation(Camera camera, int angle){
        Method downPolymorphic;
        try
        {
            downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
            if (downPolymorphic != null)
                downPolymorphic.invoke(camera, new Object[] { angle });
        }
        catch (Exception e1)
        {
        }
}

The activity has android:screenOrientation="portrait" on AndroidManifest.xml

http://code.google.com/p/android/issues/detail?id=1193#c42

Upvotes: 2

Masson
Masson

Reputation: 107

camera.setDisplayOrientation(int) is not exist under 2.1!

And this code may work, but fail in my milestone/droid :(

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);

you can see more in http://code.google.com/p/android/issues/detail?id=1193#c42

Upvotes: 2

Related Questions