确保相机预览大小/宽高比与生成的视频相匹配

时间:2022-08-27 10:50:06

I'm using the MediaRecorder and Camera classes to preview and capture video. My problem is I'm not really sure how to go about ensuring that what the user sees while recording matches the resulting video. My first inclination is to iterate through the supported preview sizes of the Camera until I find the best one that also matches the aspect ratio of the video size I set to MediaRecorder:

我正在使用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);

Is this the right way to go about it?

这是正确的方法吗?

1 个解决方案

#1


0  

Well it worked fine for me, and as I've received no critique, might as well throw in the getBestSize functionality as well:

好吧,它对我来说很好,而且由于我没有收到批评,也可以投入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;
}

#1


0  

Well it worked fine for me, and as I've received no critique, might as well throw in the getBestSize functionality as well:

好吧,它对我来说很好,而且由于我没有收到批评,也可以投入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;
}