/
ImageUtils.java
147 lines (126 loc) · 5.22 KB
/
ImageUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.capacitorjs.plugins.camera;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import androidx.exifinterface.media.ExifInterface;
import com.getcapacitor.Logger;
import java.io.IOException;
import java.io.InputStream;
public class ImageUtils {
/**
* Resize an image to the given width and height considering the preserveAspectRatio flag.
* @param bitmap
* @param width
* @param height
* @return a new, scaled Bitmap
*/
public static Bitmap resize(Bitmap bitmap, final int width, final int height) {
return ImageUtils.resizePreservingAspectRatio(bitmap, width, height);
}
/**
* Resize an image to the given max width and max height. Constraint can be put
* on one dimension, or both. Resize will always preserve aspect ratio.
* @param bitmap
* @param desiredMaxWidth
* @param desiredMaxHeight
* @return a new, scaled Bitmap
*/
private static Bitmap resizePreservingAspectRatio(Bitmap bitmap, final int desiredMaxWidth, final int desiredMaxHeight) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 0 is treated as 'no restriction'
int maxHeight = desiredMaxHeight == 0 ? height : desiredMaxHeight;
int maxWidth = desiredMaxWidth == 0 ? width : desiredMaxWidth;
// resize with preserved aspect ratio
float newWidth = Math.min(width, maxWidth);
float newHeight = (height * newWidth) / width;
if (newHeight > maxHeight) {
newWidth = (width * maxHeight) / height;
newHeight = maxHeight;
}
return Bitmap.createScaledBitmap(bitmap, Math.round(newWidth), Math.round(newHeight), false);
}
/**
* Transform an image with the given matrix
* @param bitmap
* @param matrix
* @return
*/
private static Bitmap transform(final Bitmap bitmap, final Matrix matrix) {
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
/**
* Correct the orientation of an image by reading its exif information and rotating
* the appropriate amount for portrait mode
* @param bitmap
* @param imageUri
* @return
*/
public static Bitmap correctOrientation(final Context c, final Bitmap bitmap, final Uri imageUri) throws IOException {
if (Build.VERSION.SDK_INT < 24) {
return correctOrientationOlder(c, bitmap, imageUri);
} else {
final int orientation = getOrientation(c, imageUri);
if (orientation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
ExifInterface exif = new ExifInterface(imageUri.getPath());
exif.resetOrientation();
exif.saveAttributes();
return transform(bitmap, matrix);
} else {
return bitmap;
}
}
}
private static Bitmap correctOrientationOlder(final Context c, final Bitmap bitmap, final Uri imageUri) {
// TODO: To be tested on older phone using Android API < 24
String[] orientationColumn = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION };
Cursor cur = c.getContentResolver().query(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();
if (orientation != -1) {
matrix.postRotate(orientation);
}
return transform(bitmap, matrix);
}
private static int getOrientation(final Context c, final Uri imageUri) throws IOException {
int result = 0;
try (InputStream iStream = c.getContentResolver().openInputStream(imageUri)) {
final ExifInterface exifInterface = new ExifInterface(iStream);
final int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
result = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
result = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
result = 270;
}
}
return result;
}
public static ExifWrapper getExifData(final Context c, final Bitmap bitmap, final Uri imageUri) {
InputStream stream = null;
try {
stream = c.getContentResolver().openInputStream(imageUri);
final ExifInterface exifInterface = new ExifInterface(stream);
return new ExifWrapper(exifInterface);
} catch (IOException ex) {
Logger.error("Error loading exif data from image", ex);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException ignored) {}
}
}
return new ExifWrapper(null);
}
}