pull/1391/head
gedoor 3 years ago
parent 7eae45460b
commit abc043f38f
  1. 2
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  2. 2
      app/src/main/java/io/legado/app/help/ThemeConfig.kt
  3. 33
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt
  4. 2
      app/src/main/java/io/legado/app/utils/QRCodeUtils.kt

@ -92,7 +92,7 @@ object ReadBookConfig {
val height = dm.heightPixels
bg = durConfig.curBgDrawable(width, height).apply {
if (this is BitmapDrawable) {
bgMeanColor = BitmapUtils.getMeanColor(bitmap)
bgMeanColor = bitmap.getMeanColor()
} else if (this is ColorDrawable) {
bgMeanColor = color
}

@ -63,7 +63,7 @@ object ThemeConfig {
if (bgCfg.second == 0) {
return bgImage
}
return BitmapUtils.stackBlur(bgImage, bgCfg.second.toFloat())
return bgImage.stackBlur(bgCfg.second.toFloat())
}
fun upConfig() {

@ -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(
@ -277,16 +277,19 @@ object BitmapUtils {
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()
)
@ -304,5 +307,3 @@ object BitmapUtils {
averagePixelBlue + 3
)
}
}

@ -182,7 +182,7 @@ object QRCodeUtils {
hints: Map<DecodeHintType?, Any?> = DecodeFormatManager.ALL_HINTS
): Result? {
if (bitmap.width > reqWidth || bitmap.height > reqHeight) {
val bm = BitmapUtils.changeBitmapSize(bitmap, reqWidth, reqHeight)
val bm = bitmap.changeSize(reqWidth, reqHeight)
return parseCodeResult(getRGBLuminanceSource(bm), hints)
}
return parseCodeResult(getRGBLuminanceSource(bitmap), hints)

Loading…
Cancel
Save