Skip to content

Instantly share code, notes, and snippets.

@EdgeJH
Created October 21, 2019 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdgeJH/8bb9b97c4fdabc05a421eca4662fc17b to your computer and use it in GitHub Desktop.
Save EdgeJH/8bb9b97c4fdabc05a421eca4662fc17b to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class RotateImageUtil {
private RotateImageUtil(){}
private static RotateImageUtil rotateImageUtil;
public static RotateImageUtil getInstance(){
if (rotateImageUtil==null){
return new RotateImageUtil();
}
return rotateImageUtil;
}
public File getRotatedImageFile(String path,Context context) throws IOException {
return saveBitmapToFileCache(rotateImage(path),context);
}
private File saveBitmapToFileCache(Bitmap bitmap, Context context) throws IOException {
File imageFile = File.createTempFile( "JPEG_" , ".jpg" , context.getCacheDir() );
OutputStream out = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
return imageFile;
}
private Bitmap rotateImage(String path) throws IOException {
Bitmap bitmap = BitmapFactory.decodeFile(path);
ExifInterface exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
break;
default:
break;
}
return bitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment