-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbitmap.java
225 lines (176 loc) · 6.81 KB
/
bitmap.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import java.io.Closeable;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Yakiv M. on 22.10.2014.
*/
public class BitmapUtils extends BitmapFactory {
public static final String TAG = BitmapUtils.class.getSimpleName();
@Nullable
public static Bitmap decodeUri(@Nullable Context context, @Nullable Uri fileUri) {
if (context == null || fileUri == null) {
return null;
}
String path = UriUtils.getPathFromUri(context, fileUri);
return decodeFile(path);
}
@Nullable
public static Bitmap decodeUri(@Nullable Context context, @Nullable Uri fileUri, int minWidth,
int minHeight) {
if (context == null || fileUri == null) {
return null;
}
String path = UriUtils.getPathFromUri(context, fileUri);
Options options = new Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
if (minWidth > 0 || minHeight > 0) {
// First decode with inJustDecodeBounds=true to check dimensions
options.inJustDecodeBounds = true;
decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize2(options, minWidth, minHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
}
return decodeFile(path, options);
}
@NonNull
public static Options decodeBounds(@Nullable Context context, @Nullable Uri fileUri) {
if (context == null || fileUri == null) {
return null;
}
String path = UriUtils.getPathFromUri(context, fileUri);
Options options = new Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inJustDecodeBounds = true;
decodeFile(path);
return options;
}
@Nullable
public static Bitmap decodeFile(@NonNull String path, int minWidth, int minHeight) {
Options options = null;
if (minWidth > 0 || minHeight > 0) {
// First decode with inJustDecodeBounds=true to check dimensions
options = new Options();
options.inJustDecodeBounds = true;
decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, minWidth, minHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
}
return decodeFile(path, options);
}
@Nullable
public static Bitmap decodeResources(@NonNull Resources res, int resId, int minWidth,
int minHeight) {
BitmapFactory.Options options = null;
if (minWidth > 0 || minHeight > 0) {
// First decode with inJustDecodeBounds=true to check dimensions
options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, minWidth, minHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
}
return decodeResource(res, resId, options);
}
public static void save(@Nullable Bitmap bitmap, @Nullable Uri fileUri) {
if (fileUri == null || bitmap == null) {
return;
}
FileOutputStream out = null;
try {
out = new FileOutputStream(fileUri.getPath());
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (Exception e) {
Log.d(TAG, e.toString());
} finally {
closeStream(out);
}
}
@NonNull
public static Bitmap rotateBitmap(@NonNull Context context, @NonNull Uri fileUri,
@NonNull Bitmap bitmap) {
Matrix matrix = new Matrix();
try {
ExifInterface exif = new ExifInterface(UriUtils.getPathFromUri(context, fileUri));
int rotation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
if (rotation != 0f) {
matrix.preRotate(rotationInDegrees);
}
} catch (IOException e) {
Log.d(TAG, e.toString());
}
return Bitmap.createBitmap(
bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true
);
}
public static int exifToDegrees(int exifOrientation) {
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
}
return 0;
}
private static void closeStream(@Nullable Closeable is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.d(TAG, e.toString());
}
}
}
private static int calculateInSampleSize(BitmapFactory.Options o, int minWidth, int minHeight) {
// Raw height and width of image
final int height = o.outHeight;
final int width = o.outWidth;
int inSampleSize = 1;
if (height > minHeight || width > minWidth) {
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) > minHeight
&& (halfWidth / inSampleSize) > minWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
private static int calculateInSampleSize2(BitmapFactory.Options o, int minWidth,
int minHeight) {
final int height = o.outHeight;
final int width = o.outWidth;
int inSampleSize = 1;
if (height > minHeight || width > minWidth) {
int minLargerSide = Math.max(minWidth, minHeight);
int largerSide = Math.max(height, width);
final int halfLargerSide = largerSide / 2;
while ((halfLargerSide / inSampleSize) > minLargerSide) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}