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 val height = dm.heightPixels
bg = durConfig.curBgDrawable(width, height).apply { bg = durConfig.curBgDrawable(width, height).apply {
if (this is BitmapDrawable) { if (this is BitmapDrawable) {
bgMeanColor = BitmapUtils.getMeanColor(bitmap) bgMeanColor = bitmap.getMeanColor()
} else if (this is ColorDrawable) { } else if (this is ColorDrawable) {
bgMeanColor = color bgMeanColor = color
} }

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

@ -1,3 +1,5 @@
@file:Suppress("unused")
package io.legado.app.utils package io.legado.app.utils
import android.content.Context import android.content.Context
@ -14,7 +16,7 @@ import java.io.IOException
import kotlin.math.* import kotlin.math.*
@Suppress("unused", "WeakerAccess", "MemberVisibilityCanBePrivate") @Suppress("WeakerAccess", "MemberVisibilityCanBePrivate")
object BitmapUtils { object BitmapUtils {
/** /**
@ -142,7 +144,6 @@ object BitmapUtils {
return BitmapFactory.decodeStream(inputStream, null, op) return BitmapFactory.decodeStream(inputStream, null, op)
} }
//图片不被压缩 //图片不被压缩
fun convertViewToBitmap(view: View, bitmapWidth: Int, bitmapHeight: Int): Bitmap { fun convertViewToBitmap(view: View, bitmapWidth: Int, bitmapHeight: Int): Bitmap {
val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888) val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888)
@ -150,7 +151,6 @@ object BitmapUtils {
return bitmap return bitmap
} }
/** /**
* @param options * @param options
* @param minSideLength * @param minSideLength
@ -220,10 +220,11 @@ object BitmapUtils {
} }
} }
fun changeBitmapSize(bitmap: Bitmap, newWidth: Int, newHeight: Int): Bitmap { }
val width = bitmap.width fun Bitmap.changeSize(newWidth: Int, newHeight: Int): Bitmap {
val height = bitmap.height val width = this.width
val height = this.height
//计算压缩的比率 //计算压缩的比率
var scaleWidth = newWidth.toFloat() / width var scaleWidth = newWidth.toFloat() / width
@ -240,17 +241,16 @@ object BitmapUtils {
matrix.postScale(scaleWidth, scaleHeight) matrix.postScale(scaleWidth, scaleHeight)
//获取新的bitmap //获取新的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? { fun Bitmap.stackBlur(radius: Float = 8f): Bitmap? {
if (srcBitmap == null) return null
val rs = RenderScript.create(appCtx) 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( val input = Allocation.createFromBitmap(
@ -277,16 +277,19 @@ object BitmapUtils {
return 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 pixel: Int
var pixelSumRed = 0 var pixelSumRed = 0
var pixelSumBlue = 0 var pixelSumBlue = 0
var pixelSumGreen = 0 var pixelSumGreen = 0
for (i in 0..99) { for (i in 0..99) {
for (j in 70..99) { for (j in 70..99) {
pixel = bitmap.getPixel( pixel = this.getPixel(
(i * width / 100.toFloat()).roundToInt(), (i * width / 100.toFloat()).roundToInt(),
(j * height / 100.toFloat()).roundToInt() (j * height / 100.toFloat()).roundToInt()
) )
@ -304,5 +307,3 @@ object BitmapUtils {
averagePixelBlue + 3 averagePixelBlue + 3
) )
} }
}

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

Loading…
Cancel
Save