find Closet Preview Size - Android Camera

Android examples for Camera:Camera Preview

Description

find Closet Preview Size

Demo Code

import java.util.List;

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

public class Main {
  private static final String TAG = "";

  public static Camera.Size findClosetPreviewSize(Camera camera,
      Point preferSize) {/*from   w  ww  . j  a  v a  2s . c o m*/
    int preferX = preferSize.x;
    int preferY = preferSize.y;
    Camera.Parameters parameters = camera.getParameters();
    List<Camera.Size> allSupportSizes = parameters.getSupportedPreviewSizes();

    int minDiff = Integer.MAX_VALUE;
    int index = 0;
    for (int i = 0; i < allSupportSizes.size(); i++) {
      Camera.Size size = allSupportSizes.get(i);
      int x = size.width;
      int y = size.height;

      int diff = Math.abs(x - preferX) + Math.abs(y - preferY);
      if (diff < minDiff) {
        minDiff = diff;
        index = i;
      }
    }

    Camera.Size size = allSupportSizes.get(index);
    return size;
  }

  private static String dumpPreviewSizeList(List<Camera.Size> sizes) {
    String result = "";
    for (Camera.Size size : sizes) {
      result += "(" + size.width + "," + size.height + ") ";
    }
    return result;
  }
}

Related Tutorials