Last active
May 27, 2020 18:22
-
-
Save javikin/1de5fc5196abef9f52cdff4f40e4ed33 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Crop a image taking a recerence a view parent like a frame, and a view child like final | |
* reference | |
* | |
* @param bitmap image to crop | |
* @param frame where the image is set it | |
* @param reference frame to take reference for crop the image | |
* @return image already cropped | |
*/ | |
public static byte[] cropImage(Bitmap bitmap, View frame, View reference){ | |
int heightOriginal = frame.getHeight(); | |
int widthOriginal = frame.getWidth(); | |
int heightFrame = reference.getHeight(); | |
int widthFrame = reference.getWidth(); | |
int leftFrame = reference.getLeft(); | |
int topFrame = reference.getTop(); | |
int heightReal = bitmap.getHeight(); | |
int widthReal = bitmap.getWidth(); | |
int widthFinal = (widthFrame * widthReal)/widthOriginal; | |
int heightFinal = (heightFrame * heightReal)/heightOriginal; | |
int leftFinal = (leftFrame * widthReal)/widthOriginal; | |
int topFinal = (topFrame * heightReal)/heightOriginal; | |
Bitmap bitmapfinal = Bitmap.createBitmap(bitmap, | |
leftFinal, topFinal, widthFinal, heightFinal); | |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
bitmapfinal.compress(Bitmap.CompressFormat.JPEG, 100, stream);//100 is the best quality possibe | |
return stream.toByteArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment