我正在开发适用于 Android 的人脸检测应用程序(我的设备是 Nexus S 和 Android 4.1.2)。我的 SurfaceView 尺寸自动设置为 800x480,但我的最大相机分辨率为 720x480。我试图在其 onLayout() 方法中更改 SurfaceView 的大小,但后来我在预览中丢失了 80px。是否可以拉伸或至少使 CameraPreview 居中?
谢谢
我正在开发适用于 Android 的人脸检测应用程序(我的设备是 Nexus S 和 Android 4.1.2)。我的 SurfaceView 尺寸自动设置为 800x480,但我的最大相机分辨率为 720x480。我试图在其 onLayout() 方法中更改 SurfaceView 的大小,但后来我在预览中丢失了 80px。是否可以拉伸或至少使 CameraPreview 居中?
谢谢
可以使用 Camera.Parameters 更改 CameraPreview 大小。但我建议将surfaceview放在屏幕中央。这是代码。我没有执行代码,但它可能有效。
// Center the child SurfaceView within the parent.
final int width = r - l;
final int height = b - t;
if (width * previewHeight > height * previewWidth) {
final int surfaceViewWidth = previewWidth * height / previewHeight;
surfaceView.layout((int)((width - surfaceViewWidth)*0.5), 0, (int)((width + surfaceViewWidth)*0.5), height);
} else {
final int surfaceViewHeight = previewHeight * width / previewWidth;
surfaceView.layout(0, (int)((height - surfaceViewHeight)*0.5), width, (int)((height + surfaceViewHeight)*0.5));
}
如果其他人遇到这个问题 - 将相机预览居中的方法是使用 FrameLayout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/surfacecontainer">
<android.view.SurfaceView
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
</FrameLayout>