Android-纵向模式下的Google Sample HdrViewFinder

时间:2019-08-25 11:57:04

标签: android android-camera2 aspect-ratio landscape-portrait

有人可以帮我解决以下代码吗?我想从Google样本(https://github.com/googlesamples/android-HdrViewfinder)改造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更改为720并将TARGET_ASPECT更改为9.f/16.f。但这也无济于事。

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

这是一个小草图,显示了问题/当我将清单文件中的screenOrientation属性设置为纵向模式时会发生什么: enter image description here

1 个答案:

答案 0 :(得分:1)

我有一个experimental fork通过从SurfaceView切换到TextureView来实现。

已在具有 Android Pie

的Sony G8441 Xperia XZ1 Compact上进行了测试