人脸检测UI

This commit is contained in:
2026-04-27 17:03:56 +08:00
parent b47704b281
commit 1b62e4d264
9 changed files with 192 additions and 14 deletions

View File

@@ -0,0 +1,16 @@
package com.example.defenseapplication.okhttp
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
object ApiService {
private const val BASE_URL = "https://stem-boozy-margarine.ngrok-free.dev"
private val client = OkHttpClient()
private val gson = Gson()
}

View File

@@ -0,0 +1,8 @@
package com.example.defenseapplication.okhttp
data class Post(
val userId: Int,
val id: Int,
val title: String,
val body: String
)

View File

@@ -1,4 +0,0 @@
package com.example.defenseapplication.okhttp
class OkHttpClient {
}

View File

@@ -4,20 +4,31 @@ import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.google.mlkit.vision.face.Face
class FaceOverlayView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private val paint = Paint().apply {
color = Color.GREEN
private val framePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.WHITE
style = Paint.Style.STROKE
strokeWidth = 4f
strokeWidth = 6f
}
private val textPaint = Paint().apply {
color = Color.YELLOW
textSize = 40f
private val maskPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.parseColor("#7A000000")
style = Paint.Style.FILL
}
private val facePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.parseColor("#80FFFFFF")
style = Paint.Style.STROKE
strokeWidth = 3f
}
private val maskPath = Path()
private val frameRect = RectF()
private var faces = mutableListOf<Face>()
fun setFaces(faces: List<Face>) {
@@ -27,10 +38,27 @@ class FaceOverlayView(context: Context, attrs: AttributeSet?) : View(context, at
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val cx = width / 2f
val cy = height * 0.45f
val radius = minOf(width, height) * 0.31f
maskPath.reset()
maskPath.addRect(0f, 0f, width.toFloat(), height.toFloat(), Path.Direction.CW)
maskPath.addCircle(cx, cy, radius, Path.Direction.CCW)
canvas.drawPath(maskPath, maskPaint)
canvas.drawCircle(cx, cy, radius, framePaint)
frameRect.set(cx - radius, cy - radius, cx + radius, cy + radius)
for (face in faces) {
val rect = face.boundingBox
canvas.drawRect(rect, paint)
canvas.drawText("Face", rect.left.toFloat(), rect.top - 10f, textPaint)
val clippedLeft = rect.left.coerceAtLeast(frameRect.left.toInt()).toFloat()
val clippedTop = rect.top.coerceAtLeast(frameRect.top.toInt()).toFloat()
val clippedRight = rect.right.coerceAtMost(frameRect.right.toInt()).toFloat()
val clippedBottom = rect.bottom.coerceAtMost(frameRect.bottom.toInt()).toFloat()
if (clippedRight > clippedLeft && clippedBottom > clippedTop) {
canvas.drawRect(clippedLeft, clippedTop, clippedRight, clippedBottom, facePaint)
}
}
}
}