java 高性能低内存高斯模糊
一、高斯模糊算法都是通用的没什么修改和调整的地方
以下是常用高斯模糊算法
fun blur(context: Context, bitmap: Bitmap?, radius: Int): Bitmap? {var radius = radiusif (bitmap == null) {return null}if (radius >= 25) {radius = 24}//Let's create an empty bitmap with the same size of the bitmap we want to blurval outBitmap = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)//Instantiate a new Renderscriptval rs = RenderScript.create(context)//Create an Intrinsic Blur Script using the Renderscriptval blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))//Create the Allocations (in/out) with the Renderscript and the in/out bitmapsif (bitmap == null) {return null}val fromBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)val allIn = Allocation.createFromBitmap(rs, fromBitmap)val allOut = Allocation.createFromBitmap(rs, outBitmap)//Set the radius of the blur: 0 < radius <= 25blurScript.setRadius(radius.toFloat())//Perform the RenderscriptblurScript.setInput(allIn)blurScript.forEach(allOut)//Copy the final bitmap created by the out Allocation to the outBitmapallOut.copyTo(outBitmap)//recycle the original bitmapif (bitmap != null && !bitmap.isRecycled) {bitmap.recycle()}//After finishing everything, we destroy the Renderscript.rs.destroy()return outBitmap}
二、高斯模糊分析
通过上面方式对图片做高斯模糊非常有限,以下就是常用高斯模糊算法计算最大模糊度后结果。
缺点:
1、图片太大性能消耗很大耗时。
2、高斯模糊度范围太小无法做到更大程度模糊度。
3、大图片占用内存大。
三、使用新方案对比
1.耗时计算
耗时
count ratio = 1 time = 137
count ratio = 10 time = 96
count ratio = 20 time = 6
count ratio = 30 time = 3
count ratio = 40 time = 2
count ratio = 50 time = 2
count ratio = 60 time = 2
count ratio = 70 time = 2
count ratio = 80 time = 2
count ratio = 90 time = 2
count ratio = 100 time = 1
2.图片内存占用对比
内存
ratio = 1 allocationByteCount 34617600
ratio = 10 allocationByteCount 345600
ratio = 20 allocationByteCount 86400
ratio = 30 allocationByteCount 38400
ratio = 40 allocationByteCount 21600
ratio = 50 allocationByteCount 13824
ratio = 60 allocationByteCount 9600
ratio = 70 allocationByteCount 6936
ratio = 80 allocationByteCount 5400
ratio = 90 allocationByteCount 4160
ratio = 100 allocationByteCount 3456
3.高斯模糊程度对比
从1-100比例
动态效果
四、核心代码
1、新方案核心代码,主要就是对图片先放缩后对图片进行做高斯模糊处理,两个叠加以后可以做到更大程度模糊效果
fun getBlurBitmap(context: Context, bitmap: Bitmap, ratio: Int, callback: (Bitmap?) -> Unit) {//后台线程处理GlobalScope.async {val start = SystemClock.currentThreadTimeMillis()if (ratio != 0) {//先对图片压缩val width = (bitmap.width / ratio)val height = (bitmap.height / ratio)val scalBimap: Bitmap? = Bitmap.createScaledBitmap(bitmap,width,height,false)//做高斯模糊效果val blurBitmap: Bitmap? = blur(context, scalBimap, maxRadius)Log.d(TAG, "getBlurBitmap ratio = " + ratio + " allocationByteCount " + blurBitmap?.allocationByteCount)//切换线程回调withContext(Dispatchers.Main) {callback(blurBitmap)}} else {withContext(Dispatchers.Main) {callback(bitmap)}}val end = SystemClock.currentThreadTimeMillis()Log.d(TAG,"getBlurBitmap count ratio = "+ratio+" time = "+(end - start))}}
2、放缩后自动适配显示
使用ImageView,设置setScaleType(ImageView.ScaleType.CENTER_CROP);
package com.data.blur.blurviewdemon.viewimport android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.widget.ImageView
import com.data.blur.blurviewdemon.utils.BlurUtilclass BlurView : ImageView {constructor(context: Context) : this(context,null)constructor(context: Context, attrs: AttributeSet?) : this(context, attrs,-1)constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(context, attrs, defStyleAttr,-1)constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {init()}fun init(){setScaleType(ImageView.ScaleType.CENTER_CROP);}public fun setImageBimtap(bitmap : Bitmap, ratio : Int){BlurUtil.getBlurBitmap(context,bitmap,ratio){ bitmap -> setImageBitmap(bitmap)}}
}
demo传输门:https://github.com/xingchongzhu/BlurViewDemo
java 高性能低内存高斯模糊相关推荐
- PHP合并大文件 高性能 低内存 低CPU 快速合并大文件 非耗时操作 快速合并PDF等影视大文件...
为什么80%的码农都做不了架构师?>>> <?php$command = 'pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf';$re ...
- java 拼音_GitHub - promeG/TinyPinyin: 适用于Java和Android的快速、低内存占用的汉字转拼音库。...
TinyPinyin 适用于Java和Android的快速.低内存占用的汉字转拼音库. 当前稳定版本:2.0.3 特性 生成的拼音不包含声调,均为大写: 支持自定义词典,支持简体中文.繁体中文: 执行 ...
- java 内存管理_高性能Java代码之内存管理
本文通过几个方面,来介绍Java代码的内存管理. 有的代码,GC根本就回收不了,直接系统挂掉.GC是一段程序,不是智能,他只回收他认为的垃圾,而不是回收你认为的垃圾. GC垃圾回收: Grabage ...
- 微信java精简版低内存_微信精简版低内存apk-微信精简版2019下载v7.0.6 安卓版-腾牛安卓网...
微信精简版,去除繁杂的功能,保留最核心的微信通讯.公众号等功能,去除升级永保低内存运行,适合各类高旧手机,使用微信再也不会面临卡顿,内存占用太大等问题了. 版本特性: 1.去除升级 2.去除游戏等各种 ...
- Java进阶 JVM 内存与垃圾回收篇(一)
JVM 1. 引言 1.1 什么是JVM? 定义 Java Vritual Machine - java 程序的运行环境(Java二进制字节码的运行环境) 好处 一次编译 ,到处运行 自动内存管理,垃 ...
- 形式参数内存在哪java_深入浅出Java中JVM内存管理
原标题:深入浅出Java中JVM内存管理 Java岗位面试,JVM是对程序员基本功考察,通常会问你对JVM了解吗?可以分几部分回答这个问题,首先JVM内存划分 | JVM垃圾回收的含义 | 有哪些GC ...
- java 物理内存_聊聊Java中的内存
JVM的内存 先放一张JVM的内存划分图,总体上可以分为堆和非堆(粗略划分,基于java8) 那么一个Java进程最大占用的物理内存为: Max Memory = eden + survivor + ...
- 独占设备的分配与回收_灵魂拷问:Java对象的内存分配过程是如何保证线程安全的?...
点击上方"linkoffer", 选择关注公众号高薪职位第一时间送达 作者 l Hollis JVM内存结构,是很重要的知识,相信每一个静心准备过面试的程序员都可以清楚的把堆.栈. ...
- 内存映射文件 写入 卡住_在Java中使用内存映射文件时检测(写入)失败
内存映射文件 写入 卡住 内存映射文件是一个很好的并且经常被忽视的工具. 我不会在这里详细介绍它们的工作方式(使用 力 Google Luke!),但我将快速总结其优势: 操作系统提供的延迟加载和写入 ...
最新文章
- LeetCode简单题之作为子字符串出现在单词中的字符串数目
- Redis初学:14(Redis中的事务)
- R语言plotly可视化:plotly可视化在散点图中添加误差条(Scatterplot with Error Bars with plotly in R)
- ca 自建 颁发证书_openssl自建CA后颁发证书
- SCVMM2012 SP1 添加VMware vCenter服务器
- Spread Studio for .NET 7: 使用现有数据填充单元格
- 《系统集成项目管理工程师》必背100个知识点-100信息技术服务标准
- vb.net axWindowsMediaPlayer 控件使用
- Java 在指定目录中查找文件
- 百度高级副总裁沈抖加入爱奇艺董事会 王路退出
- 人工智能必备数学知识· 学习笔记 ·002【马尓可夫链,马尓可夫链奖励过程,马尔可夫决策过程】
- hadoop开启后jps只有jps一个进程
- 简洁大气的网站微信QQ防红跳转代码
- AGM FPGA选型手册
- 合作博弈(coalitional game)——核仁(Nucleolus)初解
- artDialog | 经典的网页对话框组件
- - 麦田守望者 -读后感
- 黑苹果E430c, 安装过程
- 视频画面怎么快速进行水平翻转?
- 凯撒密码(移位加密)
热门文章
- CA-IS3721HS 双通道标准数字隔离器简介
- 基于C++的Easyx库的考勤管理系统
- mysql考勤数据库设计,mysql – 跟踪学生出勤的数据库设计
- CSS水平居中+垂直居中+水平/垂直居中的方法总结
- 惠普(hp)官网驱动下载和更新网址
- TensorFlow 1.x 深度学习秘籍:11~14
- SSM 实训笔记 -10- 使用 sessionStorage 存储数据、js 图片验证码、登录加载动画
- 智鼎逻辑推理题及答案_宝洁和联合利华这类快消500强企业怎么进?
- 小甜点,RecyclerView 之 ItemDecoration 讲解及高级特性实践
- WCF 之 什么是分布式系统