Alberto PR
Alberto PR

Reputation: 135

Camera2 Api, Size object, getHeight method returns width and getWidth returns height

This doesn't make any sense to me as I said in the title. The method that should return the width returns 1920, and the method that should return the height returns 1080. Am I doing something wrong?

My camera activity is locked in portrait mode. My phone is a Samsung Note 4. Since my phone is in portrait mode and locked in that orientation, my height should be 1920 and my width 1080, however when I don't lock my camera activity in portrait mode, it still returns the wrong value, but when it is turned to landscape mode, the height is 1080 and the width is 1920.

My code:

mPreviewSize = 
chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedWidth, 
rotatedHeight);

private static Size chooseOptimalSize(Size[] choices, int width, int 
height) {
    List<Size> bigEnough = new ArrayList<Size>();
    for(Size option : choices) {
        Log.d("detectivepikachu","option: h: "+option.getHeight()+" w: 
"+option.getWidth());

        if(option.getHeight() == option.getWidth() * height / width &&
                option.getWidth() >= width && option.getHeight() >= 
height) {
            bigEnough.add(option);
        }
    }
    if(bigEnough.size() > 0) {
        return Collections.min(bigEnough, new CompareSizeByArea());
    } else {
        return choices[0];
    }

}

Upvotes: 0

Views: 328

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57173

You can rotate the texture, but camera hardware is still producing same images even in portrait mode. See Why camera2 supported preview size width always bigger than height? for more details.

If you use MediaRecorder to produce video, or employ ImageReader to capture the preview images on the fly, they will also arrive in the orientation dictated by camera hardware. To fit your device orientation, you must explicitly rotate them.

Upvotes: 1

Related Questions