-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBitmapUtil.java
287 lines (257 loc) · 9.91 KB
/
BitmapUtil.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.android.kickstart.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.view.View;
import com.android.kickstart.R;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
public class BitmapUtil {
/**
* @param bitmap : Bitmap
* @param degree : Rotation degree
* @return : New rotated degree
*/
public static Bitmap rotateBitmap(Bitmap bitmap, int degree) {
// Setting pre rotate
Matrix mMatrix = new Matrix();
mMatrix.preRotate(degree);
// Rotating Bitmap & convert to ARGB_8888
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mMatrix, true);
}
/**
* @param imagePath : ImagePath
* @return : Bitmap
* @throws IOException : Exception
*/
public static Bitmap adjustImageOrientation(String imagePath) throws IOException {
Bitmap image = null;
File mFile = new File(imagePath);
if (mFile.exists())
image = BitmapFactory.decodeFile(mFile.getAbsolutePath());
else
return null;
Bitmap bitmap = null;
ExifInterface exif = new ExifInterface(imagePath);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
bitmap = rotateBitmap(image, rotate);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
bitmap = rotateBitmap(image, rotate);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
bitmap = rotateBitmap(image, rotate);
break;
default:
bitmap = image;
}
return bitmap;
}
/**
* @param context : Activity/Fragment
* @param maskResourceId : Resource Id
* @param mBitmap : Bitmap
* @return : Masked Bitmap
*/
public static Bitmap getMaskedBitmap(Context context, int maskResourceId, Bitmap mBitmap) {
Bitmap mask = BitmapFactory.decodeResource(context.getResources(), maskResourceId);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(mBitmap, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
mBitmap.recycle();
mask.recycle();
return result;
}
/**
* @param context : Activity/Fragment
* @param mask_img_id : Resource Id
* @param color : Color
* @return : Masked Bitmap
*/
public static Bitmap getMaskedImageByColor(Context context, int mask_img_id, int color) {
Bitmap mask = BitmapFactory.decodeResource(context.getResources(), mask_img_id);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Bitmap.Config.ARGB_8888);
// and width of the previous bitmap
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawColor(color);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
mask.recycle();
return result;
}
/**
* @param view : View
* @return : Bitmap of view
*/
public static Bitmap getBitmapFromView(View view) {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
/**
* @param bitmap : Bitmap
* @return : New Bitmap
*/
public static Bitmap cropBitmapToSquare(Bitmap bitmap) {
Bitmap result = null;
int height = bitmap.getHeight();
int width = bitmap.getWidth();
if (height <= width) {
result = Bitmap.createBitmap(bitmap, (width - height) / 2, 0, height, height);
} else {
result = Bitmap.createBitmap(bitmap, 0, (height - width) / 2, width, width);
}
return result;
}
/**
* @param drawableName : drawableName
* @return : resourceId
* @throws NoSuchFieldException : Exception
* @throws IllegalAccessException : Exception
*/
public static int getDrawableIdByName(String drawableName) throws NoSuchFieldException, IllegalAccessException {
if (drawableName == null)
return -1;
Field field = R.drawable.class.getDeclaredField(drawableName);
int id = field.getInt(field);
return id;
}
/**
* @param bitmap : Bitmap
* @param round : Round value
* @return : New Bitmap
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float round) {
// Source image size
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// create result bitmap output
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// set canvas for painting
Canvas canvas = new Canvas(result);
canvas.drawARGB(0, 0, 0, 0);
// configure paint
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.TRANSPARENT);
// configure rectangle for embedding
Rect rect = new Rect(0, 0, width, height);
RectF rectF = new RectF(rect);
// draw Round rectangle to canvas
canvas.drawRoundRect(rectF, round, round, paint);
// create Xfer mode
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
// draw source image to canvas
canvas.drawBitmap(bitmap, rect, rect, paint);
// return final image
return result;
}
/**
* @param bitmap : Bitmap
* @param newSize : size
* @return : New Bitmap
*/
public static Bitmap getResizedBitmap(Bitmap bitmap, int newSize) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth, newHeight;
if (width >= height) {
newHeight = newSize;
newWidth = (int) (width * ((float) newSize / height));
} else {
newWidth = newSize;
newHeight = (int) (height * ((float) newSize / width));
}
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
System.gc();
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
/**
* @param bitmap : Bitmap
* @param newHeight : Height
* @param newWidth : Width
* @return : New Bitmap
*/
public static Bitmap getResizedBitmap(Bitmap bitmap, int newHeight, int newWidth) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
System.gc();
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
/**
* @param drawable : Drawable
* @return : Bitmap
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable)
return ((BitmapDrawable) drawable).getBitmap();
Bitmap mBitmap;
if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
mBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
mBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(mBitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return mBitmap;
}
/**
* @param context : Activity/Fragment
* @param url : URL
* @return : Drawable
* @throws IOException : Exception
*/
public static Drawable drawableFromUrl(Context context, String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream input = connection.getInputStream();
Bitmap mBitmap = BitmapFactory.decodeStream(input);
return new BitmapDrawable(context.getResources(), mBitmap);
}
}