如何在Native Android中制作水平捕获的自定义相机?

问题描述 投票:2回答:1

我想要水平捕捉视频。

如果用户旋转手机,我想水平捕捉视频。

我有一个reference,但我想通过自定义为我自己做。

我怎样才能顺利实现呢?

android android-camera
1个回答
0
投票

试试这个

public static void setCameraDisplayOrientation(int phoneRotation, int cameraId, Camera camera) {
   Camera.CameraInfo info = new Camera.CameraInfo();
    info = Camera.getCameraInfo(cameraId, info);
    int degrees = 0;
    switch (phoneRotation) {
        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; 
    } else { 
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

当phoneRotation是 int phoneRotation = activity.getWindowManager().getDefaultDisplay().getRotation();

顺便说一下,这个答案最初是从stackoverflow中的一些答案重构的。

© www.soinside.com 2019 - 2024. All rights reserved.