11package com .blankj .utilcode .util ;
22
33import android .annotation .TargetApi ;
4+ import android .content .res .Resources ;
45import android .graphics .Bitmap ;
56import android .graphics .Bitmap .CompressFormat ;
67import android .graphics .BitmapFactory ;
3435import android .support .v4 .content .ContextCompat ;
3536import android .view .View ;
3637
37- import java .io .BufferedInputStream ;
3838import java .io .BufferedOutputStream ;
3939import java .io .ByteArrayOutputStream ;
4040import java .io .File ;
4141import java .io .FileDescriptor ;
4242import java .io .FileInputStream ;
43- import java .io .FileNotFoundException ;
4443import java .io .FileOutputStream ;
4544import java .io .IOException ;
4645import java .io .InputStream ;
@@ -170,16 +169,25 @@ public static Bitmap view2Bitmap(final View view) {
170169 */
171170 public static Bitmap getBitmap (final File file ) {
172171 if (file == null ) return null ;
173- InputStream is = null ;
174- try {
175- is = new BufferedInputStream (new FileInputStream (file ));
176- return BitmapFactory .decodeStream (is );
177- } catch (FileNotFoundException e ) {
178- e .printStackTrace ();
179- return null ;
180- } finally {
181- CloseUtils .closeIO (is );
182- }
172+ return BitmapFactory .decodeFile (file .getAbsolutePath ());
173+ }
174+
175+ /**
176+ * 获取bitmap
177+ *
178+ * @param file 文件
179+ * @param maxWidth 最大宽度
180+ * @param maxHeight 最大高度
181+ * @return bitmap
182+ */
183+ public static Bitmap getBitmap (final File file , final int maxWidth , final int maxHeight ) {
184+ if (file == null ) return null ;
185+ BitmapFactory .Options options = new BitmapFactory .Options ();
186+ options .inJustDecodeBounds = true ;
187+ BitmapFactory .decodeFile (file .getAbsolutePath (), options );
188+ options .inSampleSize = calculateInSampleSize (options , maxWidth , maxHeight );
189+ options .inJustDecodeBounds = false ;
190+ return BitmapFactory .decodeFile (file .getAbsolutePath (), options );
183191 }
184192
185193 /**
@@ -193,6 +201,24 @@ public static Bitmap getBitmap(final String filePath) {
193201 return BitmapFactory .decodeFile (filePath );
194202 }
195203
204+ /**
205+ * 获取bitmap
206+ *
207+ * @param filePath 文件路径
208+ * @param maxWidth 最大宽度
209+ * @param maxHeight 最大高度
210+ * @return bitmap
211+ */
212+ public static Bitmap getBitmap (final String filePath , final int maxWidth , final int maxHeight ) {
213+ if (isSpace (filePath )) return null ;
214+ BitmapFactory .Options options = new BitmapFactory .Options ();
215+ options .inJustDecodeBounds = true ;
216+ BitmapFactory .decodeFile (filePath , options );
217+ options .inSampleSize = calculateInSampleSize (options , maxWidth , maxHeight );
218+ options .inJustDecodeBounds = false ;
219+ return BitmapFactory .decodeFile (filePath , options );
220+ }
221+
196222 /**
197223 * 获取bitmap
198224 *
@@ -204,6 +230,24 @@ public static Bitmap getBitmap(final InputStream is) {
204230 return BitmapFactory .decodeStream (is );
205231 }
206232
233+ /**
234+ * 获取bitmap
235+ *
236+ * @param is 输入流
237+ * @param maxWidth 最大宽度
238+ * @param maxHeight 最大高度
239+ * @return bitmap
240+ */
241+ public static Bitmap getBitmap (final InputStream is , final int maxWidth , final int maxHeight ) {
242+ if (is == null ) return null ;
243+ BitmapFactory .Options options = new BitmapFactory .Options ();
244+ options .inJustDecodeBounds = true ;
245+ BitmapFactory .decodeStream (is , null , options );
246+ options .inSampleSize = calculateInSampleSize (options , maxWidth , maxHeight );
247+ options .inJustDecodeBounds = false ;
248+ return BitmapFactory .decodeStream (is , null , options );
249+ }
250+
207251 /**
208252 * 获取bitmap
209253 *
@@ -216,6 +260,25 @@ public static Bitmap getBitmap(final byte[] data, final int offset) {
216260 return BitmapFactory .decodeByteArray (data , offset , data .length );
217261 }
218262
263+ /**
264+ * 获取bitmap
265+ *
266+ * @param data 数据
267+ * @param offset 偏移量
268+ * @param maxWidth 最大宽度
269+ * @param maxHeight 最大高度
270+ * @return bitmap
271+ */
272+ public static Bitmap getBitmap (final byte [] data , final int offset , final int maxWidth , final int maxHeight ) {
273+ if (data .length == 0 ) return null ;
274+ BitmapFactory .Options options = new BitmapFactory .Options ();
275+ options .inJustDecodeBounds = true ;
276+ BitmapFactory .decodeByteArray (data , offset , data .length , options );
277+ options .inSampleSize = calculateInSampleSize (options , maxWidth , maxHeight );
278+ options .inJustDecodeBounds = false ;
279+ return BitmapFactory .decodeByteArray (data , offset , data .length , options );
280+ }
281+
219282 /**
220283 * 获取bitmap
221284 *
@@ -232,6 +295,24 @@ public static Bitmap getBitmap(@DrawableRes final int resId) {
232295 return bitmap ;
233296 }
234297
298+ /**
299+ * 获取bitmap
300+ *
301+ * @param resId 资源id
302+ * @param maxWidth 最大宽度
303+ * @param maxHeight 最大高度
304+ * @return bitmap
305+ */
306+ public static Bitmap getBitmap (@ DrawableRes final int resId , final int maxWidth , final int maxHeight ) {
307+ BitmapFactory .Options options = new BitmapFactory .Options ();
308+ final Resources resources = Utils .getApp ().getResources ();
309+ options .inJustDecodeBounds = true ;
310+ BitmapFactory .decodeResource (resources , resId , options );
311+ options .inSampleSize = calculateInSampleSize (options , maxWidth , maxHeight );
312+ options .inJustDecodeBounds = false ;
313+ return BitmapFactory .decodeResource (resources , resId , options );
314+ }
315+
235316 /**
236317 * 获取bitmap
237318 *
@@ -243,6 +324,24 @@ public static Bitmap getBitmap(final FileDescriptor fd) {
243324 return BitmapFactory .decodeFileDescriptor (fd );
244325 }
245326
327+ /**
328+ * 获取bitmap
329+ *
330+ * @param fd 文件描述
331+ * @param maxWidth 最大宽度
332+ * @param maxHeight 最大高度
333+ * @return bitmap
334+ */
335+ public static Bitmap getBitmap (final FileDescriptor fd , final int maxWidth , final int maxHeight ) {
336+ if (fd == null ) return null ;
337+ BitmapFactory .Options options = new BitmapFactory .Options ();
338+ options .inJustDecodeBounds = true ;
339+ BitmapFactory .decodeFileDescriptor (fd , null , options );
340+ options .inSampleSize = calculateInSampleSize (options , maxWidth , maxHeight );
341+ options .inJustDecodeBounds = false ;
342+ return BitmapFactory .decodeFileDescriptor (fd , null , options );
343+ }
344+
246345 /**
247346 * 缩放图片
248347 *
@@ -1592,6 +1691,41 @@ public static Bitmap compressBySampleSize(final Bitmap src, final int sampleSize
15921691 return BitmapFactory .decodeByteArray (bytes , 0 , bytes .length , options );
15931692 }
15941693
1694+ /**
1695+ * 按采样大小压缩
1696+ *
1697+ * @param src 源图片
1698+ * @param maxWidth 最大宽度
1699+ * @param maxHeight 最大高度
1700+ * @return 按采样率压缩后的图片
1701+ */
1702+ public static Bitmap compressBySampleSize (final Bitmap src , final int maxWidth , final int maxHeight ) {
1703+ return compressBySampleSize (src , maxWidth , maxHeight , false );
1704+ }
1705+
1706+ /**
1707+ * 按采样大小压缩
1708+ *
1709+ * @param src 源图片
1710+ * @param maxWidth 最大宽度
1711+ * @param maxHeight 最大高度
1712+ * @param recycle 是否回收
1713+ * @return 按采样率压缩后的图片
1714+ */
1715+ public static Bitmap compressBySampleSize (final Bitmap src , final int maxWidth , final int maxHeight , final boolean recycle ) {
1716+ if (isEmptyBitmap (src )) return null ;
1717+ BitmapFactory .Options options = new BitmapFactory .Options ();
1718+ options .inJustDecodeBounds = true ;
1719+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
1720+ src .compress (Bitmap .CompressFormat .JPEG , 100 , baos );
1721+ byte [] bytes = baos .toByteArray ();
1722+ BitmapFactory .decodeByteArray (bytes , 0 , bytes .length , options );
1723+ options .inSampleSize = calculateInSampleSize (options , maxWidth , maxHeight );
1724+ options .inJustDecodeBounds = false ;
1725+ if (recycle && !src .isRecycled ()) src .recycle ();
1726+ return BitmapFactory .decodeByteArray (bytes , 0 , bytes .length , options );
1727+ }
1728+
15951729 private static File getFileByPath (final String filePath ) {
15961730 return isSpace (filePath ) ? null : new File (filePath );
15971731 }
@@ -1631,12 +1765,13 @@ private static boolean isSpace(final String s) {
16311765 * @param maxHeight 最大高度
16321766 * @return 采样大小
16331767 */
1634- private static int calculateInSampleSize (final BitmapFactory .Options options , final int maxWidth , final int maxHeight ) {
1635- if (maxWidth == 0 || maxHeight == 0 ) return 1 ;
1768+ private static int calculateInSampleSize (final BitmapFactory .Options options ,
1769+ final int maxWidth ,
1770+ final int maxHeight ) {
16361771 int height = options .outHeight ;
16371772 int width = options .outWidth ;
16381773 int inSampleSize = 1 ;
1639- while ((height >>= 1 ) > maxHeight && (width >>= 1 ) > maxWidth ) {
1774+ while ((width >>= 1 ) >= maxWidth && (height >>= 1 ) >= maxHeight ) {
16401775 inSampleSize <<= 1 ;
16411776 }
16421777 return inSampleSize ;
0 commit comments