-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathImageUtils.java
More file actions
1925 lines (1784 loc) · 68 KB
/
ImageUtils.java
File metadata and controls
1925 lines (1784 loc) · 68 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
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 com.blankj.utilcode.util;
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.media.ExifInterface;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.FloatRange;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
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;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/08/12
* desc : utils about image
* </pre>
*/
public final class ImageUtils {
private ImageUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Bitmap to bytes.
*
* @param bitmap The bitmap.
* @param format The format of bitmap.
* @return bytes
*/
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();
}
/**
* Bytes to bitmap.
*
* @param bytes The bytes.
* @return bitmap
*/
public static Bitmap bytes2Bitmap(final byte[] bytes) {
return (bytes == null || bytes.length == 0)
? null
: BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
/**
* Drawable to bitmap.
*
* @param drawable The drawable.
* @return bitmap
*/
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;
}
/**
* Bitmap to drawable.
*
* @param bitmap The bitmap.
* @return drawable
*/
public static Drawable bitmap2Drawable(final Bitmap bitmap) {
return bitmap == null ? null : new BitmapDrawable(Utils.getApp().getResources(), bitmap);
}
/**
* Drawable to bytes.
*
* @param drawable The drawable.
* @param format The format of bitmap.
* @return bytes
*/
public static byte[] drawable2Bytes(final Drawable drawable, final CompressFormat format) {
return drawable == null ? null : bitmap2Bytes(drawable2Bitmap(drawable), format);
}
/**
* Bytes to drawable.
*
* @param bytes The bytes.
* @return drawable
*/
public static Drawable bytes2Drawable(final byte[] bytes) {
return bitmap2Drawable(bytes2Bitmap(bytes));
}
/**
* View to bitmap.
*
* @param view The view.
* @return bitmap
*/
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;
}
/**
* Return bitmap.
*
* @param file The file.
* @return bitmap
*/
public static Bitmap getBitmap(final File file) {
if (file == null) return null;
return BitmapFactory.decodeFile(file.getAbsolutePath());
}
/**
* Return bitmap.
*
* @param file The file.
* @param maxWidth The maximum width.
* @param maxHeight The maximum height.
* @return bitmap
*/
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);
}
/**
* Return bitmap.
*
* @param filePath The path of file.
* @return bitmap
*/
public static Bitmap getBitmap(final String filePath) {
if (isSpace(filePath)) return null;
return BitmapFactory.decodeFile(filePath);
}
/**
* Return bitmap.
*
* @param filePath The path of file.
* @param maxWidth The maximum width.
* @param maxHeight The maximum height.
* @return bitmap
*/
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);
}
/**
* Return bitmap.
*
* @param is The input stream.
* @return bitmap
*/
public static Bitmap getBitmap(final InputStream is) {
if (is == null) return null;
return BitmapFactory.decodeStream(is);
}
/**
* Return bitmap.
*
* @param is The input stream.
* @param maxWidth The maximum width.
* @param maxHeight The maximum height.
* @return bitmap
*/
public static Bitmap getBitmap(final InputStream is, final int maxWidth, final int maxHeight) {
if (is == null) return null;
byte[] bytes = input2Byte(is);
return getBitmap(bytes, 0, maxWidth, maxHeight);
}
/**
* Return bitmap.
*
* @param data The data.
* @param offset The offset.
* @return bitmap
*/
public static Bitmap getBitmap(final byte[] data, final int offset) {
if (data.length == 0) return null;
return BitmapFactory.decodeByteArray(data, offset, data.length);
}
/**
* Return bitmap.
*
* @param data The data.
* @param offset The offset.
* @param maxWidth The maximum width.
* @param maxHeight The maximum height.
* @return bitmap
*/
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);
}
/**
* Return bitmap.
*
* @param resId The resource id.
* @return bitmap
*/
public static Bitmap getBitmap(@DrawableRes final int resId) {
Drawable drawable = ContextCompat.getDrawable(Utils.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;
}
/**
* Return bitmap.
*
* @param resId The resource id.
* @param maxWidth The maximum width.
* @param maxHeight The maximum height.
* @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 = Utils.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);
}
/**
* Return bitmap.
*
* @param fd The file descriptor.
* @return bitmap
*/
public static Bitmap getBitmap(final FileDescriptor fd) {
if (fd == null) return null;
return BitmapFactory.decodeFileDescriptor(fd);
}
/**
* Return bitmap.
*
* @param fd The file descriptor
* @param maxWidth The maximum width.
* @param maxHeight The maximum height.
* @return bitmap
*/
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);
}
/**
* Return the bitmap with the specified color.
*
* @param src The source of bitmap.
* @param color The color.
* @return the bitmap with the specified color
*/
public static Bitmap drawColor(@NonNull final Bitmap src, @ColorInt final int color) {
return drawColor(src, color, false);
}
/**
* Return the bitmap with the specified color.
*
* @param src The source of bitmap.
* @param color The color.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the bitmap with the specified color
*/
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;
}
/**
* Return the scaled bitmap.
*
* @param src The source of bitmap.
* @param newWidth The new width.
* @param newHeight The new height.
* @return the scaled bitmap
*/
public static Bitmap scale(final Bitmap src, final int newWidth, final int newHeight) {
return scale(src, newWidth, newHeight, false);
}
/**
* Return the scaled bitmap.
*
* @param src The source of bitmap.
* @param newWidth The new width.
* @param newHeight The new height.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the scaled bitmap
*/
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;
}
/**
* Return the scaled bitmap
*
* @param src The source of bitmap.
* @param scaleWidth The scale of width.
* @param scaleHeight The scale of height.
* @return the scaled bitmap
*/
public static Bitmap scale(final Bitmap src, final float scaleWidth, final float scaleHeight) {
return scale(src, scaleWidth, scaleHeight, false);
}
/**
* Return the scaled bitmap
*
* @param src The source of bitmap.
* @param scaleWidth The scale of width.
* @param scaleHeight The scale of height.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the scaled bitmap
*/
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;
}
/**
* Return the clipped bitmap.
*
* @param src The source of bitmap.
* @param x The x coordinate of the first pixel.
* @param y The y coordinate of the first pixel.
* @param width The width.
* @param height The height.
* @return the clipped bitmap
*/
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);
}
/**
* Return the clipped bitmap.
*
* @param src The source of bitmap.
* @param x The x coordinate of the first pixel.
* @param y The y coordinate of the first pixel.
* @param width The width.
* @param height The height.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the clipped bitmap
*/
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;
}
/**
* Return the skewed bitmap.
*
* @param src The source of bitmap.
* @param kx The skew factor of x.
* @param ky The skew factor of y.
* @return the skewed bitmap
*/
public static Bitmap skew(final Bitmap src, final float kx, final float ky) {
return skew(src, kx, ky, 0, 0, false);
}
/**
* Return the skewed bitmap.
*
* @param src The source of bitmap.
* @param kx The skew factor of x.
* @param ky The skew factor of y.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the skewed bitmap
*/
public static Bitmap skew(final Bitmap src,
final float kx,
final float ky,
final boolean recycle) {
return skew(src, kx, ky, 0, 0, recycle);
}
/**
* Return the skewed bitmap.
*
* @param src The source of bitmap.
* @param kx The skew factor of x.
* @param ky The skew factor of y.
* @param px The x coordinate of the pivot point.
* @param py The y coordinate of the pivot point.
* @return the skewed bitmap
*/
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);
}
/**
* Return the skewed bitmap.
*
* @param src The source of bitmap.
* @param kx The skew factor of x.
* @param ky The skew factor of y.
* @param px The x coordinate of the pivot point.
* @param py The y coordinate of the pivot point.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the skewed bitmap
*/
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;
}
/**
* Return the rotated bitmap.
*
* @param src The source of bitmap.
* @param degrees The number of degrees.
* @param px The x coordinate of the pivot point.
* @param py The y coordinate of the pivot point.
* @return the rotated bitmap
*/
public static Bitmap rotate(final Bitmap src,
final int degrees,
final float px,
final float py) {
return rotate(src, degrees, px, py, false);
}
/**
* Return the rotated bitmap.
*
* @param src The source of bitmap.
* @param degrees The number of degrees.
* @param px The x coordinate of the pivot point.
* @param py The y coordinate of the pivot point.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the rotated bitmap
*/
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;
}
/**
* Return the rotated degree.
*
* @param filePath The path of file.
* @return the rotated degree
*/
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;
}
}
/**
* Return the round bitmap.
*
* @param src The source of bitmap.
* @return the round bitmap
*/
public static Bitmap toRound(final Bitmap src) {
return toRound(src, 0, 0, false);
}
/**
* Return the round bitmap.
*
* @param src The source of bitmap.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the round bitmap
*/
public static Bitmap toRound(final Bitmap src, final boolean recycle) {
return toRound(src, 0, 0, recycle);
}
/**
* Return the round bitmap.
*
* @param src The source of bitmap.
* @param borderSize The size of border.
* @param borderColor The color of border.
* @return the round bitmap
*/
public static Bitmap toRound(final Bitmap src,
@IntRange(from = 0) int borderSize,
@ColorInt int borderColor) {
return toRound(src, borderSize, borderColor, false);
}
/**
* Return the round bitmap.
*
* @param src The source of bitmap.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @param borderSize The size of border.
* @param borderColor The color of border.
* @return the round bitmap
*/
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;
}
/**
* Return the round corner bitmap.
*
* @param src The source of bitmap.
* @param radius The radius of corner.
* @return the round corner bitmap
*/
public static Bitmap toRoundCorner(final Bitmap src, final float radius) {
return toRoundCorner(src, radius, 0, 0, false);
}
/**
* Return the round corner bitmap.
*
* @param src The source of bitmap.
* @param radius The radius of corner.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the round corner bitmap
*/
public static Bitmap toRoundCorner(final Bitmap src,
final float radius,
final boolean recycle) {
return toRoundCorner(src, radius, 0, 0, recycle);
}
/**
* Return the round corner bitmap.
*
* @param src The source of bitmap.
* @param radius The radius of corner.
* @param borderSize The size of border.
* @param borderColor The color of border.
* @return the round corner bitmap
*/
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);
}
/**
* Return the round corner bitmap.
*
* @param src The source of bitmap.
* @param radius The radius of corner.
* @param borderSize The size of border.
* @param borderColor The color of border.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the round corner bitmap
*/
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;
}
/**
* Return the round corner bitmap with border.
*
* @param src The source of bitmap.
* @param borderSize The size of border.
* @param color The color of border.
* @param cornerRadius The radius of corner.
* @return the round corner bitmap with border
*/
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);
}
/**
* Return the round corner bitmap with border.
*
* @param src The source of bitmap.
* @param borderSize The size of border.
* @param color The color of border.
* @param cornerRadius The radius of corner.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the round corner bitmap with border
*/
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);
}
/**
* Return the round bitmap with border.
*
* @param src The source of bitmap.
* @param borderSize The size of border.
* @param color The color of border.
* @return the round bitmap with border
*/
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);
}
/**
* Return the round bitmap with border.
*
* @param src The source of bitmap.
* @param borderSize The size of border.
* @param color The color of border.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the round bitmap with border
*/
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);
}
/**
* Return the bitmap with border.
*
* @param src The source of bitmap.
* @param borderSize The size of border.
* @param color The color of border.
* @param isCircle True to draw circle, false to draw corner.
* @param cornerRadius The radius of corner.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the bitmap with border
*/
private static Bitmap addBorder(final Bitmap src,
@IntRange(from = 1) final int borderSize,
@ColorInt final int color,
final boolean isCircle,
final float cornerRadius,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
int width = ret.getWidth();
int height = ret.getHeight();
Canvas canvas = new Canvas(ret);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(borderSize);
if (isCircle) {
float radius = Math.min(width, height) / 2f - borderSize / 2f;
canvas.drawCircle(width / 2f, height / 2f, radius, paint);
} else {
int halfBorderSize = borderSize >> 1;
RectF rectF = new RectF(halfBorderSize, halfBorderSize,
width - halfBorderSize, height - halfBorderSize);
canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
}
return ret;
}
/**
* Return the bitmap with reflection.
*
* @param src The source of bitmap.
* @param reflectionHeight The height of reflection.
* @return the bitmap with reflection
*/
public static Bitmap addReflection(final Bitmap src, final int reflectionHeight) {
return addReflection(src, reflectionHeight, false);
}
/**
* Return the bitmap with reflection.
*
* @param src The source of bitmap.
* @param reflectionHeight The height of reflection.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the bitmap with reflection
*/
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;
}
/**
* Return the bitmap with text watermarking.
*
* @param src The source of bitmap.
* @param content The content of text.
* @param textSize The size of text.
* @param color The color of text.
* @param x The x coordinate of the first pixel.
* @param y The y coordinate of the first pixel.
* @return the bitmap with text watermarking
*/
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);
}
/**
* Return the bitmap with text watermarking.
*
* @param src The source of bitmap.
* @param content The content of text.
* @param textSize The size of text.
* @param color The color of text.
* @param x The x coordinate of the first pixel.
* @param y The y coordinate of the first pixel.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the bitmap with text watermarking
*/
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;
}
/**
* Return the bitmap with image watermarking.
*
* @param src The source of bitmap.
* @param watermark The image watermarking.
* @param x The x coordinate of the first pixel.