kotlin 二维码实现高斯模糊

Kotlin 复制代码
  /**
     * 核心方法:应用模糊效果
     * 兼容 Android 4.4 (API 19) 及以上版本
     */
    private fun applyBlurEffect() {
        // 获取当前 ImageView 里的 Bitmap
        val drawable = viewBinding.qrCodeImage.drawable ?: return
        val bitmap = when (drawable) {
            is android.graphics.drawable.BitmapDrawable -> drawable.bitmap
            else -> {
                // 将其他类型的 Drawable 转换为 Bitmap
                val width = drawable.intrinsicWidth.coerceAtLeast(1)
                val height = drawable.intrinsicHeight.coerceAtLeast(1)
                val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
                val canvas = Canvas(bmp)
                drawable.setBounds(0, 0, canvas.width, canvas.height)
                drawable.draw(canvas)
                bmp
            }
        } ?: return

        // 应用模糊效果
        val blurredBitmap = blurBitmap(bitmap, 15f)

        // 设置模糊后的 Bitmap
        viewBinding.qrCodeImage.setImageBitmap(blurredBitmap)
    }

    /**
     * 对 Bitmap 应用高斯模糊
     * 使用 RenderScript 实现,兼容 API 17+ (Android 4.2+)
     * 对于更低版本,使用简单的缩放模糊作为降级方案
     */
    private fun blurBitmap(bitmap: Bitmap, radius: Float): Bitmap {
        // 创建输出 Bitmap
        val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            // 使用 RenderScript (API 17+)
            try {
                val rs = RenderScript.create(this)
                val input = Allocation.createFromBitmap(rs, bitmap)
                val outputAlloc = Allocation.createFromBitmap(rs, output)

                val script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
                // RenderScript 要求 radius 在 0-25 之间
                val clampedRadius = radius.coerceIn(0f, 25f)
                script.setRadius(clampedRadius)
                script.setInput(input)
                script.forEach(outputAlloc)

                outputAlloc.copyTo(output)
                rs.destroy()
            } catch (e: Exception) {
                // RenderScript 失败时降级到缩放模糊
                applyScaleBlur(bitmap, output)
            }
        } else {
            // API 17 以下使用缩放模糊
            applyScaleBlur(bitmap, output)
        }

        return output
    }

    /**
     * 缩放模糊作为降级方案
     * 先缩小再放大,产生模糊效果
     */
    private fun applyScaleBlur(source: Bitmap, output: Bitmap) {
        val scale = 0.1f // 缩小到 10%
        val smallWidth = (source.width * scale).toInt().coerceAtLeast(1)
        val smallHeight = (source.height * scale).toInt().coerceAtLeast(1)

        // 先缩小
        val small = Bitmap.createScaledBitmap(source, smallWidth, smallHeight, false)

        // 再放大回原尺寸
        val canvas = Canvas(output)
        val scaled = Bitmap.createScaledBitmap(small, source.width, source.height, false)
        canvas.drawBitmap(scaled, 0f, 0f, null)

        // 回收临时 Bitmap
        if (small !== source) small.recycle()
        if (scaled !== small) scaled.recycle()
    }

精简的写法:

Kotlin 复制代码
  /**
     * 对当前 ImageView 中的二维码做模糊处理。
     * 通过多次缩小→放大的方式模拟高斯模糊,兼容所有 Android 版本(含 4.2/4.4 TV),
     * 不依赖 RenderScript 或第三方库。
     */
    private fun applyBlurEffect() {
        val drawable = viewBinding.qrCodeImage.drawable as? BitmapDrawable ?: return
        val src = drawable.bitmap
        val blurred = blurBitmap(src, 2)
        viewBinding.qrCodeImage.setImageBitmap(blurred)
    }

    /**
     * 快速模糊:每次迭代宽高缩小到 1/8 再放大回原尺寸。
     * 缩小倍数越大越模糊,2 次迭代足以让二维码无法识别/扫描。
     */
    private fun blurBitmap(src: Bitmap, passes: Int): Bitmap {
        var bitmap = src
        for (i in 0 until passes) {
            val w = (bitmap.width / 8).coerceAtLeast(1)
            val h = (bitmap.height / 8).coerceAtLeast(1)
            val small = Bitmap.createScaledBitmap(bitmap, w, h, true)
            val scaled = Bitmap.createScaledBitmap(small, bitmap.width, bitmap.height, true)
            small.recycle()
            if (bitmap !== src) {
                bitmap.recycle()
            }
            bitmap = scaled
        }
        return bitmap
    }
相关推荐
传奇开心果10 小时前
【Compose Multiplatform跨端声明式UI实用技术】第19课:多平台部署与发布
android·kotlin
红尘伴蝶舞10 小时前
加密拦截器让抓包失效,我只能从 4KB 的 logcat 碎片里拼回 JSON
android·kotlin·ai编程
booboosui10 小时前
219 个还是 0 个:读不到微信界面时,我只加不删
android
JMchen10 小时前
【Jetpack 内核实战 08 】:组件整合——DataStore + Room 构建离线优先架构
架构·kotlin·android jetpack
Android打工仔10 小时前
Compose 中何时该用 padding,何时该用 offset
android·android jetpack
二流小码农11 小时前
HarmonyOS开发助手,0代码让元服务从开发到上架
android·ios·harmonyos
limit007511 小时前
三维 WebGIS 工具选型:加载、样式、分析、交付,一套界面能做完什么
android·前端·web3·安卓·webgl
骑着蜗牛撵大象32712 小时前
让 Codex 值夜班:把告警排查与巡检接成可回滚的自动化链路
android·java·运维·ai·自动化·codex·智能体
2401_8858850412 小时前
芬兰语音通知接口怎样对接?语音提醒接入流程
android·前端·python·php·语音识别
程序猿老A13 小时前
轻量应用服务器怎么用phpcms搭建网站?
android