簡體   English   中英

Android:縱向模式下的相機預覽方向

[英]Android: Camera preview orientation in portrait mode

我正在使用相機僅顯示預覽(不拍照或錄制視頻)。

應用程序始終處於縱向模式(禁用橫向模式)。 相機預覽始終旋轉90度ccw並且我無法更改它(既不使用setDisplayOrientation也不使用p.set("orientation", "portrait" )p.set("rotation", 90) )

預覽總是像這樣旋轉還是依賴於設備? 如果在縱向模式下總是如此,我可以隨后旋轉圖像。

或者有沒有辦法正確設置相機? 我已經閱讀了很多有關此問題的帖子,但沒有回答對我有用(Galaxy s2,Android v2.3)

要強制縱向方向:

AndroidManifest.xml設置android:screenOrientation="portrait"並調用camera.setDisplayOrientation(90); 在調用camera.startPreview();之前camera.startPreview();

我沒有在GS2上測試,但它適用於我測試過的每部手機。

注意:在API級別8中添加了setDisplayOrientation

我只為肖像模式編寫了應用程序。

camera.setDisplayOrientation(90);

將使相機旋轉到90度這可能導致Android中某些設備的方向不合適為了獲得所有Android設備的正確預覽,請使用以下代碼,這些代碼在開發人員站點中被引用。

下面你必須發送你的活動,cameraId = back是0,前置攝像頭是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;
    //int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        // do something for phones running an SDK before lollipop
        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);
} 

這是如何為相機設置setDisplayOrientation,它將完美適用於所有Android設備。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM