Android - Google Sample HdrViewFinder in Portrait Mode

can somebody help me out with the following code. I want to transform the HdrViewFinder project from google samples ( so that it also works in portrait mode. But the project works only in landscape mode. In portrait mode, the camera preview gets distorted.

So, here is the method I extracted from the class which I think is responsible for setting the whole view into landscape:

     * 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 =
        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);

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

How could I change this method so that it works also in portrait mode? What do I need to change ? Setting only the screenOrientation attribute in the manifest file to portrait did not help. I have found out the 16:9 is used for full landscape mode and that 9:16 used for full portrait mode. So, I changed MAX_WIDTH to 720 and TARGET_ASPECT to 9.f/16.f . But that also did not help.

Can somebody provide a solution ?

Here is a little sketch showing the problem/what happens when I just set the screenOrientation attribute in the manifest file to portrait mode: enter image description here


  • I have an experimental fork that achieves this, by switching to TextureView from SurfaceView.

    tested on Sony G8441 a.k.a Xperia XZ1 Compact with Android Pie