get Best Preview Size - Android android.hardware

Android examples for android.hardware:Camera Preview

Description

get Best Preview Size

Demo Code

import java.util.List;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;

public class Main {

  public static Size getBestPreviewSize(Camera camera) {
    Parameters params = camera.getParameters();
    Size rsize = params.getPictureSize();
    float raspect = (float) rsize.width / (float) rsize.height;
    float minDiff = 1000000000000F;
    Size psize = params.getPreviewSize();
    List<Size> sizes = params.getSupportedPreviewSizes();
    for (Size size : sizes) {
      float paspect = (float) size.width / (float) size.height;
      float diff = Math.abs(raspect - paspect);
      if (diff < minDiff) {
        psize = size;/*from   w w  w.jav  a 2 s .  c o  m*/
        minDiff = diff;
      }
      if (diff == 0.0F)
        break;
    }
    return psize;
  }

}

Related Tutorials