質問

Motorola defy os android 2.1があり、カメラのプレビューでアプリケーションを作成します。問題は、カメラがSamsung Galaxy SでAndroid 2.1で正常に動作することですが、モトローラではカメラが90度で回転しています。私はこれをやろうとしました:

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

しかし、それは機能していません。まだ解決策が見つかりませんでした。

役に立ちましたか?

解決

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

他のヒント

Android Docsに今(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);
}

Camera.SetDisPlayOrientation(INT)は2.1の下に存在しません!

そして、このコードは機能するかもしれませんが、私のマイルストーン/ドロイドで失敗します:(

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

もっと見ることができます http://code.google.com/p/android/issues/detail?id=1193#c42

Android 1.6以上で動作するこのコードを見つけました(2.1を使用して、回転せずにポートレートモードでプレビューをプレビューします)

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

アクティビティにはAndroid:scuentorientation = "Portrait" AndroidManifest.xml

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

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

API 2.2から2.1をサポートする設定を作成できないと思います。 APIは現在のデバイスLIBにありません。 APIレベル8をサポートするには、2.2に変更する必要があります。ちなみに、APIレベル7を使用しようとします。

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

この関数は、Samsung Galaxyタブですが、Nexus Oneでうまく機能します。 Samsung Galaxy TabはOS 2.2.0を使用し、Nexus OneはOS 2.2.1を使用します。 APIレベル8を使用しようとするとき:

camera.setDisplayOrientation(90);

どちらもうまく機能します。したがって、Android OS 2.2.1で使用すると、APIレベル7に問題があると思います。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top