-
Notifications
You must be signed in to change notification settings - Fork 803
/
AppImageMgr.java
1522 lines (1374 loc) · 47.8 KB
/
AppImageMgr.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 com.jingewenku.abrahamcaijin.commonutil;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.*;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import java.io.*;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
/**
* @Description:主要功能:图片管理工具类
* @Prject: CommonUtilLibrary
* @Package: com.jingewenku.abrahamcaijin.commonutil
* @author: AbrahamCaiJin
* @date: 2017年05月22日 15:52
* @Copyright: 个人版权所有
* @Company:
* @version: 1.0.0
*/
@SuppressLint("NewApi")
public class AppImageMgr {
private WeakHashMap<Integer, WeakReference<Bitmap>> mBitmaps;
private WeakHashMap<Integer, WeakReference<Drawable>> mDrawables;
private Context mContext;
// 下载图片,最大边长
public static int MIN_SIDE_LENGTH = 256;
// 是否重新计算压缩比
public static boolean isComputeSampleSize = false;
private static final long POLY64REV = 0x95AC9329AC4BC9B5L;
private static final long INITIALCRC = 0xFFFFFFFFFFFFFFFFL;
private static long[] sCrcTable = new long[256];
public AppImageMgr(Context context) {
mContext = context.getApplicationContext();
mBitmaps = new WeakHashMap<Integer, WeakReference<Bitmap>>();
mDrawables = new WeakHashMap<Integer, WeakReference<Drawable>>();
}
/**
* 根据drawable id获取Bitmap
*
* @param resource
* @return
*/
public Bitmap getBitmap(int resource) {
if (!mBitmaps.containsKey(resource) && mContext != null) {
mBitmaps.put(resource, new WeakReference<Bitmap>(
readDrawableBitmap(mContext, resource)));
}
return ((WeakReference<Bitmap>) mBitmaps.get(resource)).get();
}
/**
* 根据drawable id获取Drawable
*
* @param resource
* @return
*/
public Drawable getDrawable(int resource) {
try {
if (!mDrawables.containsKey(resource) && mContext != null) {
try {
mDrawables.put(resource, new WeakReference<Drawable>(mContext
.getResources().getDrawable(resource)));
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
}
return ((WeakReference<Drawable>) mDrawables.get(resource)).get();
} catch (Exception e) {
e.printStackTrace();
}
return readBitmapResIdToDrawable(mContext, resource);
}
/**
*bitmap转drawable
* @param uri
* @param mcontext
* @return
*/
public static Drawable bitmapToDrawble(Uri uri,Context mcontext){
Drawable drawable = new BitmapDrawable(mcontext.getResources(), getBitmapFromUri(mcontext, uri));
return drawable;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public void recycleBitmaps() {
final Iterator itr = mBitmaps.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry e = (Map.Entry) itr.next();
if (e != null) {
final Bitmap bitmap = ((WeakReference<Bitmap>) e.getValue()).get();
if (bitmap != null) {
bitmap.recycle();
}
}
}
mBitmaps.clear();
}
/**
*
* @param context
* @param resId
* @return
*/
public static Drawable readBitmapResIdToDrawable(Context context, int resId) {
final BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
final InputStream is = context.getResources().openRawResource(resId);
final Bitmap btm = BitmapFactory.decodeStream(is, null, opt);
if (btm != null) {
final BitmapDrawable bd = new BitmapDrawable(btm);
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return bd;
}
return null;
}
/**
* 以最省内存的方式读取本地资源的图片
*
* @param context
* @param resId
* @return
*/
public static Bitmap readDrawableBitmap(Context context, int resId) {
final BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
final InputStream is = context.getResources().openRawResource(resId);
final Bitmap bitmap = BitmapFactory.decodeStream(is, null, opt);
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
/**
*
* @param filename
* @return
*/
public static Bitmap readBitmap565FromFile(String filename) {
Bitmap bitmap = null;
File file = new File(filename);
if (file.exists()) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inPurgeable = true;
options.inInputShareable = true;
try {
bitmap = BitmapFactory.decodeFile(filename, options);
if (bitmap == null) {
file.delete();
}
} catch (OutOfMemoryError e) {
e.printStackTrace();
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
System.gc();
}
}
return bitmap;
}
/**
* 读取本地drawable中较大的资源图片
*
* @param context
* @param resId
* @return
*/
public static Bitmap readDrawableBigBitmap(Context context, int resId) {
final InputStream is = context.getResources().openRawResource(resId);
// final BitmapFactory.Options options=new BitmapFactory.Options();
// options.inJustDecodeBounds = true;
// // 如果该
// 值设为true那么将不返回实际的bitmap,也不给其分配内存空间这样就避免内存溢出了。但是允许我们查询图片的信息这其中就包括图片大小信息(
// // options.outHeight (图片原始高度)和option.outWidth(图片原始宽度))。
// BitmapFactory.decodeStream(is, null, options);
// options.inSampleSize = computeSampleSize(options, -1, 256*256);
// options.inJustDecodeBounds = false;
// return BitmapFactory.decodeStream(is, null, options);
final Bitmap bitmap = getBitmapFromStream(is, 256, 256);
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
//从文件得到BitMap
public static Bitmap getBitmapFromFile(String path, int width, int height) {
if (!TextUtils.isEmpty(path)) {
final File file = new File(path);
return getBitmapFromFile(file, width, height);
}
return null;
}
//从文件得到BitMap
public static Bitmap getBitmapFromFile(File dst, int width, int height) {
if (null != dst && dst.exists()) {
BitmapFactory.Options opts = null;
if (width > 0 && height > 0) {
opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(dst.getPath(), opts);
// 计算图片缩放比例
final int minSideLength = Math.min(width, height);
opts.inSampleSize = computeSampleSize(opts, minSideLength,
width * height);
opts.inPreferredConfig = Bitmap.Config.RGB_565;
opts.inJustDecodeBounds = false;
opts.inInputShareable = true;
opts.inPurgeable = true;
}
try {
return BitmapFactory.decodeFile(dst.getPath(), opts);
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.gc();
}
}
return null;
}
/**
* 从数组得到Bitmap
* @param data
* @param width
* @param height
* @return
*/
public static Bitmap getBitmapByteArray(byte[] data, int width, int height) {
BitmapFactory.Options opts = null;
if (width > 0 && height > 0) {
opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, opts);
// 计算图片缩放比例
final int minSideLength = Math.min(width, height);
opts.inSampleSize = computeSampleSize(opts, minSideLength, width
* height);
opts.inJustDecodeBounds = false;
opts.inInputShareable = true;
// 使得内存可以被回收
opts.inPurgeable = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
}
try {
return BitmapFactory.decodeByteArray(data, 0, data.length, opts);
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.gc();
}
return null;
}
/**
* 从流中得到Bitmap
* @param is
* @param width
* @param height
* @return
*/
public static Bitmap getBitmapFromStream(InputStream is, int width,
int height) {
BitmapFactory.Options opts = null;
if (width > 0 && height > 0) {
opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, opts);
// 计算图片缩放比例
final int minSideLength = Math.min(width, height);
opts.inSampleSize = computeSampleSize(opts, minSideLength, width
* height);
opts.inJustDecodeBounds = false;
opts.inInputShareable = true;
// 使得内存可以被回收
opts.inPurgeable = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
}
try {
return BitmapFactory.decodeStream(is, null, opts);
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.gc();
}
return null;
}
/**
*
* @param options
* @param minSideLength
* @param maxNumOfPixels
* @return
*/
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
final double w = options.outWidth;
final double h = options.outHeight;
final int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
final int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
/**
* 图片透明度处理
*
* @param sourceImg
* 原始图片
* @param number
* 透明度
* @return
*/
public static Bitmap setAlpha(Bitmap sourceImg, int number) {
try {
int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,
sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值
number = number * 255 / 100;
for (int i = 0; i < argb.length; i++) {
if ((argb[i] & 0xff000000) != 0x00000000) {// 透明色不做处理
argb[i] = (number << 24) | (argb[i] & 0xFFFFFF);// 修改最高2位的值
}
}
sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(),
sourceImg.getHeight(), Config.ARGB_8888);
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.gc();
}
return sourceImg;
}
/**
*
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
try {
bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
// canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
} catch (OutOfMemoryError e) {
e.printStackTrace();
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
System.gc();
}
return bitmap;
}
/**
* 获取源图片的BITMAP,压缩,本地图片
*
* @param sImagePath
* @return
*/
public static Bitmap getImgCacheFromLocal2Bitmap(String sImagePath) {
if (!TextUtils.isEmpty(sImagePath)) {
Bitmap bitmap = null;
try {
final File f = new File(sImagePath);
if (!f.exists()) {
return null;
}
final FileInputStream fis = new FileInputStream(f);
// bitmap = BitmapFactory.decodeStream(fis);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 1; // width,hight设为原来的十分一
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeStream(fis, null, options);
fis.close();
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
System.gc();
return null;
} catch (OutOfMemoryError ex) {
ex.printStackTrace();
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
System.gc();
return null;
}
}
return null;
}
/**
*
* @param sImagePath
* @return
*/
public static byte[] getImgCacheFromLocal2Byte(String sImagePath) {
if (!TextUtils.isEmpty(sImagePath)) {
try {
final File f = new File(sImagePath);
if (!f.exists()) {
return null;
}
final FileInputStream fis = new FileInputStream(f);
final int length = fis.available();
final byte[] buffer = new byte[length];
fis.read(buffer);
fis.close();
return buffer;
} catch (Exception ex) {
ex.printStackTrace();
System.gc();
return null;
}
}
return null;
}
/**
* bitmap转byte[]
*
* @param bitmap
* @return
*/
public static byte[] getBitmap2Byte(Bitmap bitmap) {
if (bitmap != null) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
final byte[] data = baos.toByteArray();
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
return null;
}
/**
* 获取缩略图
*
* @param bitmap
* 是否转成缩略图
* @return
*/
public static Bitmap decodeBitmapToThumbnail(Bitmap bitmap) {
return decodeBitmapToThumbnail(bitmap, true);
}
/**
*
* @param bitmap
* @param isThumbnail
* @return
*/
public static Bitmap decodeBitmapToThumbnail(Bitmap bitmap,
boolean isThumbnail) {
if (isThumbnail) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
final float realWidth = options.outWidth;
final float realHeight = options.outHeight;
// 计算缩放比
int scale = (int) ((realHeight > realWidth ? realHeight : realWidth) / 100);
if (scale <= 0) {
scale = 1;
}
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
// 注意这次要把options.inJustDecodeBounds 设为 false,这次图片是要读取出来的。
final byte[] data = AppImageMgr.getBitmap2Byte(bitmap);
if (data != null) {
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
}
}
return bitmap;
}
/**
* 保存图片
*
* @param oldbitmap
* @param sNewImagePath
* @return
*/
public static boolean saveImage(Bitmap oldbitmap, String sNewImagePath) {
try {
final FileOutputStream fileout = new FileOutputStream(sNewImagePath);
oldbitmap.compress(CompressFormat.JPEG, 80, fileout);
fileout.flush();
fileout.close();
return true;
} catch (Exception e) {
e.printStackTrace();
System.gc();
return false;
}
}
/**
*
* @param oldbitmap
* @param sNewImagePath
* @return
*/
public static boolean saveImage(byte[] oldbitmap, String sNewImagePath) {
try {
File file = new File(sNewImagePath);
if (file != null && !file.exists()) {
file.createNewFile();
}
final FileOutputStream fileout = new FileOutputStream(sNewImagePath);
fileout.write(oldbitmap);
fileout.flush();
fileout.close();
return true;
} catch (Exception e) {
e.printStackTrace();
System.gc();
return false;
}
}
/**
*
* @param b
* @return
*/
public static Bitmap bytes2Bimap(byte[] b) {
if (b != null && b.length != 0) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
return bitmap;
} else {
return null;
}
}
/**
* 光晕效果
*
* @param bmp
* @param x
* 光晕中心点在bmp中的x坐标
* @param y
* 光晕中心点在bmp中的y坐标
* @param r
* 光晕的半径
* @return
*/
public static Bitmap grayMasking(Bitmap bmp, int x, int y, float r) {
// 高斯矩阵
int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int delta = 18; // 值越小图片会越亮,越大则越暗
int idx = 0;
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
idx = 0;
int distance = (int) (Math.pow(k - x, 2) + Math.pow(i - y, 2));
// 不是中心区域的点做模糊处理
if (distance > r * r) {
for (int m = -1; m <= 1; m++) {
for (int n = -1; n <= 1; n++) {
pixColor = pixels[(i + m) * width + k + n];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = newR + (int) (pixR * gauss[idx]);
newG = newG + (int) (pixG * gauss[idx]);
newB = newB + (int) (pixB * gauss[idx]);
idx++;
}
}
newR /= delta;
newG /= delta;
newB /= delta;
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
pixels[i * width + k] = Color.argb(255, newR, newG, newB);
newR = 0;
newG = 0;
newB = 0;
}
}
}
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 获取bitmap的字节大小
* @param bitmap
* @return
*/
public static int getBitmapSize(Bitmap bitmap) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ //API 19
// return bitmap.getAllocationByteCount();
// }
//
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1){//API 12
return bitmap.getByteCount();
}
return bitmap.getRowBytes() * bitmap.getHeight(); //earlier version
}
public static byte[] getBytes(String in) {
byte[] result = new byte[in.length() * 2];
int output = 0;
for (char ch : in.toCharArray()) {
result[output++] = (byte) (ch & 0xFF);
result[output++] = (byte) (ch >> 8);
}
return result;
}
public static boolean isSameKey(byte[] key, byte[] buffer) {
int n = key.length;
if (buffer.length < n) {
return false;
}
for (int i = 0; i < n; ++i) {
if (key[i] != buffer[i]) {
return false;
}
}
return true;
}
public static byte[] copyOfRange(byte[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
byte[] copy = new byte[newLength];
System.arraycopy(original, from, copy, 0,Math.min(original.length - from, newLength));
return copy;
}
static {
//参考 http://bioinf.cs.ucl.ac.uk/downloads/crc64/crc64.c
long part;
for (int i = 0; i < 256; i++) {
part = i;
for (int j = 0; j < 8; j++) {
long x = ((int) part & 1) != 0 ? POLY64REV : 0;
part = (part >> 1) ^ x;
}
sCrcTable[i] = part;
}
}
public static byte[] makeKey(String httpUrl) {
return getBytes(httpUrl);
}
/**
* A function thats returns a 64-bit crc for string
*
* @param in input string
* @return a 64-bit crc value
*/
public static final long crc64Long(String in) {
if (in == null || in.length() == 0) {
return 0;
}
return crc64Long(getBytes(in));
}
public static final long crc64Long(byte[] buffer) {
long crc = INITIALCRC;
for (int k = 0, n = buffer.length; k < n; ++k) {
crc = sCrcTable[(((int) crc) ^ buffer[k]) & 0xff] ^ (crc >> 8);
}
return crc;
}
/**
* 将彩色图转换为黑白图
*
* @return 返回转换好的位图
*/
public static Bitmap convertToBlackWhite(Bitmap bmp) {
int width = bmp.getWidth(); // 获取位图的宽
int height = bmp.getHeight(); // 获取位图的高
int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
return newBmp;
}
/**
* 转换成圆角
*
* @param bmp
* @param roundPx
* @return
*/
public static Bitmap convertToRoundedCorner(Bitmap bmp, float roundPx) {
Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
Config.ARGB_8888);
// 得到画布
Canvas canvas = new Canvas(newBmp);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
final RectF rectF = new RectF(rect);
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(bmp, rect, rect, paint);
return newBmp;
}
/** 水平方向模糊度 */
private static float hRadius = 2;
/** 竖直方向模糊度 */
private static float vRadius = 2;
/** 模糊迭代度 */
private static int iterations = 7;
/**
* 高斯模糊
*/
public static Bitmap BoxBlurFilter(Bitmap bmp) {
long start = System.currentTimeMillis();
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] inPixels = new int[width * height];
int[] outPixels = new int[width * height];
Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
bmp.getPixels(inPixels, 0, width, 0, 0, width, height);
for (int i = 0; i < iterations; i++) {
blur(inPixels, outPixels, width, height, hRadius);
blur(outPixels, inPixels, height, width, vRadius);
}
blurFractional(inPixels, outPixels, width, height, hRadius);
blurFractional(outPixels, inPixels, height, width, vRadius);
bitmap.setPixels(inPixels, 0, width, 0, 0, width, height);
long end = System.currentTimeMillis();
return bitmap;
}
public static void blur(int[] in, int[] out, int width, int height,
float radius) {
int widthMinus1 = width - 1;
int r = (int) radius;
int tableSize = 2 * r + 1;
int divide[] = new int[256 * tableSize];
for (int i = 0; i < 256 * tableSize; i++)
divide[i] = i / tableSize;
int inIndex = 0;
for (int y = 0; y < height; y++) {
int outIndex = y;
int ta = 0, tr = 0, tg = 0, tb = 0;
for (int i = -r; i <= r; i++) {
int rgb = in[inIndex + clamp(i, 0, width - 1)];
ta += (rgb >> 24) & 0xff;
tr += (rgb >> 16) & 0xff;
tg += (rgb >> 8) & 0xff;
tb += rgb & 0xff;
}
for (int x = 0; x < width; x++) {
out[outIndex] = (divide[ta] << 24) | (divide[tr] << 16)
| (divide[tg] << 8) | divide[tb];
int i1 = x + r + 1;
if (i1 > widthMinus1)
i1 = widthMinus1;
int i2 = x - r;
if (i2 < 0)
i2 = 0;
int rgb1 = in[inIndex + i1];
int rgb2 = in[inIndex + i2];
ta += ((rgb1 >> 24) & 0xff) - ((rgb2 >> 24) & 0xff);
tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16;
tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8;
tb += (rgb1 & 0xff) - (rgb2 & 0xff);
outIndex += height;
}
inIndex += width;
}
}
private static void blurFractional(int[] in, int[] out, int width,
int height, float radius) {
radius -= (int) radius;
float f = 1.0f / (1 + 2 * radius);
int inIndex = 0;
for (int y = 0; y < height; y++) {
int outIndex = y;
out[outIndex] = in[0];
outIndex += height;
for (int x = 1; x < width - 1; x++) {
int i = inIndex + x;
int rgb1 = in[i - 1];
int rgb2 = in[i];
int rgb3 = in[i + 1];
int a1 = (rgb1 >> 24) & 0xff;
int r1 = (rgb1 >> 16) & 0xff;
int g1 = (rgb1 >> 8) & 0xff;
int b1 = rgb1 & 0xff;
int a2 = (rgb2 >> 24) & 0xff;
int r2 = (rgb2 >> 16) & 0xff;
int g2 = (rgb2 >> 8) & 0xff;
int b2 = rgb2 & 0xff;
int a3 = (rgb3 >> 24) & 0xff;
int r3 = (rgb3 >> 16) & 0xff;
int g3 = (rgb3 >> 8) & 0xff;
int b3 = rgb3 & 0xff;
a1 = a2 + (int) ((a1 + a3) * radius);
r1 = r2 + (int) ((r1 + r3) * radius);
g1 = g2 + (int) ((g1 + g3) * radius);
b1 = b2 + (int) ((b1 + b3) * radius);
a1 *= f;
r1 *= f;
g1 *= f;
b1 *= f;
out[outIndex] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1;
outIndex += height;
}
out[outIndex] = in[width - 1];
inIndex += width;
}
}
public static int clamp(int x, int a, int b) {
return (x < a) ? a : (x > b) ? b : x;
}
/**
* 圆形图片
*
* @param bitmap
* @return