get Best Camera Size - Android android.hardware

Android examples for android.hardware:Camera Size

Description

get Best Camera Size

Demo Code

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.graphics.Point;
import android.hardware.Camera;

public class Main {

  private static Comparator<Camera.Size> CAMERA_SIZE_COMPARATOR = new Comparator<Camera.Size>() {
    @Override/*from   w  ww  .  ja v  a  2s  .  co  m*/
    public int compare(Camera.Size lhs, Camera.Size rhs) {
      return lhs.width * lhs.height - rhs.width * rhs.height;
    }
  };

  public static Point getBestSize(final List<Camera.Size> list, final int width, final int height, int rotation) {
    if (list == null || list.isEmpty())
      return null;
    final boolean swap = rotation % 180 != 0;
    final int requiredWidth = swap ? height : width, requiredHeight = swap ? width : height;
    final ArrayList<Camera.Size> sorted = new ArrayList<>(list);
    Collections.sort(sorted, CAMERA_SIZE_COMPARATOR);
    for (final Camera.Size size : sorted) {
      if (size.width >= requiredWidth && size.height >= requiredHeight)
        return new Point(size.width, size.height);
    }
    return null;
  }

}

Related Tutorials