get Best Camera Preview Size - Android Camera

Android examples for Camera:Camera Preview

Description

get Best Camera Preview Size

Demo Code


//package com.java2s;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;

import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.ImageReader;
import android.util.Size;

public class Main {
    public static Size getBestPreviewSize(StreamConfigurationMap map,
            ImageReader imageSize) throws CameraAccessException {
        float imageAspect = (float) imageSize.getHeight()
                / imageSize.getWidth(); // for portrait
        float minDiff = 1000000000000F;
        Size[] previewSizes = map.getOutputSizes(SurfaceTexture.class);
        Size previewSize = previewSizes[0];
        for (Size size : previewSizes) {
            float previewAspect = (float) size.getWidth()
                    / size.getHeight();/*w  w w  . java 2 s .c  o  m*/
            float diff = Math.abs(imageAspect - previewAspect);
            if (diff < minDiff) {
                previewSize = size;
                minDiff = diff;
            }
            if (diff == 0.0F)
                break;
        }
        return previewSize;
    }
}

Related Tutorials