set Camera Preview Size by width and height - Android android.hardware

Android examples for android.hardware:Camera Preview

Description

set Camera Preview Size by width and height

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 void setPreviewSize(Camera cam, int width, int height) {
    Parameters parameters = cam.getParameters();
    List<Size> sizes = parameters.getSupportedPreviewSizes();

    float ratio = (float) width / height;
    boolean isCompared = false;
    for (Size size : sizes) {

      if (ratio == (float) size.width / size.height) {
        isCompared = true;/*from www.  j  a v  a 2 s  .c o m*/
        break;
      }
    }

    if (isCompared) {
      parameters.setPreviewSize(width, height);
      cam.setParameters(parameters);
    }
  }

}

Related Tutorials