-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImageUtil.java
More file actions
416 lines (392 loc) · 17.8 KB
/
ImageUtil.java
File metadata and controls
416 lines (392 loc) · 17.8 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package com.gordonlu.imageutil;
import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import android.graphics.ColorMatrix;
import android.view.View;
import androidx.core.content.ContextCompat;
import android.view.View;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import android.widget.ImageView;
import android.graphics.PorterDuff.Mode;
import android.graphics.ColorMatrixColorFilter;
import java.lang.Object;
import android.graphics.Paint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.view.View.OnLongClickListener;
import java.util.Arrays;
import java.lang.Object;
import android.graphics.Color;
import android.graphics.Typeface;
@DesignerComponent(
version = 4,
description = "A non-visible extension that provides additional tools to the built-in Image component." +
"<br><br>Made by Gordon Lu (AICODE). For details, please read my website: " +
"<a href='https://sites.google.com/view/appinventor-aicode/my-extensions/imageutil'>https://sites.google.com/view/appinventor-aicode/my-extensions/imageutil</a>",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "https://docs.google.com/drawings/d/e/2PACX-1vQCI87PHLBF0jb8QWyYmIRQSjjNW3EFXf-qpsWCvBYkUQ9vEgPAB8SpxcMpblxNpbIYrjCjLrRLIU2c/pub?w=16&h=16")
@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")
public class ImageUtil extends AndroidNonvisibleComponent {
//Activity and Context
private Context context;
private Activity activity;
public ImageUtil(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}
@SimpleFunction(description = "Applies grayscale effect to an image and fades the image.")
public void ApplyGrayscaleAndFade(AndroidViewComponent image){
// https://stackoverflow.com/a/28312202/17802442
View view = image.getView();
ImageView v = (ImageView) view;
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(matrix);
v.setColorFilter(cf);
v.setImageAlpha(128);
}
@SimpleFunction(description = "Undos the grayscale and fade effects applied with the AplyGrayscaleAndFade method.")
public void UndoGrayscaleAndFade (AndroidViewComponent image) {
View view = image.getView();
ImageView v = (ImageView) view;
v.setColorFilter(null);
v.setImageAlpha(255);
}
@SimpleFunction(description = "Checks whether this ImageView crops to padding.")
public boolean IsCroppedToPadding(AndroidViewComponent image) {
View view = image.getView();
ImageView v = (ImageView) view;
return v.getCropToPadding();
}
@SimpleFunction(description = "Returns the alpha that was applied to this ImageView.")
public int ImageAlpha(AndroidViewComponent image) {
View view = image.getView();
ImageView v = (ImageView) view;
return v.getImageAlpha();
}
@SimpleFunction(description = "Sets the alpha value that should be applied to the image.")
public void SetImageAlpha(AndroidViewComponent image, int alpha) {
View view = image.getView();
ImageView v = (ImageView) view;
v.setImageAlpha(alpha);
}
@SimpleFunction(description = "Change the image tint color. You can use too alpha values if you want with the 'make a list' block."+
" Do not forget to use the 'make color' block together with the 'make a list' block.")
public void SetImageTintColor(AndroidViewComponent image, int tint) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.setColorFilter(tint);
}
@SimpleFunction(description = "Applies watermark text to the given image's content. Set the text of the watermark via the watermark parameter" +
", and the x and y parameters are the co-ordinates of the watermark located on the image. The parameter color is the text color of the watermark, and alpha is the luminance of the watermark." +
" The size parameter is the size of the watermark in points. Specify whether to underline the watermark via the underline boolean parameter. " +
"Use the blocks in the properties of this extension for the font parameter. If useCurrentFont is true, the font parameter will be ignored. ")
public void ApplyWatermark(AndroidViewComponent image, String watermark, int x, int y, int color, int alpha, int size, boolean underline, String font, boolean useCurrentFont) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap src = drawable.getBitmap();
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
Typeface t = Typeface.DEFAULT;
if (useCurrentFont) {
t = paint.getTypeface();
} else {
if (font == "DEFAULT") {
t = Typeface.DEFAULT;
} else if (font == "SERIF") {
t = Typeface.SERIF;
} else if (font == "SANS SERIF") {
t = Typeface.SANS_SERIF;
} else if (font == "MONOSPACE") {
t = Typeface.MONOSPACE;
}
}
paint.setTypeface(t);
canvas.drawText(watermark, x, y, paint);
Bitmap bitmap = result;
imageView.setImageBitmap(bitmap);
}
@SimpleFunction(description = "Applies round corners to the Image, with the radius parameter as the radius of each corner.")
public void RoundCorners(AndroidViewComponent image, int radius) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
// https://stackoverflow.com/questions/2459916/how-to-make-an-imageview-with-rounded-corners#:~:text=https%3A//stackoverflow.com/a/3292810/17802442
Bitmap bitmap = drawable.getBitmap();
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = radius;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
imageView.setImageBitmap(output);
}
@SimpleFunction(description = "Adds gamma effect to the given image.")
public void GammaEffect(AndroidViewComponent image, double red, double green, double blue) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
// https://shaikhhamadali.blogspot.com/2013/06/before-gamma-correction-after-gamma.html
Bitmap src = drawable.getBitmap();
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
int width = src.getWidth();
int height = src.getHeight();
int A, R, G, B;
int pixel;
final int MAX_SIZE = 256;
final double MAX_VALUE_DBL = 255.0;
final int MAX_VALUE_INT = 255;
final double REVERSE = 1.0;
int[] gammaR = new int[MAX_SIZE];
int[] gammaG = new int[MAX_SIZE];
int[] gammaB = new int[MAX_SIZE];
for(int i = 0; i < MAX_SIZE; ++i) {
gammaR[i] = (int)Math.min(MAX_VALUE_INT,
(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / red)) + 0.5));
gammaG[i] = (int)Math.min(MAX_VALUE_INT,
(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / green)) + 0.5));
gammaB[i] = (int)Math.min(MAX_VALUE_INT,
(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / blue)) + 0.5));
}
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = gammaR[Color.red(pixel)];
G = gammaG[Color.green(pixel)];
B = gammaB[Color.blue(pixel)];
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
imageView.setImageBitmap(bmOut);
}
@SimpleFunction(description = "Applies color boost to the content of the given content. " +
"To find out the type parameter, go to: https://shaikhhamadali.blogspot.com/2013/07/color-boost-imagebitmap-in-imageview.html.")
public void ColorBoost(AndroidViewComponent image, int type, float percent) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap src = drawable.getBitmap();
int width = src.getWidth();
int height = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
int A, R, G, B;
int pixel;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
if(type == 1) {
R = (int)(R * (1 + percent));
if(R > 255) R = 255;
}
else if(type == 2) {
G = (int)(G * (1 + percent));
if(G > 255) G = 255;
}
else if(type == 3) {
B = (int)(B * (1 + percent));
if(B > 255) B = 255;
}
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
imageView.setImageBitmap(bmOut);
}
@SimpleProperty(description = "A font block.")
public String DefaultFont() {
return "DEFAULT";
}
@SimpleProperty(description = "A font block.")
public String Serif() {
return "SERIF";
}
@SimpleProperty(description = "A font block.")
public String SansSerif() {
return "SANS SERIF";
}
@SimpleProperty(description = "A font block.")
public String Monospace() {
return "MONOSPACE";
}
@SimpleFunction(description = "Sets the color depth of the given image.")
public void SetColorDepth(AndroidViewComponent image, int bitOffset) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap src = drawable.getBitmap();
int width = src.getWidth();
int height = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
int A, R, G, B;
int pixel;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R = ((R + (bitOffset / 2)) - ((R + (bitOffset / 2)) % bitOffset) - 1);
if(R < 0) { R = 0; }
G = ((G + (bitOffset / 2)) - ((G + (bitOffset / 2)) % bitOffset) - 1);
if(G < 0) { G = 0; }
B = ((B + (bitOffset / 2)) - ((B + (bitOffset / 2)) % bitOffset) - 1);
if(B < 0) { B = 0; }
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
imageView.setImageBitmap(bmOut);
}
@SimpleFunction(description = "Applies hue filter for your image with the given level.")
public void HueFilter(AndroidViewComponent image, int level) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap source = drawable.getBitmap();
// https://shaikhhamadali.blogspot.com/2013/07/before-hue-filter-effect-after-hue.html
int width = source.getWidth();
int height = source.getHeight();
int[] pixels = new int[width * height];
float[] HSV = new float[3];
source.getPixels(pixels, 0, width, 0, 0, width, height);
int index = 0;
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
index = y * width + x;
Color.colorToHSV(pixels[index], HSV);
HSV[0] *= level;
HSV[0] = (float) Math.max(0.0, Math.min(HSV[0], 360.0));
pixels[index] |= Color.HSVToColor(HSV);
}
}
Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
imageView.setImageBitmap(bmOut);
}
@SimpleFunction(description = "Sets the brightness of the content for the given image, according to the value parameter.")
public void SetBrightness(AndroidViewComponent image, int value) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap src = drawable.getBitmap();
// https://shaikhhamadali.blogspot.com/2013/07/set-brightness-of-imageincreasedecrease.html
int width = src.getWidth();
int height = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
int A, R, G, B;
int pixel;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R += value;
if(R > 255) { R = 255; }
else if(R < 0) { R = 0; }
G += value;
if(G > 255) { G = 255; }
else if(G < 0) { G = 0; }
B += value;
if(B > 255) { B = 255; }
else if(B < 0) { B = 0; }
// apply new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
imageView.setImageBitmap(bmOut);
}
@SimpleFunction(description = "Sets the padding of the given image.")
public void SetPadding(AndroidViewComponent image, int left, int top, int right, int bottom) {
View view = image.getView();
view.setPadding(left, top, right, bottom);
}
@SimpleFunction(description = "Sets the image to a Sepia Toning version of the image.")
public void SepiaToningEffect(AndroidViewComponent image, int depth, double red, double green, double blue) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap src = drawable.getBitmap();
int width = src.getWidth();
int height = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
final double GS_RED = 0.3;
final double GS_GREEN = 0.59;
final double GS_BLUE = 0.11;
int A, R, G, B;
int pixel;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
R += (depth * red);
if(R > 255) { R = 255; }
G += (depth * green);
if(G > 255) { G = 255; }
B += (depth * blue);
if(B > 255) { B = 255; }
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
imageView.setImageBitmap(bmOut);
}
@SimpleFunction(description = "Sets whether the image should crop to padding.")
public void SetCropToPadding(AndroidViewComponent image, boolean cropToPadding) {
View view = image.getView();
ImageView imageView = (ImageView) view;
imageView.setCropToPadding(cropToPadding);
}
}