Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 确保相机预览大小/纵横比与生成的视频匹配_Java_Android_Video_Android Camera_Mediarecorder - Fatal编程技术网

Java 确保相机预览大小/纵横比与生成的视频匹配

Java 确保相机预览大小/纵横比与生成的视频匹配,java,android,video,android-camera,mediarecorder,Java,Android,Video,Android Camera,Mediarecorder,我正在使用MediaRecorder和Camera课程预览和捕获视频。我的问题是,我不确定如何确保用户在录制时看到的内容与生成的视频相匹配。我的第一个倾向是反复浏览支持的相机预览尺寸,直到找到与我设置为MediaRecorder的视频尺寸纵横比相匹配的最佳尺寸: camProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P); aspectRatio = (float)camProfile.videoFrameWidth / ca

我正在使用MediaRecorder和Camera课程预览和捕获视频。我的问题是,我不确定如何确保用户在录制时看到的内容与生成的视频相匹配。我的第一个倾向是反复浏览支持的相机预览尺寸,直到找到与我设置为MediaRecorder的视频尺寸纵横比相匹配的最佳尺寸:

camProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
aspectRatio = (float)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;
}
private Size getBestSize(列表支持预览大小、浮点aspectRatio){
int surfaceHeight=videoView.getHeight();
大小bestSize=null;
大小backupSize=null;
用于(大小:supportedPreviewSizes){
浮动比率=大小.宽度/(浮动)大小.高度;
PreviewSpectratio=数学圆整(PreviewSpectratio*10)/10f;
如果(PreviewSpectRatio==aspectRatio){//最佳大小必须与首选纵横比匹配
if(bestSize==null | | Math.abs(surfacehweight-size.height)
mRecorder.setVideoSize(camProfile.videoFrameWidth, camProfile.videoFrameHeight);
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;
}