Reputation: 3281
I am working on the face detection application for Android (my device is Nexus S with Android 4.1.2). My SurfaceView size is automatically set to 800x480 but my maximal camera resolution is 720x480. I have tried to change the size of SurfaceView in its onLayout() method which worked but then I was missing 80px in the preview. Is it possible to stretch or at least center the CameraPreview?
Thanks
Upvotes: 2
Views: 2737
Reputation: 309
If anybody else runs into this issue - the way to center the camera preview is with a 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>
More details can be found here.
Upvotes: 1
Reputation: 250
CameraPreview size can be changed using Camera.Parameters. But I recommend that put the surfaceview on the center of screen. Here's the code. I didn't execute the code, but it may works.
// 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));
}
Upvotes: 2