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. 157
      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,89 +220,90 @@ 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
var scaleHeight = newHeight.toFloat() / height var scaleHeight = newHeight.toFloat() / height
if (scaleWidth > scaleHeight) { if (scaleWidth > scaleHeight) {
scaleWidth = scaleHeight scaleWidth = scaleHeight
} else { } else {
scaleHeight = scaleWidth scaleHeight = scaleWidth
} }
//获取想要缩放的matrix //获取想要缩放的matrix
val matrix = Matrix() val matrix = Matrix()
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 = this.copy(Config.ARGB_8888, true)
val blurredBitmap = srcBitmap.copy(Config.ARGB_8888, true)
//分配用于渲染脚本的内存
val input = Allocation.createFromBitmap(
rs,
blurredBitmap,
Allocation.MipmapControl.MIPMAP_FULL,
Allocation.USAGE_SHARED
)
val output = Allocation.createTyped(rs, input.type)
//加载我们想要使用的特定脚本的实例。
val script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
script.setInput(input)
//设置模糊半径
script.setRadius(radius)
//启动 ScriptIntrinsicBlur
script.forEach(output)
//将输出复制到模糊的位图
output.copyTo(blurredBitmap)
return blurredBitmap
}
fun getMeanColor(bitmap: Bitmap): Int { //分配用于渲染脚本的内存
val width: Int = bitmap.width val input = Allocation.createFromBitmap(
val height: Int = bitmap.height rs,
var pixel: Int blurredBitmap,
var pixelSumRed = 0 Allocation.MipmapControl.MIPMAP_FULL,
var pixelSumBlue = 0 Allocation.USAGE_SHARED
var pixelSumGreen = 0 )
for (i in 0..99) { val output = Allocation.createTyped(rs, input.type)
for (j in 70..99) {
pixel = bitmap.getPixel( //加载我们想要使用的特定脚本的实例。
(i * width / 100.toFloat()).roundToInt(), val script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
(j * height / 100.toFloat()).roundToInt() script.setInput(input)
)
pixelSumRed += Color.red(pixel) //设置模糊半径
pixelSumGreen += Color.green(pixel) script.setRadius(radius)
pixelSumBlue += Color.blue(pixel)
} //启动 ScriptIntrinsicBlur
script.forEach(output)
//将输出复制到模糊的位图
output.copyTo(blurredBitmap)
return blurredBitmap
}
/**
* 取平均色
*/
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 = this.getPixel(
(i * width / 100.toFloat()).roundToInt(),
(j * height / 100.toFloat()).roundToInt()
)
pixelSumRed += Color.red(pixel)
pixelSumGreen += Color.green(pixel)
pixelSumBlue += Color.blue(pixel)
} }
val averagePixelRed = pixelSumRed / 3000
val averagePixelBlue = pixelSumBlue / 3000
val averagePixelGreen = pixelSumGreen / 3000
return Color.rgb(
averagePixelRed + 3,
averagePixelGreen + 3,
averagePixelBlue + 3
)
} }
val averagePixelRed = pixelSumRed / 3000
val averagePixelBlue = pixelSumBlue / 3000
val averagePixelGreen = pixelSumGreen / 3000
return Color.rgb(
averagePixelRed + 3,
averagePixelGreen + 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