Nexus 5X保护反向景观传感器固定在机器人摄像头preVIEW应用 [英] Nexus 5x reverse landscape sensor fix in a android camera preview app
问题描述
我在那种Android开发新手,所以我的道歉提前如果我的问题是微不足道的。在我的应用程序的一部分,我需要我的后置摄像头的实况preVIEW,所以我创建延伸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 5倍,这是我刚刚意识到,它已经安装上逆向相机传感器测试我的应用程序。出于这个原因,我的应用程序对我的Nexus 5倍运行时,相机preVIEW出现倒挂,这是我不想。
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需要更新我的code。使用这种新的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.
所以我读了那里,我发现了一张code,我需要在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.
有什么想法?
推荐答案
5倍的相机是不是反转;它在报告其参数正确的摄像机方向,所以不需要特殊的解决办法,只是要确保你正确设置显示方向:
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);
}
从<一个href=\"http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29\" rel=\"nofollow\">http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29
这篇关于Nexus 5X保护反向景观传感器固定在机器人摄像头preVIEW应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!