【发布时间】:2021-07-12 06:25:20
【问题描述】:
我们在 Android 中实现了用于人脸检测的 Android ML Kit。它的作用就像魅力一样,可以检测人脸。
问题:当检测到多个人脸时,我们想在检测到的人脸周围绘制矩形
我们做了什么:
已实现
implementation 'com.google.android.gms:play-services-mlkit-face-detection:16.1.5'
创建了一个自定义视图:
class FaceView(val theContext : Context, val bounds : Rect) : View(theContext) {
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
val myPaint = Paint()
myPaint.color = Color.BLACK
myPaint.style = Paint.Style.STROKE
myPaint.strokeWidth = 10f
canvas?.drawRect(bounds, myPaint)
}
}
试图在我们从人脸对象 ML 套件创建的边界上绘制一个矩形
val result = detector.process(image).addOnSuccessListener { faces ->
for (face in faces) {
val bounds = face.boundingBox
val view = FaceView(requireContext(), bounds)
binding.actionRoot.addView(view)
val lp : ConstraintLayout.LayoutParams =
ConstraintLayout.LayoutParams(bounds.width(),bounds.height())
lp.startToStart = binding.actionPhoto.id
lp.topToTop = binding.actionPhoto.id
lp.marginStart = bounds.right
lp.topMargin = bounds.bottom
view.layoutParams = lp
}}
结果:
我们如何为我们从 URI(不是从 CameraX)生成的每个人脸绘制一个矩形并使其可点击?
【问题讨论】:
标签: android android-canvas face-detection firebase-mlkit google-mlkit