Last active
May 5, 2019 09:27
-
-
Save ryondev/bffd7c3d69c99316bf51a4f91dd5c2a4 to your computer and use it in GitHub Desktop.
Crop Face With FaceInfo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class FaceCropUtil { | |
| public static Bitmap getCropedFaceFromBitmap(Bitmap bitmap, FaceInfo info) { | |
| if (bitmap != null && info != null) { | |
| int centerX = (int) info.mCenter_x; | |
| int centerY = (int) info.mCenter_y; | |
| int faceWidth = (int) (info.mWidth * 1.5f); | |
| int faceHeight = (int) (info.mWidth * 1.6f); | |
| Rect cropRect = new Rect( | |
| Math.max(0, centerX - faceWidth / 2), | |
| Math.max(0, centerY - (int) (faceHeight / 1.6f)), | |
| Math.min(bitmap.getWidth() - 1, centerX + faceWidth / 2), | |
| Math.min(bitmap.getHeight() - 1, centerY + faceHeight / 2)); | |
| Bitmap transformed = null; | |
| BitmapFactory.Options opts = new BitmapFactory.Options(); | |
| try { | |
| opts.inJustDecodeBounds = false; | |
| Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); | |
| if (!rect.contains(cropRect)) { | |
| return null; | |
| } | |
| try { | |
| transformed = Bitmap.createBitmap(bitmap, cropRect.left, cropRect.top, | |
| cropRect.width(), cropRect.height()); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } catch (OutOfMemoryError e) { | |
| e.printStackTrace(); | |
| bitmap.recycle(); | |
| } | |
| if (transformed != bitmap) { | |
| bitmap.recycle(); | |
| } | |
| return transformed; | |
| } | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment