Skip to content

Commit 5bc6e67

Browse files
committed
see 09/19 log
1 parent eefb03f commit 5bc6e67

File tree

6 files changed

+166
-28
lines changed

6 files changed

+166
-28
lines changed

app/src/main/java/com/blankj/androidutilcode/core/image/ImageActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ public void initView(Bundle savedInstanceState, View view) {
6060
int width = src.getWidth();
6161
int height = src.getHeight();
6262

63-
mList.add(new ImageBean(R.string.image_compress_by_quality, ImageUtils.compressByQuality(src, 3706L)));
64-
ImageUtils.save(ImageUtils.compressByQuality(src, 3706L), getExternalCacheDir() + "/low.jpg", Bitmap.CompressFormat.JPEG);
65-
ImageUtils.save(ImageUtils.compressByQuality(src, 195752L), getExternalCacheDir() + "/high.jpg", Bitmap.CompressFormat.JPEG);
66-
6763
mList.add(new ImageBean(R.string.image_src, src));
6864
mList.add(new ImageBean(R.string.image_scale, ImageUtils.scale(src, width / 2, height / 2)));
6965
mList.add(new ImageBean(R.string.image_clip, ImageUtils.clip(src, 0, 0, width / 2, height / 2)));
@@ -82,6 +78,10 @@ public void initView(Bundle savedInstanceState, View view) {
8278
mList.add(new ImageBean(R.string.image_fast_blur, ImageUtils.fastBlur(src, 0.1f, 5)));
8379
mList.add(new ImageBean(R.string.image_render_script_blur, ImageUtils.renderScriptBlur(src, 10)));
8480
mList.add(new ImageBean(R.string.image_stack_blur, ImageUtils.stackBlur(src, 10)));
81+
mList.add(new ImageBean(R.string.image_compress_by_scale, ImageUtils.compressByScale(src, 0.5f, 0.5f)));
82+
mList.add(new ImageBean(R.string.image_compress_by_quality_half, ImageUtils.compressByQuality(src, 50)));
83+
mList.add(new ImageBean(R.string.image_compress_by_quality_max_size, ImageUtils.compressByQuality(src, 10L * 1024)));// 10Kb
84+
mList.add(new ImageBean(R.string.image_compress_by_sample_size, ImageUtils.compressBySampleSize(src, 2)));
8585

8686
rvImages.setAdapter(new ImageAdapter(mList, R.layout.item_image));
8787
rvImages.setLayoutManager(new LinearLayoutManager(this));
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout
2+
<LinearLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
android:layout_width="match_parent"
55
android:layout_height="wrap_content"
6-
android:gravity="center_horizontal"
6+
android:orientation="vertical"
77
android:padding="@dimen/spacing_16">
88

99
<Button
@@ -16,7 +16,6 @@
1616
<android.support.v7.widget.RecyclerView
1717
android:id="@+id/rv_images"
1818
android:layout_width="match_parent"
19-
android:layout_height="match_parent"
20-
android:layout_below="@id/btn_save" />
19+
android:layout_height="match_parent" />
2120

22-
</RelativeLayout>
21+
</LinearLayout>

app/src/main/res/core/values/strings.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@
9797
<string name="image_render_script_blur">Render Script Blur</string>
9898
<string name="image_stack_blur">Stack Blur</string>
9999
<string name="image_save">Save</string>
100-
<string name="image_compress_by_quality">Compress By Quality</string>
100+
<string name="image_compress_by_scale">Compress By Scale</string>
101+
<string name="image_compress_by_quality_half">Compress By Quality Half</string>
102+
<string name="image_compress_by_quality_max_size">Compress By Quality Max Size</string>
103+
<string name="image_compress_by_sample_size">Compress By Sample Size</string>
101104

102105

103106
<!--Network相关-->

app/src/main/res/layout/activity_back.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<FrameLayout
3030
android:id="@+id/activity_container"
3131
android:layout_width="match_parent"
32-
android:layout_height="match_parent" />
32+
android:layout_height="match_parent"
33+
android:descendantFocusability="blocksDescendants" />
3334
</android.support.v4.widget.NestedScrollView>
3435
</android.support.design.widget.CoordinatorLayout>

utilcode/src/main/java/com/blankj/utilcode/util/CrashUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static boolean init() {
125125
* @return {@code true}: 初始化成功<br>{@code false}: 初始化失败
126126
*/
127127
public static boolean init(@NonNull final File crashDir) {
128-
return init(crashDir.getAbsolutePath() + FILE_SEP);
128+
return init(crashDir.getAbsolutePath());
129129
}
130130

131131
/**
@@ -139,7 +139,7 @@ public static boolean init(final String crashDir) {
139139
if (isSpace(crashDir)) {
140140
dir = null;
141141
} else {
142-
dir = crashDir.endsWith(FILE_SEP) ? dir : dir + FILE_SEP;
142+
dir = crashDir.endsWith(FILE_SEP) ? crashDir : crashDir + FILE_SEP;
143143
}
144144
if (mInitialized) return true;
145145
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())

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

Lines changed: 150 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.blankj.utilcode.util;
22

33
import android.annotation.TargetApi;
4+
import android.content.res.Resources;
45
import android.graphics.Bitmap;
56
import android.graphics.Bitmap.CompressFormat;
67
import android.graphics.BitmapFactory;
@@ -34,13 +35,11 @@
3435
import android.support.v4.content.ContextCompat;
3536
import android.view.View;
3637

37-
import java.io.BufferedInputStream;
3838
import java.io.BufferedOutputStream;
3939
import java.io.ByteArrayOutputStream;
4040
import java.io.File;
4141
import java.io.FileDescriptor;
4242
import java.io.FileInputStream;
43-
import java.io.FileNotFoundException;
4443
import java.io.FileOutputStream;
4544
import java.io.IOException;
4645
import 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

Comments
 (0)