1010import android .graphics .BitmapShader ;
1111import android .graphics .Canvas ;
1212import android .graphics .Color ;
13+ import android .graphics .ColorMatrix ;
14+ import android .graphics .ColorMatrixColorFilter ;
1315import android .graphics .LinearGradient ;
1416import android .graphics .Matrix ;
1517import 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