首页 > 解决方案 > Android - 纵向模式下的 Google 示例 HdrViewFinder

问题描述

有人可以帮我解决以下代码。我想HdrViewFinder从谷歌样本(https://github.com/googlesamples/android-HdrViewfinder)转换项目,以便它也可以在纵向模式下工作。但该项目仅适用于横向模式。在纵向模式下,相机预览会失真。

所以,这是我从 HdrViewfinderActivity.java 类中提取的方法,我认为它负责将整个视图设置为横向:

/**
     * Configure the surfaceview and RS processing.
     */
    private void configureSurfaces() {
        // Find a good size for output - largest 16:9 aspect ratio that's less than 720p
        final int MAX_WIDTH = 1280;
        final float TARGET_ASPECT = 16.f / 9.f;
        final float ASPECT_TOLERANCE = 0.1f;

        StreamConfigurationMap configs =
                mCameraInfo.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        if (configs == null) {
            throw new RuntimeException("Cannot get available picture/preview sizes.");
        }
        Size[] outputSizes = configs.getOutputSizes(SurfaceHolder.class);

        Size outputSize = outputSizes[0];
        float outputAspect = (float) outputSize.getWidth() / outputSize.getHeight();
        for (Size candidateSize : outputSizes) {
            if (candidateSize.getWidth() > MAX_WIDTH) continue;
            float candidateAspect = (float) candidateSize.getWidth() / candidateSize.getHeight();
            boolean goodCandidateAspect =
                    Math.abs(candidateAspect - TARGET_ASPECT) < ASPECT_TOLERANCE;
            boolean goodOutputAspect =
                    Math.abs(outputAspect - TARGET_ASPECT) < ASPECT_TOLERANCE;
            if ((goodCandidateAspect && !goodOutputAspect) ||
                    candidateSize.getWidth() > outputSize.getWidth()) {
                outputSize = candidateSize;
                outputAspect = candidateAspect;
            }
        }
        Log.i(TAG, "Resolution chosen: " + outputSize);

        // Configure processing
        mProcessor = new ViewfinderProcessor(mRS, outputSize);
        setupProcessor();

        // Configure the output view - this will fire surfaceChanged
        mPreviewView.setAspectRatio(outputAspect);
        mPreviewView.getHolder().setFixedSize(outputSize.getWidth(), outputSize.getHeight());
}

我如何更改此方法以使其在纵向模式下也可以使用?我需要改变什么?仅将screenOrientation清单文件中的属性设置为portrait没有帮助。我发现16:9用于全横向模式和9:16用于全纵向模式。所以,我MAX_WIDTH改为720TARGET_ASPECT9.f/16.f但这也无济于事。

有人可以提供解决方案吗?

这是一个小草图,显示了问题/当我screenOrientation将清单文件中的属性设置为纵向模式时会发生什么: 在此处输入图像描述

标签: androidandroid-camera2aspect-ratiolandscape-portrait

解决方案


我有一个实验叉子,通过从 SurfaceView 切换到 TextureView 来实现这一点。

在带有Android Pie的 Sony G8441 aka Xperia XZ1 Compact 上测试


推荐阅读