Crop image bitmap from given bitmap using the given points in the original bitmap and the given rotation. 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.
(Bitmap bitmap, float[] points, int degreesRotated, boolean fixAspectRatio, int aspectRatioX, int aspectRatioY)
| 163 | * image that contains the requires rectangle, rotate and then crop again a sub rectangle. |
| 164 | */ |
| 165 | public static Bitmap cropBitmap(Bitmap bitmap, float[] points, int degreesRotated, boolean fixAspectRatio, |
| 166 | int aspectRatioX, int aspectRatioY) |
| 167 | { |
| 168 | |
| 169 | // get the rectangle in original image that contains the required cropped area (larger for |
| 170 | // non rectangular crop) |
| 171 | Rect rect = getRectFromPoints(points, bitmap.getWidth(), bitmap.getHeight(), fixAspectRatio, aspectRatioX, |
| 172 | aspectRatioY); |
| 173 | |
| 174 | // crop and rotate the cropped image in one operation |
| 175 | Matrix matrix = new Matrix(); |
| 176 | matrix.setRotate(degreesRotated, bitmap.getWidth() / 2, bitmap.getHeight() / 2); |
| 177 | Bitmap result = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height(), matrix, true); |
| 178 | |
| 179 | if(result == bitmap) |
| 180 | { |
| 181 | // corner case when all bitmap is selected, no worth optimizing for it |
| 182 | result = bitmap.copy(bitmap.getConfig(), false); |
| 183 | } |
| 184 | |
| 185 | // rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping |
| 186 | if(degreesRotated % 90 != 0) |
| 187 | { |
| 188 | |
| 189 | // extra crop because non rectengular crop cannot be done directly on the image without |
| 190 | // rotating first |
| 191 | result = cropForRotatedImage(result, points, rect, degreesRotated, fixAspectRatio, aspectRatioX, |
| 192 | aspectRatioY); |
| 193 | } |
| 194 | |
| 195 | return result; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Crop image bitmap from URI by decoding it with specific width and height to down-sample if |
nothing calls this directly
no test coverage detected