android - Nexus 7 摄像头(或所有前置摄像头?)是镜像的(上下颠倒)

标签 android camera android-camera

为了获取相机引用,我这样做:

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "Surface created!");
        mCamera = Camera.open();
        if (mCamera == null) {
            // try to open front facing camera
            try {
                mCamera = Camera.open(0);
            } catch (RuntimeException e) {
                Toast.makeText(getApplicationContext(),
                        "No camera available!", Toast.LENGTH_LONG).show();
            }
        }
        try {
            mCamera.setPreviewDisplay(mCameraHolder);
            setCameraDisplayOrientation(CameraActivity.this, 0, mCamera);
        } catch (Exception e) {
            e.printStackTrace();
            if (mCamera != null)
                mCamera.release();
            mCamera = null;
        }
    }

setCameraDisplayOrientation 在哪里(我在 stackoverflow 的某个地方找到的):

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

当我用 Nexus 4 拍照时,它的方向是正确的。当我用只有前置摄像头的 Nexus 7 尝试它时,它是镜像的(倒置的)(实际上它甚至没有这段代码,所以我猜它什么都不做。)。

是否有任何可靠的方法来获取相机方向并使其正常工作?我在 Google 和 Stackoverflow 上进行了搜索,但到目前为止没有找到任何有效的方法。

最佳答案

这是我发现适用于 nexus 7 和我测试过的大多数其他设备(如 HTC One M8、联想 Yoga、Lg G4 Nexus 4、Note 4 和 Oneplus One)的解决方案。

camera.setDisplayOrientation(90);

但是,此解决方案不适用于翻转预览的 Nexus 6,因此如果有人有比这更好的解决方案,请分享。

关于android - Nexus 7 摄像头(或所有前置摄像头?)是镜像的(上下颠倒),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16015079/

相关文章:

android - 如何改变TextView部分的文字颜色?

android - 拍照并保存到内部存储并获取 getUriForFile 错误

android - setOrientationHint 在某些手机的前置摄像头 (HTC) 上逆时针旋转视频

Android Studio 显示所有红色错误但是所有代码都可以吗?

android - Retrofit2:检索 JSONObject 不起作用

ios - Sample Buffer Delegate Swift 2 用于实时视频过滤器

相机中的android如何在api 14或更高版本中设置可聚焦区域

android - 我想在 android 中读取图像中的 exif 信息。我可以从图库中的图像读取 exif 但我无法读取从相机拍摄的 exif 照片

android - Camera.getSupportedPreviewSizes 是否保证包含显示尺寸

JAVA移动设备-本地存储数据