gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:27 24 4
gpt4 key购买 nike

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

这样做正确吗?

最佳答案

嗯,它对我来说工作得很好,而且我没有收到任何批评,还不如加入 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;
}

关于java - 确保相机预览大小/宽高比与生成的视频相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23892229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com