Zee
Zee

Reputation: 1667

Custom Camera preview distorted

I'm creating a custom camera so that my app can take videos of a specified size (640 x 480), or at least close to a specified size. To do this, I try to get the supported Video size in onCreate like so:

useMe = null;
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> list = parameters.getSupportedVideoSizes();
for (int i = 0; i < list.size(); i++) {
   Camera.Size size = list.get(i);
   Log.i("SIZE: ", size.width + " " + size.height);
   if (size.width == 640) {
      useMe = size;
      break;
   }
   if (size.width < 640) {
      if ((i - 1) > 0) {
         useMe = list.get(i-1);
      } else {
         useMe = size;
      }
   }
 }
 if (useMe == null) {
   useMe = list.get(list.size()/2);
 }

Then, I use the size while setting up the camera:

 private boolean prepareVideoRecorder(){

    mCamera = getCameraInstance();
    mMediaRecorder = new MediaRecorder();

    Camera.Parameters p = mCamera.getParameters();
    p.setPreviewSize(useMe.width, useMe.height);
    mCamera.setParameters(p);

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    mMediaRecorder.setVideoSize(useMe.width, useMe.height);

    mMediaRecorder.setMaxDuration(10000);
    mMediaRecorder.setOnInfoListener(this);

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile().toString());

    // Step 5: Set the preview output

   mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {           
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }
    return true;
}

This records the video correctly, BUT the preview is stretched. I also tried setting

 parameters.setPreviewSize(size.width, size.height);

in the surfaceChanged() method.

Upvotes: 0

Views: 458

Answers (2)

Zee
Zee

Reputation: 1667

OK! I found the answer, but for the life of me - I cannot find the stackoverflow post I copied it from. Basically, the view was stretched over my surface view, so I had to resize the layout to ensure it doesnt stretch.

Here is the code:

private void setMyPreviewSize(int width, int height) {
    // Get the set dimensions
    float newProportion = (float) width / (float) height;

    // Get the width of the screen
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
    float screenProportion = (float) screenWidth / (float) screenHeight;

    // Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = mPreview.getLayoutParams();
    if (newProportion > screenProportion) {
        lp.width = screenWidth;
        lp.height = (int) ((float) screenWidth / newProportion );
    } else {
        lp.width = (int) (newProportion * (float) screenHeight);
        lp.height = screenHeight;
    }
    // Commit the layout parameters
    mPreview.setLayoutParams(lp);
}

where the given width and height is that of the resolution you are using (~640 x 480 in my case).I call this method in my OnCreate method after determining the resolution to use.

Upvotes: 0

Vivek Barai
Vivek Barai

Reputation: 1358

To get rid lf this situation i will simply suggest you to use a library go through this library

Upvotes: 1

Related Questions