Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.Rect; public class Main { /** * Crop image bitmap from given bitmap using the given points in the original bitmap and the given rotation.<br> * if the rotation is not 0,90,180 or 270 degrees then we must first crop a larger area of the image that * contains the requires rectangle, rotate and then crop again a sub rectangle. * * @param scale how much to scale the cropped image part, use 0.5 to lower the image by half (OOM handling) */ private static Bitmap cropBitmapObjectWithScale(Bitmap bitmap, float[] points, int degreesRotated, boolean fixAspectRatio, int aspectRatioX, int aspectRatioY, float scale, boolean flipHorizontally, boolean flipVertically) { // get the rectangle in original image that contains the required cropped area (larger for non rectangular crop) Rect rect = getRectFromPoints(points, bitmap.getWidth(), bitmap.getHeight(), fixAspectRatio, aspectRatioX, aspectRatioY); if (degreesRotated == 90 || degreesRotated == 270) { if (flipHorizontally != flipVertically) { boolean temp = flipHorizontally; flipHorizontally = flipVertically; flipVertically = temp; } } // crop and rotate the cropped image in one operation float scaleX = flipHorizontally ? -scale : scale; float scaleY = flipVertically ? -scale : scale; Matrix matrix = new Matrix(); matrix.setScale(scaleX, scaleY); matrix.postRotate(degreesRotated, bitmap.getWidth() / 2, bitmap.getHeight() / 2); Bitmap result = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height(), matrix, true); if (result == bitmap) { // corner case when all bitmap is selected, no worth optimizing for it result = bitmap.copy(bitmap.getConfig(), false); } // rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping if (degreesRotated % 90 != 0) { // extra crop because non rectangular crop cannot be done directly on the image without rotating first result = cropForRotatedImage(result, points, rect, degreesRotated, fixAspectRatio, aspectRatioX, aspectRatioY); } return result; } /** * Get a rectangle for the given 4 points (x0,y0,x1,y1,x2,y2,x3,y3) by finding the min/max 2 points that * contains the given 4 points and is a stright rectangle. */ static Rect getRectFromPoints(float[] points, int imageWidth, int imageHeight, boolean fixAspectRatio, int aspectRatioX, int aspectRatioY) { int left = Math.round(Math.max(0, getRectLeft(points))); int top = Math.round(Math.max(0, getRectTop(points))); int right = Math.round(Math.min(imageWidth, getRectRight(points))); int bottom = Math.round(Math.min(imageHeight, getRectBottom(points))); Rect rect = new Rect(left, top, right, bottom); if (fixAspectRatio) { fixRectForAspectRatio(rect, aspectRatioX, aspectRatioY); } return rect; } /** * Special crop of bitmap rotated by not stright angle, in this case the original crop bitmap contains parts * beyond the required crop area, this method crops the already cropped and rotated bitmap to the final * rectangle.<br> * Note: rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping. */ private static Bitmap cropForRotatedImage(Bitmap bitmap, float[] points, Rect rect, int degreesRotated, boolean fixAspectRatio, int aspectRatioX, int aspectRatioY) { if (degreesRotated % 90 != 0) { int adjLeft = 0, adjTop = 0, width = 0, height = 0; double rads = Math.toRadians(degreesRotated); int compareTo = degreesRotated < 90 || (degreesRotated > 180 && degreesRotated < 270) ? rect.left : rect.right; for (int i = 0; i < points.length; i += 2) { if (points[i] >= compareTo - 1 && points[i] <= compareTo + 1) { adjLeft = (int) Math.abs(Math.sin(rads) * (rect.bottom - points[i + 1])); adjTop = (int) Math.abs(Math.cos(rads) * (points[i + 1] - rect.top)); width = (int) Math.abs((points[i + 1] - rect.top) / Math.sin(rads)); height = (int) Math.abs((rect.bottom - points[i + 1]) / Math.cos(rads)); break; } } rect.set(adjLeft, adjTop, adjLeft + width, adjTop + height); if (fixAspectRatio) { fixRectForAspectRatio(rect, aspectRatioX, aspectRatioY); } Bitmap bitmapTmp = bitmap; bitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height()); if (bitmapTmp != bitmap) { bitmapTmp.recycle(); } } return bitmap; } /** * Get left value of the bounding rectangle of the given points. */ static float getRectLeft(float[] points) { return Math.min(Math.min(Math.min(points[0], points[2]), points[4]), points[6]); } /** * Get top value of the bounding rectangle of the given points. */ static float getRectTop(float[] points) { return Math.min(Math.min(Math.min(points[1], points[3]), points[5]), points[7]); } /** * Get right value of the bounding rectangle of the given points. */ static float getRectRight(float[] points) { return Math.max(Math.max(Math.max(points[0], points[2]), points[4]), points[6]); } /** * Get bottom value of the bounding rectangle of the given points. */ static float getRectBottom(float[] points) { return Math.max(Math.max(Math.max(points[1], points[3]), points[5]), points[7]); } /** * Fix the given rectangle if it doesn't confirm to aspect ration rule.<br> * Make sure that width and height are equal if 1:1 fixed aspect ratio is requested. */ private static void fixRectForAspectRatio(Rect rect, int aspectRatioX, int aspectRatioY) { if (aspectRatioX == aspectRatioY && rect.width() != rect.height()) { if (rect.height() > rect.width()) { rect.bottom -= rect.height() - rect.width(); } else { rect.right -= rect.width() - rect.height(); } } } }