9

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Laura
  • 2,653
  • 7
  • 37
  • 59

6 Answers6

16
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);
    }
poitroae
  • 21,129
  • 10
  • 63
  • 81
Peter
  • 2,480
  • 6
  • 39
  • 60
  • Thank you for your answer but setDisplayOrientation is working only for Android 2.2 or 2.3 and my app is in 2.1. Is there another way to work in 2.1 too? If anyone resolved this problem please help me. – Laura Mar 17 '11 at 12:26
  • 1
    emm. i tested it on 2.1 there work perfectly . i think u mean mediarRecord.setDisplayOrientation- this realy supported in 2.2 or higher – Peter Mar 17 '11 at 14:33
  • Camera.setDisplayOrientation is only supported from API 8 (2.2) See http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int) – User Jul 30 '12 at 10:49
  • 4
    What are the variables lp and aspect? – Derzu Jan 09 '13 at 20:39
  • its was in 2011. im not remember. aspect it's aspectRatio, lp - layoutparams – Peter Nov 25 '14 at 15:18
8

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);
}
Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • 1
    @Fahad Camera id refers to the rear or front facing camera. 0 for rear-facing, 1 for front-facing. Probably best to use the static instance variables `Camera.CameraInfo.CAMERA_FACING_BACK` and `Camera.CameraInfo.CAMERA_FACING_FRONT`. – Steven Aug 12 '13 at 05:14
  • @Timmmm JFYI, `camera.setDisplayOrientation` does nothing on the Samsung Galaxy SII. – Steven Aug 12 '13 at 05:15
  • @StevenWolfe try `camera.getParameters().setRotation(result);` after `camera.setDisplayOrientation(result);` – Pierre Jun 03 '17 at 19:22
2

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

Masson
  • 107
  • 4
2

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

Williew
  • 156
  • 4
1
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);
}
Crossle Song
  • 10,104
  • 2
  • 29
  • 38
  • this actually works very well, if the camerapreview is extending some view, than we also need to reverse width/ height calculations in the onLayout method. – radhoo May 12 '14 at 12:51
0

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.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
None
  • 19
  • 1