Android createScaledBitmap与Canvas通过RectF drawBitmap生成马赛克/高斯模糊(毛玻璃)对比,Kotlin

Android createScaledBitmap与Canvas通过RectF drawBitmap生成马赛克/高斯模糊(毛玻璃)对比,Kotlin

Kotlin 复制代码
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.RectF
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.toRect

class MainActivity : AppCompatActivity() {
    private var mScaleFactor = 16f //值越大,马赛克效果越强。
    private var mSrcBmp: Bitmap? = null
    private val mResId = R.mipmap.pic
    private var mSrcBmpW = 0
    private var mSrcBmpH = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val image1 = findViewById<ImageView>(R.id.image1)
        val image2 = findViewById<ImageView>(R.id.image2)

        mSrcBmp = BitmapFactory.decodeResource(resources, mResId, null)
        mSrcBmpW = mSrcBmp!!.width
        mSrcBmpH = mSrcBmp!!.height

        val smallBmp = getSmallBmp(mSrcBmp!!)

        image1.setImageBitmap(Bitmap.createScaledBitmap(smallBmp, mSrcBmpW, mSrcBmpH, true))
        image2.setImageBitmap(getScaleBmp(smallBmp))
    }

    private fun getSmallBmp(srcBmp: Bitmap): Bitmap {
        return Bitmap.createScaledBitmap(srcBmp, (mSrcBmpW / mScaleFactor).toInt(), (mSrcBmpH / mScaleFactor).toInt(), true)
    }

    private fun getScaleBmp(srcBmp: Bitmap): Bitmap {
        //空Bitmap
        val dstBmp = Bitmap.createBitmap(mSrcBmpW, mSrcBmpH, Bitmap.Config.ARGB_8888)

        val srcRectF = RectF(0f, 0f, srcBmp.width.toFloat(), srcBmp.height.toFloat())
        val dstRectF = RectF(0f, 0f, mSrcBmpW.toFloat(), mSrcBmpH.toFloat())

        val c = Canvas(dstBmp)
        c.drawBitmap(srcBmp, srcRectF.toRect(), dstRectF.toRect(), null)

        return dstBmp
    }
}

自上往下,第一张是原图,第二张是通过

java 复制代码
    public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight,
            boolean filter)

先把原图缩小1/n,然后再放大到原图等同大小,实现马赛克,发现这种方式生成的Bitmap,肯定可以达到马赛克效果,并基本接近高斯模糊效果。

第三张图是通过Canvas的

java 复制代码
    public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst,
            @Nullable Paint paint)

基于RectF放大小图到原图大小,发现马赛克的颗粒度更大,更具备马赛克典型特征。

总结起来,

java 复制代码
    public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight,
            boolean filter)

生成的图更平滑。

这种方式生成的图:

java 复制代码
    public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst,
            @Nullable Paint paint) 

尤其是放大情况下,拉伸的跨越明显,更具大颗粒度的马赛克效果。

Android BitmapShader简洁实现马赛克/高斯模糊(毛玻璃),Kotlin(三)-CSDN博客文章浏览阅读753次,点赞5次,收藏10次。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android BitmapShader简洁实现马赛克,Kotlin(一)-CSDN博客。https://zhangphil.blog.csdn.net/article/details/145322436

相关推荐
EQ-雪梨蛋花汤1 小时前
【Part 2安卓原生360°VR播放器开发实战】第三节|实现VR视频播放与时间轴同步控制
android·交互·vr
拾贰_C1 小时前
【IDEA_Maven】(进阶版)永久性的更改IDEA中每个项目所依赖的Maven默认配置文件及其仓库路径
android·maven·intellij-idea
androidwork3 小时前
Kotlin与Android Studio开发环境配置指南
开发语言·kotlin·android studio
stevenzqzq3 小时前
kotlin 01flow-StateFlow 完整教程
android·开发语言·kotlin·flow
androidwork3 小时前
使用Kotlin Flow实现Android应用的响应式编程
android·开发语言·kotlin
stevenzqzq3 小时前
kotlin 数据类
android·开发语言·kotlin
每次的天空4 小时前
Android单例模式知识总结
android·单例模式
追随远方4 小时前
Android平台FFmpeg视频解码全流程指南
android·ffmpeg·音视频
姜行运6 小时前
数据结构【二叉搜索树(BST)】
android·数据结构·c++·c#
JhonKI14 小时前
【MySQL】存储引擎 - CSV详解
android·数据库·mysql