旋转摄像头SurfaceView肖像 [英] Rotating a Camera SurfaceView to portrait
问题描述
我已经看过了几个帖子就改变了相机与面视图的方向,但我已经把我的code从例子中的:
I have looked up a few posts on changing the orientation of the camera with a surface view, but I have taken my code from the examples at:
<一个href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Camera$p$pview.html">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Camera$p$pview.html
这提供了尺寸的功能看起来像这样...
The function that provides the dimensions looks like this...
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
我的问题是,当我改变设备的方向,preVIEW照片在景观。我试着设置了摄像头的方向,但是这导致非常奇怪的结果。有谁知道我需要改变有这个运转正常?
My problem is that when I change the orientation of the device, the preview picture stays landscape. I tried setting the orientation of the camera but this resulted in very strange results. Does anyone know what I need to change to have this rotate properly?
推荐答案
在code正确地调整摄像头preVIEW定位是一个有点复杂,因为它要考虑到
The code to correctly adjust the camera preview orientation is a bit complex, since it has to take into account
- 传感器和相对定位设备的自然的方向(这是纵向的电话,景观为片剂,通常情况下)
- 设备(肖像,风景或每个反向)的当前UI方向
- 不管相机中的问题是,在正面或背面相机(由于前preVIEW流被水平镜像)
的文档<一个href="http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29">Camera.setDisplayOrientation对如何正确处理这个样品code。我复制在这里:
The documentation for Camera.setDisplayOrientation has sample code on how to deal with this correctly. I'm reproducing it here:
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);
}
调用此之后,你的UI已经绘(onSurfaceChanged将工作作为一项指标),或者该设备的用户界面旋转(onConfigurationChanged将工作作为一项指标)。
Call this after your UI has been drawn (onSurfaceChanged would work as an indicator) or the device UI rotates (onConfigurationChanged would work as an indicator).
这篇关于旋转摄像头SurfaceView肖像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!