|
|
|
@ -1,3 +1,5 @@ |
|
|
|
|
@file:Suppress("unused") |
|
|
|
|
|
|
|
|
|
package io.legado.app.utils |
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
@ -14,7 +16,7 @@ import java.io.IOException |
|
|
|
|
import kotlin.math.* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Suppress("unused", "WeakerAccess", "MemberVisibilityCanBePrivate") |
|
|
|
|
@Suppress("WeakerAccess", "MemberVisibilityCanBePrivate") |
|
|
|
|
object BitmapUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -142,7 +144,6 @@ object BitmapUtils { |
|
|
|
|
return BitmapFactory.decodeStream(inputStream, null, op) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//图片不被压缩 |
|
|
|
|
fun convertViewToBitmap(view: View, bitmapWidth: Int, bitmapHeight: Int): Bitmap { |
|
|
|
|
val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888) |
|
|
|
@ -150,7 +151,6 @@ object BitmapUtils { |
|
|
|
|
return bitmap |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param options |
|
|
|
|
* @param minSideLength |
|
|
|
@ -220,10 +220,11 @@ object BitmapUtils { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun changeBitmapSize(bitmap: Bitmap, newWidth: Int, newHeight: Int): Bitmap { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val width = bitmap.width |
|
|
|
|
val height = bitmap.height |
|
|
|
|
fun Bitmap.changeSize(newWidth: Int, newHeight: Int): Bitmap { |
|
|
|
|
val width = this.width |
|
|
|
|
val height = this.height |
|
|
|
|
|
|
|
|
|
//计算压缩的比率 |
|
|
|
|
var scaleWidth = newWidth.toFloat() / width |
|
|
|
@ -240,17 +241,16 @@ object BitmapUtils { |
|
|
|
|
matrix.postScale(scaleWidth, scaleHeight) |
|
|
|
|
|
|
|
|
|
//获取新的bitmap |
|
|
|
|
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true) |
|
|
|
|
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
/** |
|
|
|
|
* 高斯模糊 |
|
|
|
|
*/ |
|
|
|
|
fun stackBlur(srcBitmap: Bitmap?, radius: Float = 8f): Bitmap? { |
|
|
|
|
if (srcBitmap == null) return null |
|
|
|
|
fun Bitmap.stackBlur(radius: Float = 8f): Bitmap? { |
|
|
|
|
val rs = RenderScript.create(appCtx) |
|
|
|
|
val blurredBitmap = srcBitmap.copy(Config.ARGB_8888, true) |
|
|
|
|
val blurredBitmap = this.copy(Config.ARGB_8888, true) |
|
|
|
|
|
|
|
|
|
//分配用于渲染脚本的内存 |
|
|
|
|
val input = Allocation.createFromBitmap( |
|
|
|
@ -275,18 +275,21 @@ object BitmapUtils { |
|
|
|
|
output.copyTo(blurredBitmap) |
|
|
|
|
|
|
|
|
|
return blurredBitmap |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun getMeanColor(bitmap: Bitmap): Int { |
|
|
|
|
val width: Int = bitmap.width |
|
|
|
|
val height: Int = bitmap.height |
|
|
|
|
/** |
|
|
|
|
* 取平均色 |
|
|
|
|
*/ |
|
|
|
|
fun Bitmap.getMeanColor(): Int { |
|
|
|
|
val width: Int = this.width |
|
|
|
|
val height: Int = this.height |
|
|
|
|
var pixel: Int |
|
|
|
|
var pixelSumRed = 0 |
|
|
|
|
var pixelSumBlue = 0 |
|
|
|
|
var pixelSumGreen = 0 |
|
|
|
|
for (i in 0..99) { |
|
|
|
|
for (j in 70..99) { |
|
|
|
|
pixel = bitmap.getPixel( |
|
|
|
|
pixel = this.getPixel( |
|
|
|
|
(i * width / 100.toFloat()).roundToInt(), |
|
|
|
|
(j * height / 100.toFloat()).roundToInt() |
|
|
|
|
) |
|
|
|
@ -303,6 +306,4 @@ object BitmapUtils { |
|
|
|
|
averagePixelGreen + 3, |
|
|
|
|
averagePixelBlue + 3 |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|