forked from Shouheng88/utils-android
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageUtils.java
1302 lines (1155 loc) · 50.7 KB
/
ImageUtils.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package me.shouheng.utils.ui;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.media.ExifInterface;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import android.view.View;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import me.shouheng.utils.UtilsApp;
import me.shouheng.utils.app.ResUtils;
import static me.shouheng.utils.data.StringUtils.isSpace;
import static me.shouheng.utils.store.FileUtils.createFileByDeleteOldFile;
import static me.shouheng.utils.store.FileUtils.getFileByPath;
import static me.shouheng.utils.store.FileUtils.getFileExtension;
import static me.shouheng.utils.store.IOUtils.is2Bytes;
/**
* 用来处理
* 1. {@link Drawable}
* 2. {@link android.graphics.Bitmap}
* 的工具类
*
* @author Shouheng Wang 2019-05-08 21:30
*/
public final class ImageUtils {
/*------------------------------------- 转换 ----------------------------------------*/
public static Bitmap view2Bitmap(final View view) {
if (view == null) return null;
Bitmap ret = Bitmap.createBitmap(view.getWidth(),
view.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ret);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return ret;
}
public static byte[] bitmap2Bytes(final Bitmap bitmap, final CompressFormat format) {
if (bitmap == null) return null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(format, 100, baos);
return baos.toByteArray();
}
public static Bitmap bytes2Bitmap(final byte[] bytes) {
return (bytes == null || bytes.length == 0)
? null
: BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
public static Bitmap drawable2Bitmap(final Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if (bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
Bitmap bitmap;
if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1,
drawable.getOpacity() != PixelFormat.OPAQUE
? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE
? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
public static Drawable bitmap2Drawable(final Bitmap bitmap) {
return bitmap == null ? null : new BitmapDrawable(UtilsApp.getApp().getResources(), bitmap);
}
public static byte[] drawable2Bytes(final Drawable drawable, final CompressFormat format) {
return drawable == null ? null : bitmap2Bytes(drawable2Bitmap(drawable), format);
}
public static Drawable bytes2Drawable(final byte[] bytes) {
return bitmap2Drawable(bytes2Bitmap(bytes));
}
public static Drawable tintDrawable(@DrawableRes int drawableRes, @ColorInt int color) {
Drawable drawable = ResUtils.getDrawable(drawableRes);
return tintDrawable(drawable, color);
}
/**
* 对 Drawable 进行着色
*
* @param drawable 要着色的 Drawable
* @param color 颜色
* @return 着色之后的 Drawable
*/
public static Drawable tintDrawable(Drawable drawable, @ColorInt int color) {
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable.mutate());
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(color));
return wrappedDrawable;
}
/**
* 获取带圆角的 drawable,各个圆角都为 radius,这里只是总结了比较常用的获取 Drawable 的方式
*
* @param color solid color
* @param radius radius of every corner
* @return the drawable
*/
public static Drawable getDrawable(@ColorInt int color,
float radius) {
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(color);
drawable.setCornerRadius(radius);
return drawable;
}
public static Drawable getDrawable(@ColorInt int color,
float radius,
int strokeWidth,
@ColorInt int strokeColor) {
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(color);
drawable.setCornerRadius(radius);
drawable.setStroke(strokeWidth, strokeColor);
return drawable;
}
/**
* 获取带圆角的 drawable
*
* @param color the solid color
* @param topLeftRadius the top left radius
* @param topRightRadius the top right radius
* @param bottomLeftRadius the bottom left radius
* @param bottomRightRadius the bottom right radius
* @return the drawable
*/
public static Drawable getDrawable(@ColorInt int color,
float topLeftRadius,
float topRightRadius,
float bottomLeftRadius,
float bottomRightRadius) {
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(color);
drawable.setCornerRadii(new float[]{
topLeftRadius, topLeftRadius,
topRightRadius, topRightRadius,
bottomRightRadius, bottomRightRadius,
bottomLeftRadius, bottomLeftRadius
});
return drawable;
}
public static Drawable getDrawable(@ColorInt int color,
float topLeftRadius,
float topRightRadius,
float bottomLeftRadius,
float bottomRightRadius,
int strokeWidth,
@ColorInt int strokeColor) {
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(color);
drawable.setStroke(strokeWidth, strokeColor);
drawable.setCornerRadii(new float[]{
topLeftRadius, topLeftRadius,
topRightRadius, topRightRadius,
bottomRightRadius, bottomRightRadius,
bottomLeftRadius, bottomLeftRadius
});
return drawable;
}
/*------------------------------------- 获取 ----------------------------------------*/
public static boolean isImage(final File file) {
return file != null && isImage(file.getPath());
}
public static boolean isImage(final String filePath) {
String path = filePath.toUpperCase();
return path.endsWith(".PNG") || path.endsWith(".JPG")
|| path.endsWith(".JPEG") || path.endsWith(".BMP")
|| path.endsWith(".GIF") || path.endsWith(".WEBP");
}
public static String getImageType(final String filePath) {
return getImageType(getFileByPath(filePath));
}
public static String getImageType(final File file) {
if (file == null) return "";
InputStream is = null;
try {
is = new FileInputStream(file);
String type = getImageType(is);
if (type != null) {
return type;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return getFileExtension(file.getAbsolutePath()).toUpperCase();
}
public static Bitmap getBitmap(final File file) {
if (file == null) return null;
return BitmapFactory.decodeFile(file.getAbsolutePath());
}
public static Bitmap getBitmap(final File file, final int maxWidth, final int maxHeight) {
if (file == null) return null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
}
public static Bitmap getBitmap(final String filePath) {
if (isSpace(filePath)) return null;
return BitmapFactory.decodeFile(filePath);
}
public static Bitmap getBitmap(final String filePath, final int maxWidth, final int maxHeight) {
if (isSpace(filePath)) return null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
public static Bitmap getBitmap(final InputStream is) {
if (is == null) return null;
return BitmapFactory.decodeStream(is);
}
public static Bitmap getBitmap(final InputStream is, final int maxWidth, final int maxHeight) {
if (is == null) return null;
byte[] bytes = is2Bytes(is);
return getBitmap(bytes, 0, maxWidth, maxHeight);
}
public static Bitmap getBitmap(final byte[] data, final int offset) {
if (data.length == 0) return null;
return BitmapFactory.decodeByteArray(data, offset, data.length);
}
public static Bitmap getBitmap(final byte[] data, final int offset, final int maxWidth, final int maxHeight) {
if (data.length == 0) return null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, offset, data.length, options);
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(data, offset, data.length, options);
}
public static Bitmap getBitmap(@DrawableRes final int resId) {
Drawable drawable = ContextCompat.getDrawable(UtilsApp.getApp(), resId);
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
public static Bitmap getBitmap(@DrawableRes final int resId, final int maxWidth, final int maxHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
final Resources resources = UtilsApp.getApp().getResources();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, resId, options);
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, resId, options);
}
public static Bitmap getBitmap(final FileDescriptor fd) {
if (fd == null) return null;
return BitmapFactory.decodeFileDescriptor(fd);
}
public static Bitmap getBitmap(final FileDescriptor fd, final int maxWidth, final int maxHeight) {
if (fd == null) return null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd, null, options);
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFileDescriptor(fd, null, options);
}
public static Bitmap drawColor(@NonNull final Bitmap src, @ColorInt final int color) {
return drawColor(src, color, false);
}
/*------------------------------------- 变换 ----------------------------------------*/
public static Bitmap drawColor(@NonNull final Bitmap src, @ColorInt final int color, final boolean recycle) {
if (isEmptyBitmap(src)) return null;
Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
Canvas canvas = new Canvas(ret);
canvas.drawColor(color, PorterDuff.Mode.DARKEN);
return ret;
}
public static Bitmap scale(final Bitmap src,
final int newWidth,
final int newHeight) {
return scale(src, newWidth, newHeight, false);
}
public static Bitmap scale(final Bitmap src,
final int newWidth,
final int newHeight,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
Bitmap ret = Bitmap.createScaledBitmap(src, newWidth, newHeight, true);
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap scale(final Bitmap src,
final float scaleWidth,
final float scaleHeight) {
return scale(src, scaleWidth, scaleHeight, false);
}
public static Bitmap scale(final Bitmap src,
final float scaleWidth,
final float scaleHeight,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
Matrix matrix = new Matrix();
matrix.setScale(scaleWidth, scaleHeight);
Bitmap ret = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap clip(final Bitmap src,
final int x,
final int y,
final int width,
final int height) {
return clip(src, x, y, width, height, false);
}
public static Bitmap clip(final Bitmap src,
final int x,
final int y,
final int width,
final int height,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
Bitmap ret = Bitmap.createBitmap(src, x, y, width, height);
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap skew(final Bitmap src,
final float kx,
final float ky) {
return skew(src, kx, ky, 0, 0, false);
}
public static Bitmap skew(final Bitmap src,
final float kx,
final float ky,
final boolean recycle) {
return skew(src, kx, ky, 0, 0, recycle);
}
public static Bitmap skew(final Bitmap src,
final float kx,
final float ky,
final float px,
final float py) {
return skew(src, kx, ky, px, py, false);
}
public static Bitmap skew(final Bitmap src,
final float kx,
final float ky,
final float px,
final float py,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
Matrix matrix = new Matrix();
matrix.setSkew(kx, ky, px, py);
Bitmap ret = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap rotate(final Bitmap src,
final int degrees,
final float px,
final float py) {
return rotate(src, degrees, px, py, false);
}
public static Bitmap rotate(final Bitmap src,
final int degrees,
final float px,
final float py,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
if (degrees == 0) return src;
Matrix matrix = new Matrix();
matrix.setRotate(degrees, px, py);
Bitmap ret = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static int getRotateDegree(final String filePath) {
try {
ExifInterface exifInterface = new ExifInterface(filePath);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL
);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
} catch (IOException e) {
e.printStackTrace();
return -1;
}
}
public static Bitmap toRound(final Bitmap src) {
return toRound(src, 0, 0, false);
}
public static Bitmap toRound(final Bitmap src, final boolean recycle) {
return toRound(src, 0, 0, recycle);
}
public static Bitmap toRound(final Bitmap src,
@IntRange(from = 0) int borderSize,
@ColorInt int borderColor) {
return toRound(src, borderSize, borderColor, false);
}
public static Bitmap toRound(final Bitmap src,
@IntRange(from = 0) int borderSize,
@ColorInt int borderColor,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
int width = src.getWidth();
int height = src.getHeight();
int size = Math.min(width, height);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap ret = Bitmap.createBitmap(width, height, src.getConfig());
float center = size / 2f;
RectF rectF = new RectF(0, 0, width, height);
rectF.inset((width - size) / 2f, (height - size) / 2f);
Matrix matrix = new Matrix();
matrix.setTranslate(rectF.left, rectF.top);
matrix.preScale((float) size / width, (float) size / height);
BitmapShader shader = new BitmapShader(src, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
shader.setLocalMatrix(matrix);
paint.setShader(shader);
Canvas canvas = new Canvas(ret);
canvas.drawRoundRect(rectF, center, center, paint);
if (borderSize > 0) {
paint.setShader(null);
paint.setColor(borderColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(borderSize);
float radius = center - borderSize / 2f;
canvas.drawCircle(width / 2f, height / 2f, radius, paint);
}
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap toRoundCorner(final Bitmap src, final float radius) {
return toRoundCorner(src, radius, 0, 0, false);
}
public static Bitmap toRoundCorner(final Bitmap src,
final float radius,
final boolean recycle) {
return toRoundCorner(src, radius, 0, 0, recycle);
}
public static Bitmap toRoundCorner(final Bitmap src,
final float radius,
@IntRange(from = 0) int borderSize,
@ColorInt int borderColor) {
return toRoundCorner(src, radius, borderSize, borderColor, false);
}
public static Bitmap toRoundCorner(final Bitmap src,
final float radius,
@IntRange(from = 0) int borderSize,
@ColorInt int borderColor,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
int width = src.getWidth();
int height = src.getHeight();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap ret = Bitmap.createBitmap(width, height, src.getConfig());
BitmapShader shader = new BitmapShader(src, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
Canvas canvas = new Canvas(ret);
RectF rectF = new RectF(0, 0, width, height);
float halfBorderSize = borderSize / 2f;
rectF.inset(halfBorderSize, halfBorderSize);
canvas.drawRoundRect(rectF, radius, radius, paint);
if (borderSize > 0) {
paint.setShader(null);
paint.setColor(borderColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(borderSize);
paint.setStrokeCap(Paint.Cap.ROUND);
canvas.drawRoundRect(rectF, radius, radius, paint);
}
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap addCornerBorder(final Bitmap src,
@IntRange(from = 1) final int borderSize,
@ColorInt final int color,
@FloatRange(from = 0) final float cornerRadius) {
return addBorder(src, borderSize, color, false, cornerRadius, false);
}
public static Bitmap addCornerBorder(final Bitmap src,
@IntRange(from = 1) final int borderSize,
@ColorInt final int color,
@FloatRange(from = 0) final float cornerRadius,
final boolean recycle) {
return addBorder(src, borderSize, color, false, cornerRadius, recycle);
}
public static Bitmap addCircleBorder(final Bitmap src,
@IntRange(from = 1) final int borderSize,
@ColorInt final int color) {
return addBorder(src, borderSize, color, true, 0, false);
}
public static Bitmap addCircleBorder(final Bitmap src,
@IntRange(from = 1) final int borderSize,
@ColorInt final int color,
final boolean recycle) {
return addBorder(src, borderSize, color, true, 0, recycle);
}
public static Bitmap addReflection(final Bitmap src, final int reflectionHeight) {
return addReflection(src, reflectionHeight, false);
}
public static Bitmap addReflection(final Bitmap src,
final int reflectionHeight,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
final int REFLECTION_GAP = 0;
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionBitmap = Bitmap.createBitmap(src, 0, srcHeight - reflectionHeight,
srcWidth, reflectionHeight, matrix, false);
Bitmap ret = Bitmap.createBitmap(srcWidth, srcHeight + reflectionHeight, src.getConfig());
Canvas canvas = new Canvas(ret);
canvas.drawBitmap(src, 0, 0, null);
canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
LinearGradient shader = new LinearGradient(
0, srcHeight,
0, ret.getHeight() + REFLECTION_GAP,
0x70FFFFFF,
0x00FFFFFF,
Shader.TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
canvas.drawRect(0, srcHeight + REFLECTION_GAP, srcWidth, ret.getHeight(), paint);
if (!reflectionBitmap.isRecycled()) reflectionBitmap.recycle();
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap addTextWatermark(final Bitmap src,
final String content,
final int textSize,
@ColorInt final int color,
final float x,
final float y) {
return addTextWatermark(src, content, textSize, color, x, y, false);
}
public static Bitmap addTextWatermark(final Bitmap src,
final String content,
final float textSize,
@ColorInt final int color,
final float x,
final float y,
final boolean recycle) {
if (isEmptyBitmap(src) || content == null) return null;
Bitmap ret = src.copy(src.getConfig(), true);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas(ret);
paint.setColor(color);
paint.setTextSize(textSize);
Rect bounds = new Rect();
paint.getTextBounds(content, 0, content.length(), bounds);
canvas.drawText(content, x, y + textSize, paint);
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap addImageWatermark(final Bitmap src,
final Bitmap watermark,
final int x, final int y,
final int alpha) {
return addImageWatermark(src, watermark, x, y, alpha, false);
}
public static Bitmap addImageWatermark(final Bitmap src,
final Bitmap watermark,
final int x,
final int y,
final int alpha,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
Bitmap ret = src.copy(src.getConfig(), true);
if (!isEmptyBitmap(watermark)) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas(ret);
paint.setAlpha(alpha);
canvas.drawBitmap(watermark, x, y, paint);
}
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap toAlpha(final Bitmap src) {
return toAlpha(src, false);
}
public static Bitmap toAlpha(final Bitmap src, final Boolean recycle) {
if (isEmptyBitmap(src)) return null;
Bitmap ret = src.extractAlpha();
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap toGray(final Bitmap src) {
return toGray(src, false);
}
public static Bitmap toGray(final Bitmap src, final boolean recycle) {
if (isEmptyBitmap(src)) return null;
Bitmap ret = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
Canvas canvas = new Canvas(ret);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorMatrixColorFilter);
canvas.drawBitmap(src, 0, 0, paint);
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
public static Bitmap fastBlur(final Bitmap src,
@FloatRange(
from = 0, to = 1, fromInclusive = false
) final float scale,
@FloatRange(
from = 0, to = 25, fromInclusive = false
) final float radius) {
return fastBlur(src, scale, radius, false);
}
public static Bitmap fastBlur(final Bitmap src,
@FloatRange(
from = 0, to = 1, fromInclusive = false
) final float scale,
@FloatRange(
from = 0, to = 25, fromInclusive = false
) final float radius,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
int width = src.getWidth();
int height = src.getHeight();
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
Bitmap scaleBitmap =
Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas();
PorterDuffColorFilter filter = new PorterDuffColorFilter(
Color.TRANSPARENT, PorterDuff.Mode.SRC_ATOP);
paint.setColorFilter(filter);
canvas.scale(scale, scale);
canvas.drawBitmap(scaleBitmap, 0, 0, paint);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
scaleBitmap = renderScriptBlur(scaleBitmap, radius, recycle);
} else {
scaleBitmap = stackBlur(scaleBitmap, (int) radius, recycle);
}
if (scale == 1) {
if (recycle && !src.isRecycled()) src.recycle();
return scaleBitmap;
}
Bitmap ret = Bitmap.createScaledBitmap(scaleBitmap, width, height, true);
if (!scaleBitmap.isRecycled()) scaleBitmap.recycle();
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(final Bitmap src,
@FloatRange(
from = 0, to = 25, fromInclusive = false
) final float radius) {
return renderScriptBlur(src, radius, false);
}
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(final Bitmap src,
@FloatRange(
from = 0, to = 25, fromInclusive = false
) final float radius,
final boolean recycle) {
RenderScript rs = null;
Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
try {
rs = RenderScript.create(UtilsApp.getApp());
rs.setMessageHandler(new RenderScript.RSMessageHandler());
Allocation input = Allocation.createFromBitmap(rs,
ret,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
Allocation output = Allocation.createTyped(rs, input.getType());
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
blurScript.setInput(input);
blurScript.setRadius(radius);
blurScript.forEach(output);
output.copyTo(ret);
} finally {
if (rs != null) {
rs.destroy();
}
}
return ret;
}
public static Bitmap stackBlur(final Bitmap src, final int radius) {
return stackBlur(src, radius, false);
}
public static Bitmap stackBlur(final Bitmap src, int radius, final boolean recycle) {
Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
if (radius < 1) {
radius = 1;
}
int w = ret.getWidth();
int h = ret.getHeight();
int[] pix = new int[w * h];
ret.getPixels(pix, 0, w, 0, 0, w, h);
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = radius + radius + 1;
int r[] = new int[wh];
int g[] = new int[wh];
int b[] = new int[wh];
int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
int vmin[] = new int[Math.max(w, h)];
int divsum = (div + 1) >> 1;
divsum *= divsum;
int dv[] = new int[256 * divsum];
for (i = 0; i < 256 * divsum; i++) {
dv[i] = (i / divsum);
}
yw = yi = 0;
int[][] stack = new int[div][3];
int stackpointer;
int stackstart;
int[] sir;
int rbs;
int r1 = radius + 1;
int routsum, goutsum, boutsum;
int rinsum, ginsum, binsum;
for (y = 0; y < h; y++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
for (i = -radius; i <= radius; i++) {
p = pix[yi + Math.min(wm, Math.max(i, 0))];
sir = stack[i + radius];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rbs = r1 - Math.abs(i);
rsum += sir[0] * rbs;
gsum += sir[1] * rbs;
bsum += sir[2] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
}
stackpointer = radius;
for (x = 0; x < w; x++) {
r[yi] = dv[rsum];
g[yi] = dv[gsum];
b[yi] = dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (y == 0) {
vmin[x] = Math.min(x + radius + 1, wm);
}
p = pix[yw + vmin[x]];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[(stackpointer) % div];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi++;
}
yw += w;
}
for (x = 0; x < w; x++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
yp = -radius * w;
for (i = -radius; i <= radius; i++) {
yi = Math.max(0, yp) + x;
sir = stack[i + radius];
sir[0] = r[yi];
sir[1] = g[yi];
sir[2] = b[yi];
rbs = r1 - Math.abs(i);
rsum += r[yi] * rbs;
gsum += g[yi] * rbs;
bsum += b[yi] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
if (i < hm) {
yp += w;
}
}
yi = x;
stackpointer = radius;
for (y = 0; y < h; y++) {
// Preserve alpha channel: ( 0xff000000 & pix[yi] )
pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (x == 0) {
vmin[y] = Math.min(y + r1, hm) * w;
}
p = x + vmin[y];