Last active
March 6, 2019 23:23
-
-
Save gtfunes/70abf506f154d1c20ee83e18d72067c4 to your computer and use it in GitHub Desktop.
Java class to compress, resize & correctly rotate (using EXIF data) images from disk
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
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Canvas; | |
import android.graphics.Matrix; | |
import android.graphics.Paint; | |
import android.support.media.ExifInterface; | |
import android.util.Log; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
public class ImageUtils { | |
public ByteArrayOutputStream getCompressedBitmap(String imagePath) { | |
float maxHeight = 1920.0f; | |
float maxWidth = 1080.0f; | |
Bitmap scaledBitmap = null; | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
Bitmap bmp = BitmapFactory.decodeFile(imagePath, options); | |
int actualHeight = options.outHeight; | |
int actualWidth = options.outWidth; | |
float imgRatio = (float) actualWidth / (float) actualHeight; | |
float maxRatio = maxWidth / maxHeight; | |
if (actualHeight > maxHeight || actualWidth > maxWidth) { | |
if (imgRatio < maxRatio) { | |
imgRatio = maxHeight / actualHeight; | |
actualWidth = (int) (imgRatio * actualWidth); | |
actualHeight = (int) maxHeight; | |
} else if (imgRatio > maxRatio) { | |
imgRatio = maxWidth / actualWidth; | |
actualHeight = (int) (imgRatio * actualHeight); | |
actualWidth = (int) maxWidth; | |
} else { | |
actualHeight = (int) maxHeight; | |
actualWidth = (int) maxWidth; | |
} | |
} | |
if (actualWidth > 0 && actualHeight > 0) { | |
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight); | |
options.inJustDecodeBounds = false; | |
options.inDither = false; | |
options.inPurgeable = true; | |
options.inInputShareable = true; | |
options.inTempStorage = new byte[16 * 1024]; | |
try { | |
bmp = BitmapFactory.decodeFile(imagePath, options); | |
} catch (OutOfMemoryError exception) { | |
exception.printStackTrace(); | |
} | |
try { | |
scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888); | |
} catch (OutOfMemoryError exception) { | |
exception.printStackTrace(); | |
} | |
if (scaledBitmap != null) { | |
float ratioX = actualWidth / (float) options.outWidth; | |
float ratioY = actualHeight / (float) options.outHeight; | |
float middleX = actualWidth / 2.0f; | |
float middleY = actualHeight / 2.0f; | |
Matrix scaleMatrix = new Matrix(); | |
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); | |
Canvas canvas = new Canvas(scaledBitmap); | |
canvas.setMatrix(scaleMatrix); | |
canvas.drawBitmap(bmp, middleX - ((float)bmp.getWidth() / 2), middleY - ((float)bmp.getHeight() / 2), new Paint(Paint.FILTER_BITMAP_FLAG)); | |
ExifInterface exif; | |
try { | |
exif = new ExifInterface(imagePath); | |
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); | |
Matrix matrix = new Matrix(); | |
switch (orientation) { | |
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: | |
matrix.setScale(-1, 1); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_180: | |
matrix.setRotate(180); | |
break; | |
case ExifInterface.ORIENTATION_FLIP_VERTICAL: | |
matrix.setRotate(180); | |
matrix.postScale(-1, 1); | |
break; | |
case ExifInterface.ORIENTATION_TRANSPOSE: | |
matrix.setRotate(90); | |
matrix.postScale(-1, 1); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_90: | |
matrix.setRotate(90); | |
break; | |
case ExifInterface.ORIENTATION_TRANSVERSE: | |
matrix.setRotate(-90); | |
matrix.postScale(-1, 1); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_270: | |
matrix.setRotate(270); | |
break; | |
default: | |
break; | |
} | |
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); | |
} catch (IOException e) { | |
Log.e("EX", e.getMessage(), e); | |
} | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); | |
return out; | |
} else { | |
return null; | |
} | |
} else { | |
return null; | |
} | |
} | |
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { | |
final int height = options.outHeight; | |
final int width = options.outWidth; | |
int inSampleSize = 1; | |
if (height > reqHeight || width > reqWidth) { | |
final int heightRatio = Math.round((float)height / (float)reqHeight); | |
final int widthRatio = Math.round((float)width / (float)reqWidth); | |
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; | |
} | |
final float totalPixels = width * height; | |
final float totalReqPixelsCap = reqWidth * reqHeight * 2; | |
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) { | |
inSampleSize++; | |
} | |
return inSampleSize; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment