【问题标题】:Ensure Camera preview size/aspect ratio matches resulting video确保相机预览大小/纵横比与生成的视频相匹配
【发布时间】:2023-03-08 01:21:01
【问题描述】:

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

这是正确的做法吗?

【问题讨论】:

    标签: java android video android-camera mediarecorder


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 2015-02-02
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多