Java tutorial
//package com.java2s; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.Point; import android.graphics.Rect; import android.hardware.Camera; import android.view.Display; import android.view.WindowManager; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { private static int MIN_PREVIEW_PIXELS = 470 * 320; private static int MAX_PREVIEW_PIXELS = 800 * 600; public static Bitmap getFocusedBitmap(Context context, Camera camera, Bitmap bmp, Rect box) { Point CamRes = getCameraResolution(context, camera); Point ScrRes = getScreenResolution(context); int SW = ScrRes.x; int SH = ScrRes.y; int RW = box.width(); int RH = box.height(); int RL = box.left; int RT = box.top; float RSW = (float) (RW * Math.pow(SW, -1)); float RSH = (float) (RH * Math.pow(SH, -1)); float RSL = (float) (RL * Math.pow(SW, -1)); float RST = (float) (RT * Math.pow(SH, -1)); int CW = CamRes.x; int CH = CamRes.y; if (CW > CH) bmp = rotateBitmap(bmp, 90); int BW = bmp.getWidth(); int BH = bmp.getHeight(); int RBL = (int) (RSL * BW); int RBT = (int) (RST * BH); int RBW = (int) (RSW * BW); int RBH = (int) (RSH * BH); Bitmap res = Bitmap.createBitmap(bmp, RBL, RBT, RBW, RBH); bmp.recycle(); return res; } public static Point getCameraResolution(Context context, Camera camera) { return findBestPreviewSizeValue(camera.getParameters(), getScreenResolution(context)); } public static Point getScreenResolution(Context context) { WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); return new Point(width, height); } public static Bitmap rotateBitmap(Bitmap source, float angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); } public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) { List<Camera.Size> supportedPreviewSizes = new ArrayList<Camera.Size>(parameters.getSupportedPreviewSizes()); Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() { @Override public int compare(Camera.Size a, Camera.Size b) { int aPixels = a.height * a.width; int bPixels = b.height * b.width; if (bPixels < aPixels) { return -1; } if (bPixels > aPixels) { return 1; } return 0; } }); Point bestSize = null; float screenAspectRatio = (float) screenResolution.x / (float) screenResolution.y; float diff = Float.POSITIVE_INFINITY; for (Camera.Size supportedPreviewSize : supportedPreviewSizes) { int realWidth = supportedPreviewSize.width; int realHeight = supportedPreviewSize.height; int pixels = realWidth * realHeight; if (pixels < MIN_PREVIEW_PIXELS || pixels > MAX_PREVIEW_PIXELS) { continue; } boolean isCandidatePortrait = realWidth < realHeight; int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth; int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight; if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) { return new Point(realWidth, realHeight); } float aspectRatio = (float) maybeFlippedWidth / (float) maybeFlippedHeight; float newDiff = Math.abs(aspectRatio - screenAspectRatio); if (newDiff < diff) { bestSize = new Point(realWidth, realHeight); diff = newDiff; } } if (bestSize == null) { Camera.Size defaultSize = parameters.getPreviewSize(); bestSize = new Point(defaultSize.width, defaultSize.height); } return bestSize; } }