簡體   English   中英

如何以縱向打開相機?

[英]How to open camera with a portrait orientation?

我的整個android相機應用都處於縱向模式,因此當我打開相機時,我需要它自動以縱向模式打開。 當我現在打開相機時,預覽是橫向的。 如何設置相機預覽以縱向模式打開,以使預覽看起來正確?

您可以使用Android Developer文檔中的此方法來旋轉相機預覽。

公共最終空白setDisplayOrientation(以度為單位)

以度為單位設置預覽顯示的順時針旋轉。 這會影響預覽幀和快照后顯示的圖片。 此方法對於縱向模式應用程序很有用。 請注意,前置攝像頭的預覽顯示在旋轉之前會水平翻轉,也就是說,圖像會沿攝像頭傳感器的中心垂直軸反射。 因此,用戶可以將自己視為鏡子。

private 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 = 0;

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

我希望這有幫助。

可能是因為您尚未指定screenRotation嘗試:

 android:screenOrientation="portrait" 

或嘗試以下方法:

在清單中添加方向屬性

android:screenOrientation=["unspecified" | "behind" |
                                     "landscape" | "portrait" |
                                     "reverseLandscape" | "reversePortrait" |
                                     "sensorLandscape" | "sensorPortrait" |
                                     "userLandscape" | "userPortrait" |
                                     "sensor" | "fullSensor" | "nosensor" |
                                     "user" | "fullUser" | "locked"]
So in your case it will be

<activity android:name=".yourCameractivity"
      ....
      android:screenOrientation="portrait"/>

暫無
暫無

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

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