diff options
Diffstat (limited to 'src/com/android/camera/Util.java')
-rw-r--r-- | src/com/android/camera/Util.java | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/src/com/android/camera/Util.java b/src/com/android/camera/Util.java index adf9152..2bff219 100644 --- a/src/com/android/camera/Util.java +++ b/src/com/android/camera/Util.java @@ -16,22 +16,24 @@ package com.android.camera; +import com.android.camera.gallery.IImage; + import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; +import android.hardware.Camera.Size; import android.util.Log; +import android.view.Display; import android.view.Surface; import android.view.View; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; -import com.android.camera.gallery.IImage; -import com.android.camera.R; - import java.io.Closeable; +import java.util.List; /** * Collection of utility functions used in this package. @@ -43,7 +45,7 @@ public class Util { public static final int DIRECTION_UP = 2; public static final int DIRECTION_DOWN = 3; - public static final String REVIEW_ACTION = "com.cooliris.media.action.REVIEW"; + public static final String REVIEW_ACTION = "com.android.camera.action.REVIEW"; private Util() { } @@ -301,4 +303,50 @@ public class Util { int result = (info.orientation - degrees + 360) % 360; camera.setDisplayOrientation(result); } + + public static Size getOptimalPreviewSize(Activity currentActivity, + List<Size> sizes, double targetRatio) { + final double ASPECT_TOLERANCE = 0.05; + if (sizes == null) return null; + + Size optimalSize = null; + double minDiff = Double.MAX_VALUE; + + // Because of bugs of overlay and layout, we sometimes will try to + // layout the viewfinder in the portrait orientation and thus get the + // wrong size of mSurfaceView. When we change the preview size, the + // new overlay will be created before the old one closed, which causes + // an exception. For now, just get the screen size + + Display display = currentActivity.getWindowManager().getDefaultDisplay(); + int targetHeight = Math.min(display.getHeight(), display.getWidth()); + + if (targetHeight <= 0) { + // We don't know the size of SurfaceView, use screen height + targetHeight = display.getHeight(); + } + + // Try to find an size match aspect ratio and size + for (Size size : sizes) { + double ratio = (double) size.width / size.height; + if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; + if (Math.abs(size.height - targetHeight) < minDiff) { + optimalSize = size; + minDiff = Math.abs(size.height - targetHeight); + } + } + + // Cannot find the one match the aspect ratio, ignore the requirement + if (optimalSize == null) { + Log.v(TAG, "No preview size match the aspect ratio"); + minDiff = Double.MAX_VALUE; + for (Size size : sizes) { + if (Math.abs(size.height - targetHeight) < minDiff) { + optimalSize = size; + minDiff = Math.abs(size.height - targetHeight); + } + } + } + return optimalSize; + } } |