确保相机预览大小/纵横比与生成的视频匹配。

4

我正在使用MediaRecorder和Camera类来预览和捕获视频。我的问题是,我不确定如何确保用户在录制过程中看到的内容与最终录制的视频相匹配。我的第一个想法是通过迭代相机支持的预览大小,直到找到一个最好的大小,也与我设置给MediaRecorder的视频大小的宽高比相匹配:

camProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
aspectRatio = (float)camProfile.videoFrameWidth / camProfile.videoFrameHeight;

...

Camera.Parameters parameters = camera.getParameters();

Size bestSize = getBestSize(parameters.getSupportedPreviewSizes(), aspectRatio);
parameters.setPreviewSize(bestSize.width, bestSize.height);
camera.setParameters(parameters);

LayoutParams params = new LayoutParams((int)(videoView.getHeight() * aspectRatio), videoView.getHeight());
params.addRule(RelativeLayout.CENTER_IN_PARENT);

videoView.setLayoutParams(params);

...

mRecorder.setVideoSize(camProfile.videoFrameWidth, camProfile.videoFrameHeight);

这样做是正确的吗?

1个回答

0

对我来说它运行得很好,而且由于我没有收到任何批评,所以也可以加入getBestSize功能:

private Size getBestSize(List<Size> supportedPreviewSizes, float aspectRatio) {
    int surfaceHeight = videoView.getHeight();

    Size bestSize = null;
    Size backupSize = null;
    for (Size size : supportedPreviewSizes) {
        float previewAspectRatio = size.width / (float)size.height;
        previewAspectRatio = Math.round(previewAspectRatio * 10) / 10f;
        if (previewAspectRatio == aspectRatio) { // Best size must match preferred aspect ratio
            if (bestSize == null || Math.abs(surfaceHeight - size.height) < Math.abs(surfaceHeight - bestSize.height))
                bestSize = size;
        }
        else if (bestSize == null) { // If none of supported sizes match preferred aspect ratio, backupSize will be used
            if (backupSize == null || Math.abs(surfaceHeight - size.height) < Math.abs(surfaceHeight - backupSize.height))
                backupSize = size;
        }
    }
    return bestSize != null ? bestSize : backupSize;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接