0

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.

Zee
  • 1,592
  • 12
  • 26

2 Answers2

1

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

Vivek Barai
  • 1,338
  • 13
  • 26
  • Thanks! I'll give it a go and let you know what happened. – Zee May 30 '18 at 12:58
  • So, I tried it - its really easy to use and set up - BUT, I still get that the resulting video is slightly distorted :( I'll play around with it a bit more.... – Zee May 30 '18 at 13:37
  • If I use a different device, then this library seems to work! – Zee May 30 '18 at 14:01
  • You should also see center crop functionality in this library. For proper working in all devices. – Vivek Barai May 31 '18 at 09:19
0

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.

Zee
  • 1,592
  • 12
  • 26
  • Here is the link: https://stackoverflow.com/questions/16058926/camera-preview-stretched-even-after-setting-the-correct-dimensions – Zee Jun 01 '18 at 09:38