自定义相机预览失真

时间:2018-05-30 10:53:15

标签: android camera

我正在创建自定义相机,以便我的应用可以拍摄指定尺寸(640 x 480)或至少接近指定尺寸的视频。为此,我尝试在onCreate中获得支持的视频大小,如下所示:

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);
 }

然后,我在设置相机时使用尺寸:

 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;
}

这会正确记录视频,但预览会被拉伸。我也尝试过设置

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

在surfaceChanged()方法中。

2 个答案:

答案 0 :(得分:1)

要摆脱这种情况我只会建议你使用一个库来完成这个library

答案 1 :(得分:0)

OK!我找到了答案,但对于我的生活 - 我找不到我复制它的stackoverflow帖子。基本上,视图在我的表面视图上拉伸,所以我不得不调整布局的大小以确保它不会拉伸。

以下是代码:

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);
}

其中给定的宽度和高度是您使用的分辨率(在我的情况下约为640 x 480)。我在确定要使用的分辨率后,在我的OnCreate方法中调用此方法。