set Camera Preview Size - Android android.hardware

Android examples for android.hardware:Camera Preview

Description

set Camera Preview Size

Demo Code

import java.util.List;

import android.annotation.TargetApi;
import android.hardware.Camera;
import android.os.Build;
import android.util.Log;

public class Main {

  static final String TAG = "CameraUtil";

  @TargetApi(Build.VERSION_CODES.ECLAIR)
  public static void setPreviewSize(Camera camera) {
    Log.d(TAG, "----->setPreviewSize()");
    if (camera == null) {
      return;//from   w  w w.  j  ava  2 s  .c om
    }
    // set optimal preview size
    Camera.Parameters parameters = camera.getParameters();

    Camera.Size current_size = parameters.getPictureSize();

    List<Camera.Size> preview_sizes = parameters.getSupportedPreviewSizes();
    if (preview_sizes.size() > 0) {
      Camera.Size best_size = preview_sizes.get(0);
      for (Camera.Size size : preview_sizes) {

        if (size.width * size.height > best_size.width * best_size.height) {
          best_size = size;
        }
      }
      parameters.setPreviewSize(best_size.width, best_size.height);

      camera.setParameters(parameters);
    }
  }

}

Related Tutorials