保持视频录制预览的宽高比

时间:2016-05-27 11:58:04

标签: android mediarecorder

我在我的android项目上录制视频。 我用它来配置MediaRecorder

private boolean prepareVideoRecorder(){

    // BEGIN_INCLUDE (configure_preview)
    mCamera = CameraHelper.getDefaultCameraInstance();
    // We need to make sure that our preview and recording video size are supported by the
    // camera. Query camera to find all the sizes and choose the optimal size given the
    // dimensions of our preview surface.
    Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
    Camera.Size optimalSize = CameraHelper.getOptimalPreviewSize(mSupportedPreviewSizes,
            mSurfaceView.getWidth(), mSurfaceView.getHeight());

    // Use the same size for recording profile.
    CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    profile.videoFrameWidth = optimalSize.width;
    profile.videoFrameHeight = optimalSize.height;

    // likewise for the camera object itself.
    parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
    mCamera.setParameters(parameters);
    try {
        // Requires API level 11+, For backward compatibility use {@link setPreviewDisplay}
        // with {@link SurfaceView}
        mCamera.setPreviewDisplay(mSurfaceView.getHolder());
        mCamera.setDisplayOrientation(90);

    } catch (IOException e) {
        Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
        return false;
    }
    // END_INCLUDE (configure_preview)


    // BEGIN_INCLUDE (configure_media_recorder)
    mMediaRecorder = new MediaRecorder();

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

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

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

    // Step 4: Set output file
    String path  = getVideoFile(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO).getAbsolutePath();
    currentFile = path;
    mMediaRecorder.setOutputFile(path);
    mMediaRecorder.setOrientationHint(270);

    // END_INCLUDE (configure_media_recorder)

    // Step 5: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}

在我的xaml中我有这个SurfaceView

... <RelativeLayout
    android:id="@+id/videoSurfaceParent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/carouselBar"
    android:background="@android:color/black"
    android:visibility="invisible">
    <SurfaceView
        android:id="@+id/videoSurface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:visibility="invisible">
    </SurfaceView>
</RelativeLayout>...

我的问题是,当我录制视频时,预览会被拉伸,人们看起来很胖。

有没有办法保持宽高比?
我正在使用前置摄像头。

0 个答案:

没有答案