我使用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);
这是正确的方法吗?
答案 0 :(得分: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;
}