PaletteUtils.java
6.67 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.wd.common.imageglide;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.palette.graphics.Palette;
import com.wd.foundation.wdkit.utils.ScreenUtils;
import com.wd.foundation.wdkit.utils.UiUtils;
/**
* @author libo
* @version [V1.0.0, 2023/08/20]
* @since V1.0.0
*/
public class PaletteUtils {
/**
* 获取带背景的图片(同步)
*
* @param originBitmap 源图片
*/
public static Bitmap generateBitmapSync(Bitmap originBitmap) {
if (originBitmap == null) {
return null;
}
Palette palette = Palette.from(originBitmap).generate();
int rgb = palette.getVibrantColor(Color.TRANSPARENT);
return createBgBitmap(rgb);
}
/**
* 生成bitMap
* @param rgb
* @return
*/
public static Bitmap createBgBitmap(int rgb) {
int resultWidth = UiUtils.dp2px(100);
int resultHeight = resultWidth;
Bitmap resultBitmap = Bitmap.createBitmap(resultWidth, resultHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(resultBitmap);
// 绘制背景
Paint paint = new Paint();
paint.setColor(rgb);
canvas.drawRect(new Rect(0, 0, resultWidth, resultHeight), paint);
return resultBitmap;
}
/**
* 获取图片背景颜色(异步)
*
* @param originBitmap 源图片
* @param paletteListener 回调
*/
public static void suckBgColor(Bitmap originBitmap, PaletteListener paletteListener) {
if (originBitmap == null) {
if (paletteListener != null) {
paletteListener.onBgColorBack(Color.TRANSPARENT);
}
return;
}
Palette.from(originBitmap).generate(palette -> {
if (palette != null) {
if (paletteListener != null) {
//获取图片中的主色调
int rgb = palette.getDominantColor(Color.TRANSPARENT);
paletteListener.onBgColorBack(rgb);
}
} else {
if (paletteListener != null) {
paletteListener.onBgColorBack(Color.TRANSPARENT);
}
}
});
//更换取色代码暂时不用,目前UI反馈效果先不动
/* ThreadPoolUtils.postToMain(new Runnable() {
@Override
public void run() {
int bigColor = getBigColor(originBitmap);
String hexColor = Integer.toHexString(rgb);
Log.e("originColor rgbMuted==",hexColor +"");
paletteListener.onBgColorBack(bigColor);
}
});*/
}
/**
* 接口
*/
public interface PaletteListener {
/**
* 返回bitmap
* @param bitmap
*/
void onBitmapBack(Bitmap bitmap);
/**
* 返回颜色
* @param bgcolor 颜色
*/
void onBgColorBack(int bgcolor);
}
/**
* 获取带背景的图片
*
* @param originBitmap 源图片
* @param paletteListener 回调
*/
public static void generateBitmap(Bitmap originBitmap,
PaletteListener paletteListener) {
if (originBitmap == null) {
if (paletteListener != null) {
paletteListener.onBitmapBack(null);
}
return;
}
Palette.from(originBitmap).generate(palette -> {
if (palette != null) {
if (paletteListener != null) {
int rgb = palette.getVibrantColor(Color.TRANSPARENT);
paletteListener.onBitmapBack(createBgBitmap(rgb
, originBitmap));
}
} else {
if (paletteListener != null) {
paletteListener.onBitmapBack(originBitmap);
}
}
});
}
public static Bitmap createBgBitmap(int rgb, Bitmap bitmap) {
int bitmapHeight = bitmap.getHeight();
int bitmapWidth = bitmap.getWidth();
int resultWidth = ScreenUtils.getRealWidth();
int resultHeight = bitmapHeight * resultWidth / bitmapWidth;
Bitmap resultBitmap = Bitmap.createBitmap(resultWidth, resultHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(resultBitmap);
// 绘制背景
Paint paint = new Paint();
paint.setColor(rgb);
canvas.drawRect(new Rect(0, 0, resultWidth, resultHeight), paint);
// 对源图片进行缩放
Matrix matrix = new Matrix();
float scaleRatio = bitmapWidth * 1.0f / resultWidth;
if (scaleRatio > 0.8) {
scaleRatio = (resultWidth - UiUtils.dp2px(100)) * 1.0f / bitmapWidth;
} else if (bitmapWidth < resultWidth - UiUtils.dp2px(300)) {
scaleRatio = (resultWidth - UiUtils.dp2px(300)) * 1.0f / bitmapWidth;
}
matrix.postScale(scaleRatio, scaleRatio);
Bitmap matrixBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix
, true);
canvas.drawBitmap(matrixBitmap, (resultWidth - matrixBitmap.getWidth()) / 2,
(resultHeight - matrixBitmap.getHeight()) / 2, null);
matrixBitmap.recycle();
return resultBitmap;
}
/* private static Bitmap small(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postScale(0.25f, 0.25f);
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizeBmp;
}
public static ArrayList<Integer> getPicturePixel(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 保存所有的像素的数组,图片宽×高
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
ArrayList<Integer> rgb = new ArrayList<>();
for (int i = 0; i < pixels.length; i++) {
int clr = pixels[i];
// 取高两位
int red = (clr & 0x00ff0000) >> 16;
// 取中两位
int green = (clr & 0x0000ff00) >> 8;
// 取低两位
int blue = clr & 0x000000ff;
// Log.d("tag", "r=" + red + ",g=" + green + ",b=" + blue);
int color = Color.rgb(red, green, blue);
//除去白色和黑色
if (color != Color.WHITE && color != Color.BLACK) {
rgb.add(color);
}
}
return rgb;
}*/
}