CutUtil.java
4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.hjx.parent.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Base64;
import android.view.View;
import android.view.ViewTreeObserver;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.hjx.parent.function.Function1;
import com.prws.common.utils.CommonUtil;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import top.zibin.luban.Luban;
public class CutUtil {
public static float[] measureBitmap(View view, Bitmap bitmap) {
float realW;
float realH;
float offsetX;
float offsetY;
float vp = 1f * view.getWidth() / view.getHeight();
float bp = 1f * bitmap.getWidth() / bitmap.getHeight();
if (vp > bp) {
realW = view.getHeight() * bp;
realH = (float) view.getHeight();
offsetY = 0f;
offsetX = (view.getWidth() - realW) * 0.5f;
} else if (vp < bp) {
realW = (float) view.getWidth();
realH = view.getWidth() * (1f / bp);
offsetX = 0f;
offsetY = (view.getHeight() - realH) * 0.5f;
} else {
realW = (float) view.getWidth();
realH = (float) view.getHeight();
offsetX = 0f;
offsetY = 0f;
}
return new float[]{realW, realH, offsetX, offsetY};
}
public static Bitmap cut(Bitmap source, float x, float y, int w, int h, Context context) {
// 获取 Glide 的 BitmapPool
BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
// 从 BitmapPool 中获取可复用的 Bitmap
Bitmap result = bitmapPool.get(w, h, source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888);
// 填充白色背景
result.eraseColor(Color.WHITE);
// 创建画布并绘制
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
canvas.drawBitmap(source, -x, -y, paint);
return result;
}
public static void onLayoutReady(final View view, final Function1<View> action) {
view.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
action.invoke(view);
}
}
);
}
public static byte[] bitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
return output.toByteArray();
}
public static String bitmapToBase64(Bitmap bitmap, Boolean usePrefix) {
String base64 = Base64.encodeToString(bitmapToByteArray(bitmap), Base64.NO_WRAP);
if (usePrefix) return "data:image/jpeg;base64," + base64;
else return base64;
}
public static List<File> compressAllSync(Context context, List<String> originList, int ignore) throws IOException {
return Luban.with(context).ignoreBy(ignore)
.setTargetDir(context.getExternalCacheDir().getAbsolutePath())
.load(originList)
.get();
}
public static String saveBitmapToCache(Bitmap bitmap, Context context) {
String fileName = System.currentTimeMillis() + CommonUtil.getStr() + ".jpg";
String path = context.getExternalCacheDir().getPath() + "/images/" + fileName;
try {
File file = new File(path);
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
boolean success = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
if (success) {
return path;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}