Sai
Sai

Reputation: 386

Stretching camera preview to full screen in android

I am using this camera preview application for my Android app.

I want the camera preview over the full screen.Hence I used the example from Android APIs to try setting the preview to full screen. This is how I am trying to do it:

if (!cameraConfigured) {
    Camera.Parameters parameters=camera.getParameters();
    Camera.Size size = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
    if (size != null) {
        parameters.setPreviewSize(size.width, size.height);

      camera.setParameters(parameters);
      cameraConfigured=true;
    }

I am using relative layout as my layout. My layout settings are follows:

<android.view.SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

I am still not able to preview my camera over the entire screen. I would like to know how to preview over the entire screen.

Upvotes: 2

Views: 15746

Answers (4)

Kamran Bashir
Kamran Bashir

Reputation: 795

Get dimensions of the screen

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

use this width and height to get preview size.

Upvotes: 0

Sai
Sai

Reputation: 386

I found the problem. I added the following setting to Android Manifest file

<supports-screens android:largeScreens="true"
                    android:normalScreens="true"
                    android:smallScreens="true" android:xlargeScreens="true"/>

I am able to view the camera over full screen.

Upvotes: 3

Daniel Smith
Daniel Smith

Reputation: 8579

Im assuming your code is within a class that extends SurfaceView and that you will place the surfaceView inside a FrameLayout that is as large as the display.

What you are not doing in your code is setting your SurfaceView to be the same size as the display which in the following code block is done by getting the layout, and setting the width and height.

I use the following code to stretch to the dimensions of the screen:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

    ...

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

      ...

      final DisplayMetrics dm = this.getResources().getDisplayMetrics();
      Camera.Parameters parameters = mCamera.getParameters();
      parameters.setPreviewSize(cameraSize.width, cameraSize.height);
      mCamera.setParameters(parameters);
      //Here you get the SurfaceView layout and subsequently set its width and height
      FrameLayout.LayoutParams frameParams = (FrameLayout.LayoutParams) this.getLayoutParams();
      frameParams.width = LayoutParams.MATCH_PARENT;// dm.widthPixels should also work
      frameParams.height = LayoutParams.MATCH_PARENT;//dm.heightPixels should also work
      this.setLayoutParams(frameParams);
      //And we should have things set now...

      ....

    }

    ...
}

I hope this helps. The key parts to integrate are between those inner comments

Upvotes: 0

GTbrewer
GTbrewer

Reputation: 165

First, get the display width and height:

   int dw = currentDisplay.getWidth();    // dw == display width
   int dh = currentDisplay.getHeight();   // dh == display height

Then loop through the supported preview sizes, and pick the largest one where, if (size.width <= dw && size.height <= dh). When you hit one where this if statement fails, use the previous values (you could set something like prevWidth and prevHeight as a quick/easy way to step back one ... just remember to set each of those AFTER you test them; after you hit a preview size that's larger than screen size, just break out of the loop.

Later,
--jim

Upvotes: -1

Related Questions