Skip to content

Commit de6ae50

Browse files
committed
see 09/08 log
1 parent dd65d40 commit de6ae50

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

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

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
import android.renderscript.RenderScript;
2929
import android.renderscript.ScriptIntrinsicBlur;
3030

31+
import java.io.BufferedOutputStream;
3132
import java.io.ByteArrayInputStream;
3233
import java.io.ByteArrayOutputStream;
3334
import java.io.File;
3435
import java.io.FileInputStream;
35-
import java.io.FileNotFoundException;
3636
import java.io.FileOutputStream;
3737
import java.io.IOException;
3838
import java.io.InputStream;
39+
import java.io.OutputStream;
3940

4041
/**
4142
* <pre>
@@ -121,32 +122,25 @@ public static Drawable bytes2Drawable(Resources resources, byte[] bytes) {
121122
/**
122123
* 根据文件路径获取bitmap
123124
*
124-
* @param filePath 文件路径
125+
* @param file 文件路径
125126
* @return bitmap
126127
*/
127-
public static Bitmap getBitmapByFile(String filePath) {
128-
return getBitmapByFile(FileUtils.getFileByPath(filePath));
128+
public static Bitmap getBitmapByFile(File file) {
129+
if (file == null) return null;
130+
return getBitmapByFile(file.getPath());
129131
}
130132

131133
/**
132134
* 根据文件路径获取bitmap
133135
*
134-
* @param file 文件路径
136+
* @param filePath 文件路径
135137
* @return bitmap
136138
*/
137-
public static Bitmap getBitmapByFile(File file) {
138-
if (file == null) return null;
139-
try {
140-
return BitmapFactory.decodeStream(new FileInputStream(file));
141-
} catch (FileNotFoundException e) {
142-
e.printStackTrace();
143-
return null;
144-
}
139+
public static Bitmap getBitmapByFile(String filePath) {
140+
return BitmapFactory.decodeFile(filePath);
145141
}
146142

147143
/**
148-
* 根据文件路径获取bitmap
149-
*
150144
* @param filePath 文件路径
151145
* @return bitmap
152146
*/
@@ -166,24 +160,19 @@ public static int calculateInSampleSize(
166160
final int height = options.outHeight;
167161
final int width = options.outWidth;
168162
int inSampleSize = 1;
169-
170163
if (height > reqHeight || width > reqWidth) {
171-
172164
final int halfHeight = height / 2;
173165
final int halfWidth = width / 2;
174-
175166
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
176167
// height and width larger than the requested height and width.
177168
while ((halfHeight / inSampleSize) > reqHeight
178169
&& (halfWidth / inSampleSize) > reqWidth) {
179-
inSampleSize *= 2;
170+
inSampleSize <<= 1;
180171
}
181172
}
182-
183173
return inSampleSize;
184174
}
185175

186-
187176
/**
188177
* 缩放图片
189178
*
@@ -735,17 +724,17 @@ public static boolean save(Bitmap src, String filePath, CompressFormat format) {
735724
* @return {@code true}: 成功<br>{@code false}: 失败
736725
*/
737726
public static boolean save(Bitmap src, File file, CompressFormat format) {
738-
if (isEmptyBitmap(src) || file == null) return false;
739-
System.out.println(src.getWidth() + "," + src.getHeight());
740-
FileOutputStream fos = null;
727+
if (isEmptyBitmap(src) || !FileUtils.createOrExistsFile(file)) return false;
728+
System.out.println(src.getWidth() + ", " + src.getHeight());
729+
OutputStream os = null;
741730
try {
742-
fos = new FileOutputStream(file);
743-
return src.compress(format, 100, fos);
731+
os = new BufferedOutputStream(new FileOutputStream(file));
732+
return src.compress(format, 100, os);
744733
} catch (Exception e) {
745734
e.printStackTrace();
746735
return false;
747736
} finally {
748-
FileUtils.closeIO(fos);
737+
FileUtils.closeIO(os);
749738
}
750739
}
751740

utilcode/src/test/java/com/blankj/utilcode/utils/ImageUtilsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ public class ImageUtilsTest {
2626

2727
@Test
2828
public void testBitmap2Bytes() throws Exception {
29-
FileUtils.createOrExistsFile(path + "new1.png");
3029
Bitmap bitmap = ImageUtils.getBitmapByFile(path + "lena.png");
31-
System.out.println(ImageUtils.save(bitmap, path + "new1.png", Bitmap.CompressFormat.PNG));
30+
System.out.println(ImageUtils.save(bitmap, path + "new.png", Bitmap.CompressFormat.PNG));
3231
}
3332

3433
@Test

utilcode/src/test/res/image/lena.png

-209 KB
Loading

0 commit comments

Comments
 (0)