如何在CameraX TextureView上绘制矩形

问题描述 投票:0回答:1

我写了下面的代码来绘制一个红色矩形,但是显然这是错误的:(我没有得到任何错误,同时也没有得到任何绘制,我该如何解决它

    private fun updateTransform() {
        val matrix = Matrix()

        // Compute the center of the view finder
        val centerX = viewFinder.width / 2f
        val centerY = viewFinder.height / 2f

        // Correct preview output to account for display rotation
        val rotationDegrees = when(viewFinder.display.rotation) {
            Surface.ROTATION_0 -> 0
            Surface.ROTATION_90 -> 90
            Surface.ROTATION_180 -> 180
            Surface.ROTATION_270 -> 270
            else -> return
        }
        matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)

        val bmp = Bitmap.createBitmap(1425, 1425, Bitmap.Config.ARGB_8888)
        val paint = Paint().apply {
            isAntiAlias = true
            style = Paint.Style.STROKE
            color = Color.RED
            strokeWidth = 10f
        }
        // x: {559, y: 1901}, height: 1425, width: 1425
        var canvas = Canvas(bmp)
        canvas.apply {
            drawRect(
                25.toFloat(), // faceRectangle.left,
                25.toFloat(), //faceRectangle.top,
                250.toFloat(),
                250.toFloat(),
                paint
            )
        }

        viewFinder.unlockCanvasAndPost(canvas)

        viewFinder.setTransform(matrix)

    }
android android-camerax
1个回答
0
投票

我找到了解决方法,不确定是否这是唯一的方法,无论如何,我会在这里发布自己的方法,以防将来对某人有所帮助:

  • 解决方案是使用cameeraX纹理视图创建具有相同尺寸和位置(约束)的图像视图,以使其作为叠加层,并在其上进行所有绘图,您只需要确保此覆盖图是以上不在纹理视图下,即在布局xml文件中跟随它。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextureView
        android:id="@+id/view_finder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:visibility="visible" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/pic_desc"
        android:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>

绘制代码为:

lateinit var overlay: Bitmap
private fun startCamera() {
        val analyzerConfig = ImageAnalysisConfig.Builder().apply {
            setImageReaderMode(
                ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
        }.build()

        val analyzerUseCase = ImageAnalysis(analyzerConfig).apply {
            analyzer = ImageAnalysis.Analyzer { image, rotationDegrees ->
                val bitmap = view_finder.bitmap ?: return@Analyzer
                overlay = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)

                scope.launch(Dispatchers.Unconfined) {
                    val mat = Mat()
                    Utils.bitmapToMat(bitmap!!, mat)

                    val detectedFaces = FaceDetection.detectFaces(bitmap!!)

                   if (detectedFaces.toArray().isNotEmpty()) {
                       val paint = Paint().apply {
                           isAntiAlias = true
                           style = Paint.Style.STROKE
                           color = Color.RED
                           strokeWidth = 10f
                       }

                       for (rect in detectedFaces.toArray()) {
                           var canvas = Canvas(overlay)
                           canvas.drawRect(
                               rect.x.toFloat(), 
                               rect.y.toFloat(), 
                               rect.x.toFloat() + rect.width,
                               rect.y.toFloat() + rect.height,
                               paint
                           )
                           overlay?.let { Canvas(it) }?.apply {
                               canvas
                           }
                       }
                   }
                }

                runOnUiThread {
                    imageView.setImageBitmap(overlay)
                }
            }
        }
   CameraX.bindToLifecycle(this, preview, imageCapture, analyzerUseCase)
}

下面是我的应用程序的输出,截至目前显示此绘图:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.