-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtil.java
More file actions
executable file
·226 lines (193 loc) · 8.11 KB
/
ImageUtil.java
File metadata and controls
executable file
·226 lines (193 loc) · 8.11 KB
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
226
package example.base.util;
import android.content.*;
import android.database.*;
import android.graphics.*;
import android.media.*;
import android.net.*;
import android.provider.*;
import java.io.*;
/**
* Created by Silver on 9/17/16.
*/
public class ImageUtil {
private static final int MAX_TRANSFER_SIZE = 500000;
public static byte[] compressBitmap(File file) throws Exception {
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file));
bin.mark(bin.available());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(bin, null, options);
int targetHeight = 1200;
int targetWidth = 1600;
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth - targetWidth);
if (options.outHeight * options.outWidth >= targetHeight * targetWidth) {
double sampleSize = scaleByHeight
? options.outHeight / targetHeight
: options.outWidth / targetWidth;
options.inSampleSize = (int) Math.pow(2d, Math.floor(Math.log(sampleSize) / Math.log(2d)));
}
try {
bin.reset();
} catch (IOException e) {
bin = new BufferedInputStream(new FileInputStream(file));
}
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(bin, null, options);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
int compressionQuality = 40;
bitmap.compress(Bitmap.CompressFormat.JPEG, compressionQuality, bs);
while (bs.toByteArray().length > MAX_TRANSFER_SIZE && compressionQuality >= 10) {
try {
bs.flush();
compressionQuality = (int) (compressionQuality * 0.4);
bitmap.compress(Bitmap.CompressFormat.JPEG, compressionQuality, bs);
} catch (IOException e) {
LogUtil.e(e);
}
}
byte[] bytes = bs.toByteArray();
try {
bs.close();
bin.close();
} catch (IOException e) {
LogUtil.e(e);
}
return bytes;
}
public static byte[] rotateImageIfRequired(Context context, Uri uri, byte[] fileBytes,
File theFile) {
byte[] data = null;
Bitmap bitmap = BitmapFactory.decodeByteArray(fileBytes, 0, fileBytes.length);
ByteArrayOutputStream outputStream = null;
try {
if (theFile != null) {
bitmap = rotateImageIfRequired2(bitmap, context, uri, theFile);
} else {
bitmap = rotateImageIfRequired(bitmap, context, uri);
}
outputStream = new ByteArrayOutputStream();
int compressionQuality = 40;
bitmap.compress(Bitmap.CompressFormat.JPEG, compressionQuality, outputStream);
data = outputStream.toByteArray();
} catch (IOException e) {
LogUtil.e(e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException ignore) {
}
}
return data;
}
public static Bitmap rotateImageIfRequired(Bitmap img, Context context, Uri selectedImage) throws IOException {
if (selectedImage.getScheme().equals("content")) {
String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION};
Cursor c = context.getContentResolver().query(selectedImage, projection, null, null, null);
if (c != null && c.moveToFirst()) {
int rotation = c.getInt(0);
c.close();
return rotateImage(img, rotation);
}
return img;
} else {
ExifInterface ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
LogUtil.w("orientation: %s", orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
}
public static Bitmap rotateImageIfRequired2(Bitmap img, Context context, Uri selectedImage,
File theFile) throws IOException {
if (selectedImage.getPathSegments().get(0).equals("external_files")) {
ExifInterface ei = new ExifInterface(theFile.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
LogUtil.w("orientation: %s", orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
} else {
String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION};
Cursor c = context.getContentResolver().query(selectedImage, projection, null, null, null);
if (c != null && c.moveToFirst()) {
int rotation = c.getInt(0);
c.close();
return rotateImage(img, rotation);
}
return img;
}
}
private static Bitmap rotateImage(Bitmap img, int degree) {
if (img == null) {
return null;
}
Matrix matrix = new Matrix();
matrix.postRotate(degree);
try {
return Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
} catch (Exception e) {
LogUtil.e(e);
}
return null;
}
public static byte[] compressBitmap(InputStream stream) throws Exception {
BufferedInputStream bin = new BufferedInputStream(stream);
bin.mark(bin.available());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(bin, null, options);
int targetHeight = 1200;
int targetWidth = 1600;
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth - targetWidth);
if (options.outHeight * options.outWidth >= targetHeight * targetWidth) {
double sampleSize = scaleByHeight
? options.outHeight / targetHeight
: options.outWidth / targetWidth;
options.inSampleSize = (int) Math.pow(2d, Math.floor(Math.log(sampleSize) / Math.log(2d)));
}
try {
bin.reset();
} catch (IOException e) {
bin = new BufferedInputStream(stream);
}
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(bin, null, options);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
int compressionQuality = 40;
bitmap.compress(Bitmap.CompressFormat.JPEG, compressionQuality, bs);
while (bs.toByteArray().length > MAX_TRANSFER_SIZE && compressionQuality >= 10) {
try {
bs.flush();
compressionQuality = (int) (compressionQuality * 0.4);
bitmap.compress(Bitmap.CompressFormat.JPEG, compressionQuality, bs);
} catch (IOException e) {
LogUtil.e(e);
}
}
byte[] bytes = bs.toByteArray();
try {
bs.close();
bin.close();
} catch (IOException e) {
LogUtil.e(e);
}
return bytes;
}
}