【问题标题】:Nexus 7 Camera (or all front cameras?) are mirrored (upside-down)Nexus 7 摄像头(或所有前置摄像头?)是镜像的(倒置)
【发布时间】:2013-04-15 12:24:02
【问题描述】:

为了获得相机参考,我这样做:

    @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 上进行了搜索,但到目前为止没有找到任何可行的方法。

【问题讨论】:

  • 我很确定这是正常的(和预期的行为),所有用于视频聊天的“网络摄像头”都会这样做。如果它们没有被镜像,那么移动到屏幕左侧会使您的图像移动到屏幕右侧,这会使用户有些迷失方向。图像是否在没有镜像的情况下存储?
  • 好的,现在我不确定垂直方向是否正确。所以当我给我的脸拍照时,它是颠倒的......
  • 嗯,好吧,那是对的。我有 nexi 4、7、10 和一些三星标签。如果我下午晚些时候有时间,我会去看看。
  • 你有时间去看看吗?因为我没有在这方面取得任何进展
  • 没关系,我的“临时”解决方案是翻转从前置摄像头接收到的每张照片。不过还是谢谢。如果有人有“更好”的解决方案,... :)

标签: android camera android-camera


【解决方案1】:

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

camera.setDisplayOrientation(90);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多