10
10
import android .graphics .BitmapShader ;
11
11
import android .graphics .Canvas ;
12
12
import android .graphics .Color ;
13
+ import android .graphics .ColorMatrix ;
14
+ import android .graphics .ColorMatrixColorFilter ;
13
15
import android .graphics .LinearGradient ;
14
16
import android .graphics .Matrix ;
15
17
import android .graphics .Paint ;
@@ -189,7 +191,7 @@ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWi
189
191
* @return 缩放后的图片
190
192
*/
191
193
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 );
193
195
}
194
196
195
197
/**
@@ -237,6 +239,7 @@ public static int getRotateDegree(String path) {
237
239
ExifInterface .TAG_ORIENTATION ,
238
240
ExifInterface .ORIENTATION_NORMAL );
239
241
switch (orientation ) {
242
+ default :
240
243
case ExifInterface .ORIENTATION_ROTATE_90 :
241
244
degree = 90 ;
242
245
break ;
@@ -246,8 +249,6 @@ public static int getRotateDegree(String path) {
246
249
case ExifInterface .ORIENTATION_ROTATE_270 :
247
250
degree = 270 ;
248
251
break ;
249
- default :
250
- degree = 0 ;
251
252
}
252
253
} catch (IOException e ) {
253
254
e .printStackTrace ();
@@ -710,6 +711,38 @@ private Bitmap compress(Bitmap src, CompressFormat format, long topLimit, ConstU
710
711
return BitmapFactory .decodeStream (new ByteArrayInputStream (os .toByteArray ()));
711
712
}
712
713
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
+
713
746
/**
714
747
* 保存图片
715
748
*
@@ -731,22 +764,50 @@ public static boolean save(Bitmap src, String filePath, CompressFormat format) {
731
764
* @return {@code true}: 成功<br>{@code false}: 失败
732
765
*/
733
766
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 ) {
734
793
if (isEmptyBitmap (src ) || !FileUtils .createOrExistsFile (file )) return false ;
735
794
System .out .println (src .getWidth () + ", " + src .getHeight ());
736
795
OutputStream os = null ;
796
+ boolean res = false ;
737
797
try {
738
798
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 ) {
741
802
e .printStackTrace ();
742
- return false ;
743
803
} finally {
744
804
FileUtils .closeIO (os );
745
805
}
806
+ return res ;
746
807
}
747
808
748
809
/**
749
- * 判断文件是否为图片
810
+ * 根据文件名判断文件是否为图片
750
811
*
751
812
* @param file 文件
752
813
*/
@@ -755,14 +816,15 @@ public static boolean isImage(File file) {
755
816
}
756
817
757
818
/**
758
- * 判断文件是否为图片
819
+ * 根据文件名判断文件是否为图片
759
820
*
760
821
* @param filePath 文件路径
761
822
*/
762
823
public static boolean isImage (String filePath ) {
763
824
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" );
766
828
}
767
829
768
830
/**
@@ -779,10 +841,10 @@ public static String getImageType(String filePath) {
779
841
* 获取图片类型
780
842
*
781
843
* @param file 文件
782
- * @return 文件类型
844
+ * @return 图片类型
783
845
*/
784
846
public static String getImageType (File file ) {
785
- if (file == null || ! file . exists () ) return null ;
847
+ if (file == null ) return null ;
786
848
InputStream is = null ;
787
849
try {
788
850
is = new FileInputStream (file );
@@ -795,31 +857,34 @@ public static String getImageType(File file) {
795
857
}
796
858
}
797
859
860
+ /**
861
+ * 流获取图片类型
862
+ *
863
+ * @param is 图片输入流
864
+ * @return 图片类型
865
+ */
798
866
public static String getImageType (InputStream is ) {
799
867
if (is == null ) return null ;
800
868
try {
801
869
byte [] bytes = new byte [8 ];
802
- is .read (bytes );
803
- return getImageType (bytes );
870
+ return is .read (bytes ) != -1 ? getImageType (bytes ) : null ;
804
871
} catch (IOException e ) {
805
872
e .printStackTrace ();
806
873
return null ;
807
874
}
808
875
}
809
876
877
+ /**
878
+ * 获取图片类型
879
+ *
880
+ * @param bytes bitmap的前8字节
881
+ * @return 图片类型
882
+ */
810
883
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" ;
823
888
return null ;
824
889
}
825
890
@@ -848,7 +913,6 @@ private static boolean isBMP(byte[] b) {
848
913
&& (b [0 ] == 0x42 ) && (b [1 ] == 0x4d );
849
914
}
850
915
851
-
852
916
/**
853
917
* 判断bitmap对象是否为空
854
918
*
0 commit comments