Android 相机预览应用程序中的 Nexus 5x 反向风景传感器修复 [英] Nexus 5x reverse landscape sensor fix in a android camera preview app

查看:23
本文介绍了Android 相机预览应用程序中的 Nexus 5x 反向风景传感器修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 开发的新手,所以如果我的问题微不足道,请提前道歉.在我的应用程序的一部分中,我需要实时预览我的后置摄像头,因此我创建了一个自定义类,它扩展了 SurfaceView 并实现 SurfaceHolder.Callback(我基本上遵循了 android 文档中的说明).

I am kind of newbie in Android development, so my apologies in advance if my question is trivial. In one part of my app I need a live preview of my rear camera, so I created a custom class which extends SurfaceView and implement SurfaceHolder.Callback (I followed basically the instructions in the android documentation).

不幸的是,我正在 Nexus 5x 中测试我的应用程序,我刚刚意识到它以相反的方式安装了相机传感器.出于这个原因,我的应用在 Nexus 5x 上运行时的相机预览显示颠倒,这是我不想要的.

Unfortunately, I am testing my app in a Nexus 5x, which I've just realized that it has installed the camera sensor in a reverse way. For that reason, the camera preview of my app when running on my Nexus 5x appears upside down, which is something I dont want.

新的 android.hardware.camera2 API 似乎能够自动处理这个问题.最终我需要使用这个新 API 更新我的所有代码,但现在我需要的是使用旧相机 API 时的快速修复.

It seems that the new android.hardware.camera2 API is able to handle this issue automatically. Eventually I'll need to update all my code using this new API, but for now what I need is a quick fix while using the old camera API.

所以我在那里阅读并找到了一段代码,我需要在 SurfaceChanged 方法中引入该代码来解决此问题.这是:

So I was reading out there and I found a piece of code that I would need to introduce in the SurfaceChanged method to workaround this issue. Here it is:

Display display = ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        if(display.getRotation() == Surface.ROTATION_0)
        {
            parameters.setPreviewSize(capHeight, capWidth);                           
            camera.setDisplayOrientation(90);
        }

        if(display.getRotation() == Surface.ROTATION_90)
        {
            parameters.setPreviewSize(capWidth, capHeight);                           
        }

        if(display.getRotation() == Surface.ROTATION_180)
        {
            parameters.setPreviewSize(capHeight, capWidth);               
        }

        if(display.getRotation() == Surface.ROTATION_270)
        {
            parameters.setPreviewSize(capWidth, capHeight);
            camera.setDisplayOrientation(180);
        }

        camera.setParameters(parameters);*/

        camera.startPreview();

问题是我没有看到有什么变化.

The problem is that I don't see that something has changed.

有什么想法吗?

推荐答案

5X 相机不是倒车";它确实在其参数中报告了正确的相机方向,因此不需要特殊的解决方法,只需确保您正确设置了显示方向:

The 5X camera is not "reverse"; it does report the correct camera orientation in its parameters, so no special workarounds are needed, just make sure you're setting the display orientation correctly:

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

来自 http://developer.android.com/reference/android/硬件/Camera.html#setDisplayOrientation%28int%29

这篇关于Android 相机预览应用程序中的 Nexus 5x 反向风景传感器修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆