Last active
July 12, 2017 04:51
-
-
Save dohaivu/e14ce8cbdf270f3a446b19b453c1ced6 to your computer and use it in GitHub Desktop.
[Android images handling] image resize, rotate in #android
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
public static int calculateInSampleSize( | |
BitmapFactory.Options options, int reqWidth, int reqHeight) { | |
// Raw height and width of image | |
final int height = options.outHeight; | |
final int width = options.outWidth; | |
int inSampleSize = 1; | |
if (height > reqHeight || width > reqWidth) { | |
final int halfHeight = height / 2; | |
final int halfWidth = width / 2; | |
// Calculate the largest inSampleSize value that is a power of 2 and keeps both | |
// height and width larger than the requested height and width. | |
while ((halfHeight / inSampleSize) >= reqHeight | |
&& (halfWidth / inSampleSize) >= reqWidth) { | |
inSampleSize *= 2; | |
} | |
} | |
return inSampleSize; | |
} | |
public static void resizeBitmapFromFile(String inputPath, String outputPath, int outWidth) { | |
try { | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(inputPath, options); | |
int outHeight = (options.outHeight * outWidth) / options.outWidth; | |
options.inSampleSize = calculateInSampleSize(options, outWidth, outHeight); | |
options.inJustDecodeBounds = false; | |
Bitmap bitmap = BitmapFactory.decodeFile(inputPath, options); | |
Bitmap resized = Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, true); | |
FileOutputStream fOut; | |
File file = new File(outputPath); | |
fOut = new FileOutputStream(file); | |
resized.compress(Bitmap.CompressFormat.JPEG, 85, fOut); | |
fOut.flush(); | |
fOut.close(); | |
bitmap.recycle(); | |
resized.recycle(); | |
} catch (Exception e) { | |
Log.e("cropBitmapFromFile", e.getMessage(), e); | |
} | |
} |
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
public static Bitmap rotateBitmapFromFile(String picturePath) { | |
int orientation = 0; | |
Bitmap bitmap = BitmapFactory.decodeFile(picturePath); | |
try { | |
ExifInterface exif = new ExifInterface(picturePath); | |
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); | |
Log.i(LOG_TAG, "Orientation: " + orientation); | |
} catch (IOException excep) { | |
Log.e(LOG_TAG, excep.getMessage()); | |
} | |
Matrix matrix = new Matrix(); | |
switch (orientation) { | |
case ExifInterface.ORIENTATION_NORMAL: | |
return bitmap; | |
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: | |
matrix.setScale(-1, 1); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_180: | |
matrix.postRotate(180); | |
break; | |
case ExifInterface.ORIENTATION_FLIP_VERTICAL: | |
matrix.postRotate(180); | |
matrix.postScale(-1, 1); | |
break; | |
case ExifInterface.ORIENTATION_TRANSPOSE: | |
matrix.postRotate(90); | |
matrix.postScale(-1, 1); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_90: | |
matrix.postRotate(90); | |
break; | |
case ExifInterface.ORIENTATION_TRANSVERSE: | |
matrix.postRotate(-90); | |
matrix.postScale(-1, 1); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_270: | |
matrix.postRotate(-90); | |
break; | |
default: | |
return bitmap; | |
} | |
try { | |
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); | |
bitmap.recycle(); | |
return bmRotated; | |
} | |
catch (OutOfMemoryError e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment