Android kotlin 对拍照进行高斯模糊

private lateinit var mCamera: Camera //SurfaceView相机
 /**
     * 背景获取
     */
    fun takeScreenShot(gHandler: Handler){
        val callback: Camera.PictureCallback = object : Camera.PictureCallback {
            private lateinit var scaledBitmap: Bitmap
            override fun onPictureTaken(data: ByteArray, Camera: Camera) {
//                cameraTopRectView.draw(Canvas())
                try {
                    scaledBitmap = BitmapFactory.decodeByteArray(data, 0, data.size)// 获得图片
                    val msg = Message()
                    //对照片进行高斯模糊,最大为25f
                    msg.obj = blur(mContext,scaledBitmap,20f)
                    msg.what = MESSAGE_PHOTO_ALBUM
                    gHandler.sendMessage(msg)
                    scaledBitmap.recycle() // 回收bitmap空间
                    mCamera.startPreview()//摄像头预览打开
                } catch (e: Exception) {
                    e.printStackTrace()
                } finally {
                }
            }
        }
//设置参数,并拍照
//        setCameraParams(mCamera, mScreenWidth, mScreenHeight)
// 当调用camera.takePiture方法后,camera关闭了预览,这时需要调用startPreview()来重新开启预览
        mCamera.takePicture(null, null, callback)
    }

附上高斯模糊方法

//对照片进行高斯模糊
fun blur(context: Context, bitmap: Bitmap, radius: Float): Bitmap {
    val width = (bitmap.width * 0.4f).roundToInt()
    val height = (bitmap.height * 0.4f).roundToInt()

    // 创建缩小后的Bitmap对象
    val inputBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false)
    val outputBitmap = Bitmap.createBitmap(inputBitmap)

    // 创建RenderScript对象和ScriptIntrinsicBlur对象
    val rs = RenderScript.create(context)
    val theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))

    // 创建Allocation对象
    val tmpIn = Allocation.createFromBitmap(rs, inputBitmap)
    val tmpOut = Allocation.createFromBitmap(rs, outputBitmap)

    // 设置高斯模糊半径并进行模糊处理
    theIntrinsic.setRadius(radius)
    theIntrinsic.setInput(tmpIn)
    theIntrinsic.forEach(tmpOut)

    // 将模糊后的数据写入输出Bitmap对象中
    tmpOut.copyTo(outputBitmap)

    // 释放资源
    rs.destroy()

    return outputBitmap
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值