Skip to content

Commit ad230c4

Browse files
committed
see 09/10 log
1 parent 7790f13 commit ad230c4

File tree

9 files changed

+297
-162
lines changed

9 files changed

+297
-162
lines changed

utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121

2222
import static com.blankj.utilcode.utils.ConstUtils.*;
23-
import static com.blankj.utilcode.utils.ConstUtils.KB;
2423

2524
/**
2625
* <pre>
@@ -434,11 +433,20 @@ public static List<File> listFilesInDir(File dir, boolean isRecursive) {
434433
if (isRecursive) return listFilesInDir(dir);
435434
if (dir == null || !isDir(dir)) return null;
436435
List<File> list = new ArrayList<>();
437-
File[] files = dir.listFiles();
438-
Collections.addAll(list, files);
436+
Collections.addAll(list, dir.listFiles());
439437
return list;
440438
}
441439

440+
/**
441+
* 获取目录下所有文件包括子目录
442+
*
443+
* @param dirPath 目录路径
444+
* @return 文件链表
445+
*/
446+
public static List<File> listFilesInDir(String dirPath) {
447+
return listFilesInDir(getFileByPath(dirPath));
448+
}
449+
442450
/**
443451
* 获取目录下所有文件包括子目录
444452
*
@@ -463,6 +471,7 @@ public static List<File> listFilesInDir(File dir) {
463471
* <p>大小写忽略</p>
464472
*
465473
* @param dirPath 目录路径
474+
* @param suffix 后缀名
466475
* @param isRecursive 是否递归进子目录
467476
* @return 文件链表
468477
*/
@@ -475,6 +484,7 @@ public static List<File> listFilesInDirWithFilter(String dirPath, String suffix,
475484
* <p>大小写忽略</p>
476485
*
477486
* @param dir 目录
487+
* @param suffix 后缀名
478488
* @param isRecursive 是否递归进子目录
479489
* @return 文件链表
480490
*/
@@ -496,6 +506,7 @@ public static List<File> listFilesInDirWithFilter(File dir, String suffix, boole
496506
* <p>大小写忽略</p>
497507
*
498508
* @param dirPath 目录路径
509+
* @param suffix 后缀名
499510
* @return 文件链表
500511
*/
501512
public static List<File> listFilesInDirWithFilter(String dirPath, String suffix) {
@@ -506,7 +517,8 @@ public static List<File> listFilesInDirWithFilter(String dirPath, String suffix)
506517
* 获取目录下所有后缀名为suffix的文件包括子目录
507518
* <p>大小写忽略</p>
508519
*
509-
* @param dir 目录
520+
* @param dir 目录
521+
* @param suffix 后缀名
510522
* @return 文件链表
511523
*/
512524
public static List<File> listFilesInDirWithFilter(File dir, String suffix) {
@@ -528,6 +540,7 @@ public static List<File> listFilesInDirWithFilter(File dir, String suffix) {
528540
* 获取目录下所有符合filter的文件
529541
*
530542
* @param dirPath 目录路径
543+
* @param filter 过滤器
531544
* @param isRecursive 是否递归进子目录
532545
* @return 文件链表
533546
*/
@@ -539,6 +552,7 @@ public static List<File> listFilesInDirWithFilter(String dirPath, FilenameFilter
539552
* 获取目录下所有符合filter的文件
540553
*
541554
* @param dir 目录
555+
* @param filter 过滤器
542556
* @param isRecursive 是否递归进子目录
543557
* @return 文件链表
544558
*/
@@ -559,6 +573,7 @@ public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filte
559573
* 获取目录下所有符合filter的文件包括子目录
560574
*
561575
* @param dirPath 目录路径
576+
* @param filter 过滤器
562577
* @return 文件链表
563578
*/
564579
public static List<File> listFilesInDirWithFilter(String dirPath, FilenameFilter filter) {
@@ -568,7 +583,8 @@ public static List<File> listFilesInDirWithFilter(String dirPath, FilenameFilter
568583
/**
569584
* 获取目录下所有符合filter的文件包括子目录
570585
*
571-
* @param dir 目录
586+
* @param dir 目录
587+
* @param filter 过滤器
572588
* @return 文件链表
573589
*/
574590
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter) {
@@ -590,7 +606,8 @@ public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filte
590606
* 获取目录下指定文件名的文件包括子目录
591607
* <p>大小写忽略</p>
592608
*
593-
* @param dirPath 目录路径
609+
* @param dirPath 目录路径
610+
* @param fileName 文件名
594611
* @return 文件链表
595612
*/
596613
public static List<File> searchFileInDir(String dirPath, String fileName) {
@@ -601,7 +618,8 @@ public static List<File> searchFileInDir(String dirPath, String fileName) {
601618
* 获取目录下指定文件名的文件包括子目录
602619
* <p>大小写忽略</p>
603620
*
604-
* @param dir 目录
621+
* @param dir 目录
622+
* @param fileName 文件名
605623
* @return 文件链表
606624
*/
607625
public static List<File> searchFileInDir(File dir, String fileName) {
@@ -914,12 +932,12 @@ public static byte[] readFile2Bytes(File file) {
914932
* 字节数转以unit为单位的size
915933
*
916934
* @param byteNum 大小
917-
* @param unit <ul>
918-
* <li>{@link MemoryUnit#BYTE}: 字节</li>
919-
* <li>{@link MemoryUnit#KB} : 千字节</li>
920-
* <li>{@link MemoryUnit#MB} : 兆</li>
921-
* <li>{@link MemoryUnit#GB} : GB</li>
922-
* </ul>
935+
* @param unit <ul>
936+
* <li>{@link MemoryUnit#BYTE}: 字节</li>
937+
* <li>{@link MemoryUnit#KB} : 千字节</li>
938+
* <li>{@link MemoryUnit#MB} : 兆</li>
939+
* <li>{@link MemoryUnit#GB} : GB</li>
940+
* </ul>
923941
* @return 以unit为单位的size
924942
*/
925943
public static double byte2Size(long byteNum, MemoryUnit unit) {

utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import android.graphics.BitmapShader;
1111
import android.graphics.Canvas;
1212
import android.graphics.Color;
13+
import android.graphics.ColorMatrix;
14+
import android.graphics.ColorMatrixColorFilter;
1315
import android.graphics.LinearGradient;
1416
import android.graphics.Matrix;
1517
import android.graphics.Paint;
@@ -189,7 +191,7 @@ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWi
189191
* @return 缩放后的图片
190192
*/
191193
public static Bitmap scaleImage(Bitmap src, int newWidth, int newHeight) {
192-
return scaleImage(src, (float) newWidth / src.getWidth(), (float) newHeight / src.getHeight());
194+
return Bitmap.createScaledBitmap(src, newWidth, newHeight, true);
193195
}
194196

195197
/**
@@ -237,6 +239,7 @@ public static int getRotateDegree(String path) {
237239
ExifInterface.TAG_ORIENTATION,
238240
ExifInterface.ORIENTATION_NORMAL);
239241
switch (orientation) {
242+
default:
240243
case ExifInterface.ORIENTATION_ROTATE_90:
241244
degree = 90;
242245
break;
@@ -246,8 +249,6 @@ public static int getRotateDegree(String path) {
246249
case ExifInterface.ORIENTATION_ROTATE_270:
247250
degree = 270;
248251
break;
249-
default:
250-
degree = 0;
251252
}
252253
} catch (IOException e) {
253254
e.printStackTrace();
@@ -710,6 +711,38 @@ private Bitmap compress(Bitmap src, CompressFormat format, long topLimit, ConstU
710711
return BitmapFactory.decodeStream(new ByteArrayInputStream(os.toByteArray()));
711712
}
712713

714+
/**
715+
* 转为灰度图像
716+
*
717+
* @param src 源图片
718+
* @return 灰度图
719+
*/
720+
public static Bitmap toGray(Bitmap src) {
721+
return toGray(src, false);
722+
}
723+
724+
/**
725+
* 转为灰度图像
726+
*
727+
* @param src 源图片
728+
* @param recycle 是否回收
729+
* @return 灰度图
730+
*/
731+
public static Bitmap toGray(Bitmap src, boolean recycle) {
732+
if (isEmptyBitmap(src)) return null;
733+
Bitmap grayBitmap = Bitmap.createBitmap(src.getWidth(),
734+
src.getHeight(), Bitmap.Config.RGB_565);
735+
Canvas canvas = new Canvas(grayBitmap);
736+
Paint paint = new Paint();
737+
ColorMatrix colorMatrix = new ColorMatrix();
738+
colorMatrix.setSaturation(0);
739+
ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
740+
paint.setColorFilter(colorMatrixColorFilter);
741+
canvas.drawBitmap(src, 0, 0, paint);
742+
if (recycle && !src.isRecycled()) src.recycle();
743+
return grayBitmap;
744+
}
745+
713746
/**
714747
* 保存图片
715748
*
@@ -731,22 +764,50 @@ public static boolean save(Bitmap src, String filePath, CompressFormat format) {
731764
* @return {@code true}: 成功<br>{@code false}: 失败
732765
*/
733766
public static boolean save(Bitmap src, File file, CompressFormat format) {
767+
return save(src, file, format, false);
768+
}
769+
770+
/**
771+
* 保存图片
772+
*
773+
* @param src 源图片
774+
* @param filePath 要保存到的文件路径
775+
* @param format 格式
776+
* @param recycle 是否回收
777+
* @return {@code true}: 成功<br>{@code false}: 失败
778+
*/
779+
public static boolean save(Bitmap src, String filePath, CompressFormat format, boolean recycle) {
780+
return save(src, FileUtils.getFileByPath(filePath), format, recycle);
781+
}
782+
783+
/**
784+
* 保存图片
785+
*
786+
* @param src 源图片
787+
* @param file 要保存到的文件
788+
* @param format 格式
789+
* @param recycle 是否回收
790+
* @return {@code true}: 成功<br>{@code false}: 失败
791+
*/
792+
public static boolean save(Bitmap src, File file, CompressFormat format, boolean recycle) {
734793
if (isEmptyBitmap(src) || !FileUtils.createOrExistsFile(file)) return false;
735794
System.out.println(src.getWidth() + ", " + src.getHeight());
736795
OutputStream os = null;
796+
boolean res = false;
737797
try {
738798
os = new BufferedOutputStream(new FileOutputStream(file));
739-
return src.compress(format, 100, os);
740-
} catch (Exception e) {
799+
res = src.compress(format, 100, os);
800+
if (recycle && !src.isRecycled()) src.recycle();
801+
} catch (IOException e) {
741802
e.printStackTrace();
742-
return false;
743803
} finally {
744804
FileUtils.closeIO(os);
745805
}
806+
return res;
746807
}
747808

748809
/**
749-
* 判断文件是否为图片
810+
* 根据文件名判断文件是否为图片
750811
*
751812
* @param file  文件
752813
*/
@@ -755,14 +816,15 @@ public static boolean isImage(File file) {
755816
}
756817

757818
/**
758-
* 判断文件是否为图片
819+
* 根据文件名判断文件是否为图片
759820
*
760821
* @param filePath  文件路径
761822
*/
762823
public static boolean isImage(String filePath) {
763824
String path = filePath.toUpperCase();
764-
return path.endsWith(".PNG") || path.endsWith(".JPG") ||
765-
path.endsWith(".JPEG") || path.endsWith(".BMP");
825+
return path.endsWith(".PNG") || path.endsWith(".JPG")
826+
|| path.endsWith(".JPEG") || path.endsWith(".BMP")
827+
|| path.endsWith(".GIF");
766828
}
767829

768830
/**
@@ -779,10 +841,10 @@ public static String getImageType(String filePath) {
779841
* 获取图片类型
780842
*
781843
* @param file 文件
782-
* @return 文件类型
844+
* @return 图片类型
783845
*/
784846
public static String getImageType(File file) {
785-
if (file == null || !file.exists()) return null;
847+
if (file == null) return null;
786848
InputStream is = null;
787849
try {
788850
is = new FileInputStream(file);
@@ -795,31 +857,34 @@ public static String getImageType(File file) {
795857
}
796858
}
797859

860+
/**
861+
* 流获取图片类型
862+
*
863+
* @param is 图片输入流
864+
* @return 图片类型
865+
*/
798866
public static String getImageType(InputStream is) {
799867
if (is == null) return null;
800868
try {
801869
byte[] bytes = new byte[8];
802-
is.read(bytes);
803-
return getImageType(bytes);
870+
return is.read(bytes) != -1 ? getImageType(bytes) : null;
804871
} catch (IOException e) {
805872
e.printStackTrace();
806873
return null;
807874
}
808875
}
809876

877+
/**
878+
* 获取图片类型
879+
*
880+
* @param bytes bitmap的前8字节
881+
* @return 图片类型
882+
*/
810883
public static String getImageType(byte[] bytes) {
811-
if (isJPEG(bytes)) {
812-
return "JPEG";
813-
}
814-
if (isGIF(bytes)) {
815-
return "GIF";
816-
}
817-
if (isPNG(bytes)) {
818-
return "PNG";
819-
}
820-
if (isBMP(bytes)) {
821-
return "BMP";
822-
}
884+
if (isJPEG(bytes)) return "JPEG";
885+
if (isGIF(bytes)) return "GIF";
886+
if (isPNG(bytes)) return "PNG";
887+
if (isBMP(bytes)) return "BMP";
823888
return null;
824889
}
825890

@@ -848,7 +913,6 @@ private static boolean isBMP(byte[] b) {
848913
&& (b[0] == 0x42) && (b[1] == 0x4d);
849914
}
850915

851-
852916
/**
853917
* 判断bitmap对象是否为空
854918
*

0 commit comments

Comments
 (0)