forked from pockethub/PocketHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtils.java
More file actions
257 lines (233 loc) · 7.25 KB
/
ImageUtils.java
File metadata and controls
257 lines (233 loc) · 7.25 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Copyright 2012 GitHub Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mobile.util;
import static android.graphics.Bitmap.Config.ARGB_8888;
import static android.graphics.Color.WHITE;
import static android.graphics.PorterDuff.Mode.DST_IN;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.Log;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Image utilities
*/
public class ImageUtils {
private static final String TAG = "ImageUtils";
/**
* Get a bitmap from the image path
*
* @param imagePath
* @return bitmap or null if read fails
*/
public static Bitmap getBitmap(final String imagePath) {
return getBitmap(imagePath, 1);
}
/**
* Get a bitmap from the image path
*
* @param imagePath
* @param sampleSize
* @return bitmap or null if read fails
*/
public static Bitmap getBitmap(final String imagePath, int sampleSize) {
final Options options = new Options();
options.inDither = false;
options.inSampleSize = sampleSize;
RandomAccessFile file = null;
try {
file = new RandomAccessFile(imagePath, "r");
return BitmapFactory.decodeFileDescriptor(file.getFD(), null,
options);
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
return null;
} finally {
if (file != null)
try {
file.close();
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
}
}
}
/**
* Get a bitmap from the image
*
* @param image
* @param sampleSize
* @return bitmap or null if read fails
*/
public static Bitmap getBitmap(final byte[] image, int sampleSize) {
final Options options = new Options();
options.inDither = false;
options.inSampleSize = sampleSize;
return BitmapFactory.decodeByteArray(image, 0, image.length, options);
}
/**
* Get scale for image of size and max height/width
*
* @param size
* @param width
* @param height
* @return scale
*/
public static int getScale(Point size, int width, int height) {
if (size.x > width || size.y > height)
return Math.max(Math.round((float) size.y / (float) height),
Math.round((float) size.x / (float) width));
else
return 1;
}
/**
* Get size of image
*
* @param imagePath
* @return size
*/
public static Point getSize(final String imagePath) {
final Options options = new Options();
options.inJustDecodeBounds = true;
RandomAccessFile file = null;
try {
file = new RandomAccessFile(imagePath, "r");
BitmapFactory.decodeFileDescriptor(file.getFD(), null, options);
return new Point(options.outWidth, options.outHeight);
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
return null;
} finally {
if (file != null)
try {
file.close();
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
}
}
}
/**
* Get size of image
*
* @param image
* @return size
*/
public static Point getSize(final byte[] image) {
final Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(image, 0, image.length, options);
return new Point(options.outWidth, options.outHeight);
}
/**
* Get bitmap with maximum height or width
*
* @param imagePath
* @param width
* @param height
* @return image
*/
public static Bitmap getBitmap(final String imagePath, int width, int height) {
Point size = getSize(imagePath);
return getBitmap(imagePath, getScale(size, width, height));
}
/**
* Get bitmap with maximum height or width
*
* @param image
* @param width
* @param height
* @return image
*/
public static Bitmap getBitmap(final byte[] image, int width, int height) {
Point size = getSize(image);
return getBitmap(image, getScale(size, width, height));
}
/**
* Get bitmap with maximum height or width
*
* @param image
* @param width
* @param height
* @return image
*/
public static Bitmap getBitmap(final File image, int width, int height) {
return getBitmap(image.getAbsolutePath(), width, height);
}
/**
* Get a bitmap from the image file
*
* @param image
* @return bitmap or null if read fails
*/
public static Bitmap getBitmap(final File image) {
return getBitmap(image.getAbsolutePath());
}
/**
* Load a {@link Bitmap} from the given path and set it on the given
* {@link ImageView}
*
* @param imagePath
* @param view
*/
public static void setImage(final String imagePath, final ImageView view) {
setImage(new File(imagePath), view);
}
/**
* Load a {@link Bitmap} from the given {@link File} and set it on the given
* {@link ImageView}
*
* @param image
* @param view
*/
public static void setImage(final File image, final ImageView view) {
Bitmap bitmap = getBitmap(image);
if (bitmap != null)
view.setImageBitmap(bitmap);
}
/**
* Round the corners of a {@link Bitmap}
*
* @param source
* @param radius
* @return rounded corner bitmap
*/
public static Bitmap roundCorners(final Bitmap source, final float radius) {
int width = source.getWidth();
int height = source.getHeight();
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(WHITE);
Bitmap clipped = Bitmap.createBitmap(width, height, ARGB_8888);
Canvas canvas = new Canvas(clipped);
canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius,
paint);
paint.setXfermode(new PorterDuffXfermode(DST_IN));
Bitmap rounded = Bitmap.createBitmap(width, height, ARGB_8888);
canvas = new Canvas(rounded);
canvas.drawBitmap(source, 0, 0, null);
canvas.drawBitmap(clipped, 0, 0, paint);
source.recycle();
clipped.recycle();
return rounded;
}
}