Created
December 9, 2018 11:45
-
-
Save AlonsoFloo/c039ada77b22f2783c0934ef88b0966f to your computer and use it in GitHub Desktop.
Bitmap utils
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
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Matrix; | |
import android.media.ExifInterface; | |
import android.os.Handler; | |
import android.os.Looper; | |
import android.support.annotation.FloatRange; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.webkit.MimeTypeMap; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.UUID; | |
public abstract class BitmapUtil { | |
public static String DIRECTORY = BitmapUtil.class.getSimpleName(); | |
public interface FileProcessed { | |
void onFileProcesses(@NonNull File file); | |
} | |
public static void rotateImageIfNeeded(@NonNull final Context context, @NonNull final File file, @NonNull FileProcessed bitmapProcessed) { | |
new Thread(() -> { | |
final File newFile = rotateImageIfNeededSync(context, file); | |
Handler mainHandler = new Handler(context.getMainLooper()); | |
Runnable myRunnable = () -> bitmapProcessed.onFileProcesses(newFile); | |
mainHandler.post(myRunnable); | |
}).start(); | |
} | |
@NonNull | |
private static Bitmap rotateImage(@NonNull Bitmap source, @FloatRange(from = -360f, to = 360f) float angle) { | |
Matrix matrix = new Matrix(); | |
matrix.postRotate(angle); | |
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), | |
matrix, true); | |
} | |
@Nullable | |
private static File saveBitmapToJpegFile(@NonNull Bitmap bitmap, @NonNull File destination) { | |
try { | |
if (!destination.exists()) { | |
destination.getParentFile().mkdirs(); | |
destination.createNewFile(); | |
} | |
FileOutputStream out = new FileOutputStream(destination.getAbsolutePath()); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
destination = null; | |
} | |
return destination; | |
} | |
@NonNull | |
static File rotateImageIfNeededSync(@NonNull final Context context, @NonNull final File file) { | |
if (!file.exists()) { | |
return file; | |
} | |
int orientation = ExifInterface.ORIENTATION_UNDEFINED; | |
try { | |
ExifInterface ei = new ExifInterface(file.getAbsolutePath()); | |
orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, | |
ExifInterface.ORIENTATION_UNDEFINED); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
File finalFile = file; | |
if (orientation != ExifInterface.ORIENTATION_UNDEFINED) { | |
Bitmap rotatedBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); | |
switch (orientation) { | |
case ExifInterface.ORIENTATION_ROTATE_90: | |
rotatedBitmap = rotateImage(rotatedBitmap, 90); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_180: | |
rotatedBitmap = rotateImage(rotatedBitmap, 180); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_270: | |
rotatedBitmap = rotateImage(rotatedBitmap, 270); | |
break; | |
case ExifInterface.ORIENTATION_NORMAL: | |
default: | |
rotatedBitmap = null; | |
break; | |
} | |
if (rotatedBitmap != null) { | |
String newFileName = UUID.randomUUID().toString() + ".jpg"; | |
File toSaveFile = FileUtil.getDataFile(context, DIRECTORY, newFileName); | |
finalFile = saveBitmapToJpegFile(rotatedBitmap, toSaveFile); | |
if (finalFile == null) { | |
finalFile = file; | |
} | |
} | |
} | |
return finalFile; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment