确保相机预览大小/纵横比与生成的视频相匹配
Posted
技术标签:
【中文标题】确保相机预览大小/纵横比与生成的视频相匹配【英文标题】:Ensure Camera preview size/aspect ratio matches resulting video 【发布时间】:2014-07-16 12:46:16 【问题描述】:我正在使用 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】:它对我来说很好用,而且我没有收到任何批评,不妨也加入 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;
【讨论】:
以上是关于确保相机预览大小/纵横比与生成的视频相匹配的主要内容,如果未能解决你的问题,请参考以下文章