如何使用带有纹理视图的坐标在屏幕上绘制矩形? [英] How to draw a rectangle on the screen using coordinates with texture view?

查看:408
本文介绍了如何使用带有纹理视图的坐标在屏幕上绘制矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cameraX将摄像机的供稿预览到纹理视图.如何使用我拥有的坐标在屏幕上绘制矩形?我不要复杂的功能.我只是想画一个矩形. 我正在使用kotlin和android studio 4.0.

I am previewing camera feed to texture view using cameraX. How to draw a rectangle on the screen using coordinates that i have? I don't want a complex function. I simply want to draw a rectangle. I am using kotlin and android studio 4.0.

推荐答案

我将使用重叠在同一xml上的TextureView上的ImageView.此imageview将加载仅绘制矩形的透明位图.如果您有坐标,则必须执行以下操作:

I would go for an ImageView overlapping on top of the Textureview at the same xml. This imageview will load a transparent bitmap that will have only the rectangle drawn. If you have the coordinates u have to do:

val myRectPaint = Paint()
myRectPaint.strokeWidth = 5F
myRectPaint.color = Color.RED
myRectPaint.style = Paint.Style.STROKE

// Create a Canvas object for drawing on the original bitmap provided
val tempBitmap =
    Bitmap.createBitmap(bitmap!!.width, bitmap.height, Bitmap.Config.ARGB_8888)
val tempCanvas = Canvas(tempBitmap)
tempCanvas.drawBitmap(bitmap, 0F, 0F, null)

tempCanvas.drawRoundRect(
            RectF(x1.toFloat(), y1.toFloat(), x2.toFloat(), y2.toFloat()),
            2f,
            2f,
            myRectPaint
        )

// Use this to widen picture on top or bottom
    val croppedFaceBitmap =
        Bitmap.createBitmap(tempBitmap, x1, y1, x2, y2)

无论如何,您还可以遵循此示例来自tensorflow github,其中在检测到对象时绘制圆形框.

In any case you can also follow this example from tensorflow github where round boxes are drawn when object is detected.

希望我能帮忙

这篇关于如何使用带有纹理视图的坐标在屏幕上绘制矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆