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 }