-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtils.kt
More file actions
334 lines (318 loc) · 11 KB
/
ImageUtils.kt
File metadata and controls
334 lines (318 loc) · 11 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.handy.basic.utils
import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.LogUtils
import java.io.File
/**
* @title: ImageUtils
* @projectName HandyBasicKT
* @description: 图片相关工具类
* @author LiuJie https://github.com/Handy045
* @date Created in 2019-11-04 16:49
*/
class ImageUtils private constructor() {
companion object {
/**
* 通过代码改变图片颜色
*
* @param bitmap 原图片
* @param tintColor 目标颜色 (16进制)
* @return 改色后的Bitmap
*/
fun tintBitmap(bitmap: Bitmap, tintColor: Int): Bitmap? {
val bitmap1 = Bitmap.createBitmap(bitmap.width, bitmap.height, bitmap.config)
val canvas = Canvas(bitmap1)
val paint = Paint()
paint.colorFilter = PorterDuffColorFilter(tintColor, PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, 0f, 0f, paint)
return bitmap1
}
/**
* 按目标宽高所计算的宽高比压缩图片
*
* @param filePath 原图片路径
* @param targetWidth 目标宽度(建议值:360)
* @param targetHeight 目标高度(建议值:640)
* @param recycle 是否回收
* @return 压缩后的图片
*/
fun compressByScale(
filePath: String,
targetWidth: Int,
targetHeight: Int,
recycle: Boolean
): Bitmap? {
if (filePath.isEmpty()) {
LogUtils.e("图片路径为空")
return null
}
val barcode = BitmapFactory.decodeFile(filePath)
return if (isEmptyBitmap(barcode)) null else compressByScale(
barcode,
targetWidth,
targetHeight,
recycle
)
}
/**
* 按目标宽高所计算的宽高比压缩图片
*
* @param file 原图片文件
* @param targetWidth 目标宽度(建议值:360)
* @param targetHeight 目标高度(建议值:640)
* @param recycle 是否回收
* @return 压缩后的图片
*/
fun compressByScale(
file: File,
targetWidth: Int,
targetHeight: Int,
recycle: Boolean
): Bitmap? {
return compressByScale(file.path, targetWidth, targetHeight, recycle)
}
/**
* 按目标宽高所计算的宽高比压缩图片
*
* @param mBitmap 原图片BitMap
* @param targetWidth 目标宽度(建议值:360)
* @param targetHeight 目标高度(建议值:640)
* @param recycle 是否回收
* @return 压缩后的图片
*/
fun compressByScale(
mBitmap: Bitmap,
targetWidth: Int,
targetHeight: Int,
recycle: Boolean
): Bitmap? {
if (targetWidth == 0 || targetHeight == 0) {
return null
}
//计算图片宽高与制定宽高的最大比例
val scale: Double
var bitmap: Bitmap? = null
if (mBitmap.width < mBitmap.height) {
if (mBitmap.width <= targetWidth || mBitmap.height <= targetHeight) {
scale = 1.0
} else {
val widthScale = mBitmap.width.toDouble() / targetWidth.toDouble()
val heightScale = mBitmap.height.toDouble() / targetHeight.toDouble()
scale = if (widthScale > heightScale) widthScale else heightScale
}
} else {
if (mBitmap.width <= targetHeight || mBitmap.height <= targetWidth) {
scale = 1.0
} else {
val widthScale = mBitmap.width.toDouble() / targetHeight.toDouble()
val heightScale = mBitmap.height.toDouble() / targetWidth.toDouble()
scale = if (widthScale > heightScale) widthScale else heightScale
}
}
// 如果缩放倍数大于1则对Bitmap实例进行压缩,如果小于1则无需压缩
if (scale > 1) {
val imgWidth = (mBitmap.width / scale).toInt()
val imgHeight = (mBitmap.height / scale).toInt()
bitmap = Bitmap.createScaledBitmap(mBitmap, imgWidth, imgHeight, true)
}
return if (bitmap == null) {
mBitmap
} else {
if (recycle && !mBitmap.isRecycled) {
mBitmap.recycle()
}
bitmap
}
}
/**
* 添加文字水印
*
* @param src 源图片
* @param content 水印文本
* @param textSize 水印字体大小
* @param color 水印字体颜色
* @param x 起始坐标x
* @param y 起始坐标y
* @return 带有文字水印的图片
*/
fun addTextWatermark(
src: Bitmap,
content: String,
textSize: Int,
color: Int,
x: Float,
y: Float
): Bitmap? {
return addTextWatermark(src, content, textSize.toFloat(), color, x, y, false)
}
/**
* 添加文字水印
*
* @param src 源图片
* @param content 水印文本
* @param textScale 水印字体比例(字体大小 = 照片高度 / 字体比例)
* @param color 水印字体颜色
* @param x 起始坐标x
* @param y 起始坐标y
* @return 带有文字水印的图片
*/
fun addTextWatermarkScale(
src: Bitmap,
content: String,
textScale: Int,
color: Int,
x: Float,
y: Float
): Bitmap? {
return addTextWatermark(
src,
content,
(src.height / textScale).toFloat(),
color,
x,
y,
false
)
}
/**
* 添加文字水印,支持换行
*
* @param src 源图片
* @param content 水印文本
* @param textSize 水印字体大小
* @param color 水印字体颜色
* @param x 起始坐标x
* @param y 起始坐标y
* @param recycle 是否回收
* @return 带有文字水印的图片
*/
fun addTextWatermark(
src: Bitmap,
content: String,
textSize: Float,
color: Int,
x: Float,
y: Float,
recycle: Boolean
): Bitmap? {
if (isEmptyBitmap(src)) {
return null
}
val ret = src.copy(src.config, true)
val paint = TextPaint(Paint.ANTI_ALIAS_FLAG)
val canvas = Canvas(ret)
paint.color = color
paint.textSize = textSize
val bounds = Rect()
paint.getTextBounds(content, 0, content.length, bounds)
val layout = StaticLayout(
content,
paint,
(ret.width - x).toInt(),
Layout.Alignment.ALIGN_NORMAL,
1.0f,
0.0f,
true
)
canvas.translate(x, y)
layout.draw(canvas)
if (recycle && !src.isRecycled) {
src.recycle()
}
return ret
}
/**
* 添加图片水印
*
* @param src 源图片
* @param watermark 图片水印
* @param x 起始坐标x
* @param y 起始坐标y
* @param alpha 透明度
* @return 带有图片水印的图片
*/
fun addImageWatermark(src: Bitmap, watermark: Bitmap, x: Int, y: Int, alpha: Int): Bitmap? {
return addImageWatermark(src, watermark, x, y, alpha, false)
}
/**
* 添加图片水印
*
* @param src 源图片
* @param watermark 图片水印
* @param x 起始坐标x
* @param y 起始坐标y
* @param alpha 透明度
* @param recycle 是否回收
* @return 带有图片水印的图片
*/
fun addImageWatermark(
src: Bitmap,
watermark: Bitmap,
x: Int,
y: Int,
alpha: Int,
recycle: Boolean
): Bitmap? {
if (isEmptyBitmap(src)) {
return null
}
val ret = src.copy(src.config, true)
if (!isEmptyBitmap(watermark)) {
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
val canvas = Canvas(ret)
paint.alpha = alpha
canvas.drawBitmap(watermark, x.toFloat(), y.toFloat(), paint)
}
if (recycle && !src.isRecycled) {
src.recycle()
}
return ret
}
/**
* 判断bitmap对象是否为空
*
* @param src 源图片
* @return `true`: 是<br></br>`false`: 否
*/
private fun isEmptyBitmap(src: Bitmap?): Boolean {
return src == null || src.width == 0 || src.height == 0
}
/**
* 通过代码改变图片颜色
*
* @param context 上下文
* @param idDrawable 原图片
* @param tintColor 目标颜色 (16进制)
* @return 改色后的Drawable
*/
fun tintDrawable(context: Context, @DrawableRes idDrawable: Int, @ColorRes tintColor: Int): Drawable? {
if (idDrawable == 0) {
return null
}
val drawable = ContextCompat.getDrawable(context, idDrawable)?.mutate() ?: return null
val inBitmap = com.blankj.utilcode.util.ImageUtils.drawable2Bitmap(drawable)
val outBitmap = Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight,
inBitmap.config
)
val canvas = Canvas(outBitmap)
val paint = Paint()
paint.colorFilter =
PorterDuffColorFilter(
ContextCompat.getColor(context, tintColor),
PorterDuff.Mode.SRC_IN
)
canvas.drawBitmap(inBitmap, 0f, 0f, paint)
return BitmapDrawable(null, outBitmap)
}
}
}