This commit is contained in:
kunfei
2022-05-24 21:23:12 +08:00
parent 85365fc0b5
commit 826341f2f1
3 changed files with 6 additions and 21 deletions
@@ -568,11 +568,11 @@ object ReadBookConfig {
1 -> { 1 -> {
val path = "bg" + File.separator + curBgStr() val path = "bg" + File.separator + curBgStr()
val bitmap = BitmapUtils.decodeAssetsBitmap(appCtx, path, width, height) val bitmap = BitmapUtils.decodeAssetsBitmap(appCtx, path, width, height)
BitmapDrawable(resources, bitmap?.copyAndRecycle(width, height)) BitmapDrawable(resources, bitmap?.resizeAndRecycle(width, height))
} }
else -> { else -> {
val bitmap = BitmapUtils.decodeBitmap(curBgStr(), width, height) val bitmap = BitmapUtils.decodeBitmap(curBgStr(), width, height)
BitmapDrawable(resources, bitmap?.copyAndRecycle(width, height)) BitmapDrawable(resources, bitmap?.resizeAndRecycle(width, height))
} }
} }
} catch (e: OutOfMemoryError) { } catch (e: OutOfMemoryError) {
@@ -7,7 +7,6 @@ import android.graphics.Bitmap
import android.graphics.Bitmap.Config import android.graphics.Bitmap.Config
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.Color import android.graphics.Color
import android.graphics.Matrix
import com.google.android.renderscript.Toolkit import com.google.android.renderscript.Toolkit
import java.io.FileInputStream import java.io.FileInputStream
import java.io.IOException import java.io.IOException
@@ -213,23 +212,10 @@ object BitmapUtils {
/** /**
* 获取指定宽高的图片 * 获取指定宽高的图片
*/ */
fun Bitmap.copyAndRecycle(newWidth: Int, newHeight: Int): Bitmap { fun Bitmap.resizeAndRecycle(newWidth: Int, newHeight: Int): Bitmap {
val width = this.width
val height = this.height
//计算压缩的比率
val scaleWidth = newWidth.toFloat() / width
val scaleHeight = newHeight.toFloat() / height
//获取想要缩放的matrix
val matrix = Matrix()
matrix.postScale(scaleWidth, scaleHeight)
//获取新的bitmap //获取新的bitmap
val bitmap = Bitmap.createBitmap(this, 0, 0, width, height, matrix, true) val bitmap = Toolkit.resize(this, newWidth, newHeight)
recycle() recycle()
return bitmap return bitmap
} }
@@ -237,8 +223,7 @@ fun Bitmap.copyAndRecycle(newWidth: Int, newHeight: Int): Bitmap {
* 高斯模糊 * 高斯模糊
*/ */
fun Bitmap.stackBlur(radius: Int = 8): Bitmap { fun Bitmap.stackBlur(radius: Int = 8): Bitmap {
val blurredBitmap = this.copy(Config.ARGB_8888, true) return Toolkit.blur(this, radius)
return Toolkit.blur(blurredBitmap, radius)
} }
/** /**
@@ -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 = bitmap.copyAndRecycle(reqWidth, reqHeight) val bm = bitmap.resizeAndRecycle(reqWidth, reqHeight)
return parseCodeResult(getRGBLuminanceSource(bm), hints) return parseCodeResult(getRGBLuminanceSource(bm), hints)
} }
return parseCodeResult(getRGBLuminanceSource(bitmap), hints) return parseCodeResult(getRGBLuminanceSource(bitmap), hints)