【问题标题】:How to click block in Google MLKit android vision如何在 Google MLKit android vision 中点击 block
【发布时间】:2022-07-29 01:53:46
【问题描述】:

我正在尝试使用 google MLKit 视觉构建一个实时文本识别应用程序,它可以正确显示文本,但是当我尝试单击特定行时,它只显示最后一行文本。

这是覆盖代码:

TextGraphic.kt

class TextGraphic(overlay: GraphicOverlay?,
              private val element: Text.Line,
              font: Typeface,
              fontSize: Float,
              color: Int) : Graphic(overlay!!) {

private val rectPaint: Paint = Paint()
private val textPaint: Paint

override fun draw(canvas: Canvas?) {
    val rect = RectF(element.boundingBox)
    canvas!!.drawRect(rect, rectPaint)

    canvas.drawText(element.text, rect.left, rect.bottom, textPaint)
}

companion object {
    private const val TAG = "TextGraphic"
    private const val TEXT_COLOR = Color.BLACK
    private const val STROKE_WIDTH = 2.0f
}

init {
    rectPaint.color = color
    rectPaint.style = Paint.Style.FILL_AND_STROKE
    rectPaint.strokeWidth = STROKE_WIDTH

    textPaint = Paint()
    textPaint.color = TEXT_COLOR
    textPaint.textSize = fontSize
    textPaint.typeface = font
    postInvalidate()
}}

GraphicOverlay.kt

class GraphicOverlay(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
private val lock = Any()
private var previewWidth = 0
private var widthScaleFactor = 1.0f
private var previewHeight = 0
private var heightScaleFactor = 1.0f
private var facing = CameraCharacteristics.LENS_FACING_BACK
private val graphics: MutableSet<Graphic> = HashSet()

abstract class Graphic(private val overlay: GraphicOverlay) {
    abstract fun draw(canvas: Canvas?)

    fun scaleX(horizontal: Float): Float {
        return horizontal * overlay.widthScaleFactor
    }
    fun scaleY(vertical: Float): Float {
        return vertical * overlay.heightScaleFactor
    }
    val applicationContext: Context
        get() = overlay.context.applicationContext
    fun translateX(x: Float): Float {
        return if (overlay.facing == CameraCharacteristics.LENS_FACING_FRONT) {
            overlay.width - scaleX(x)
        } else {
            scaleX(x)
        }
    }

    fun translateY(y: Float): Float {
        return scaleY(y)
    }

    fun postInvalidate() {
        overlay.postInvalidate()
    }
}
fun clear() {
    synchronized(lock) { graphics.clear() }
    postInvalidate()
}

fun add(graphic: Graphic) {
    synchronized(lock) { graphics.add(graphic) }
    postInvalidate()
}

fun remove(graphic: Graphic) {
    synchronized(lock) { graphics.remove(graphic) }
    postInvalidate()
}

fun setCameraInfo(previewWidth: Int, previewHeight: Int, facing: Int) {
    synchronized(lock) {
        this.previewWidth = previewWidth
        this.previewHeight = previewHeight
        this.facing = facing
    }
    postInvalidate()
}

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    synchronized(lock) {
        if (previewWidth != 0 && previewHeight != 0) {
            widthScaleFactor = width.toFloat() / previewWidth.toFloat()
            heightScaleFactor = height.toFloat() / previewHeight.toFloat()
        }
        for (graphic in graphics) {
            graphic.draw(canvas)
        }
    }
}}

在我点击的片段中:

        private fun processTextFromImage(visionText: Text, imageProxy: ImageProxy) {
        binding.graphicOverlay.clear()
        for (block in visionText.textBlocks) {
            for (line in block.lines) {
                val textGraphic = TextGraphic(binding.graphicOverlay, line, font, fontSize, color = fontColor)
                binding.graphicOverlay.apply {
                    add(textGraphic)
                    setOnClickListener {
                        Toast.makeText(it.context, line.text, Toast.LENGTH_SHORT).show()
                    }
                }
                for (element in line.elements) {
                    textFoundListener(element.text)
                }
            }
        }
    }

有没有更好的方式来显示叠加,这个叠加太快了,我的点击只显示最后一行文字。

如果有人可以帮助我,非常感谢。

【问题讨论】:

    标签: android firebase-mlkit google-mlkit


    【解决方案1】:

    您能否举一个更具体的例子来说明“单击特定行,它只显示最后一行文本”的含义

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 2022-08-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2021-07-26
      相关资源
      最近更新 更多