【问题标题】:How to draw bounding box around face on frameLayout android如何在frameLayout android上围绕面部绘制边界框
【发布时间】:2016-01-14 16:29:51
【问题描述】:

我在 xml 中有一个 frameLayout:

<FrameLayout
    android:layout_width="150px"
    android:layout_height="200px"
    android:id="@+id/preview">
</FrameLayout>

此预览用于显示相机视图:

mPreview = new CameraSurfacePreview(MainActivity.this, cameraObj,...);
preview.addView(mPreview2);
....

它成功地从前置摄像头显示面部。我有面部矩形 x 和 y 坐标。如何在 frameLayout 上显示人脸边界框?

谢谢。

【问题讨论】:

    标签: android face-detection bounding android-framelayout


    【解决方案1】:

    好吧,您可以使用 ShapeDrawable,并将其布局参数设置为您需要的大小和位置,然后将其与您的 CameraSurfacePreview 一起添加到 FrameLayout。

    这并不难。首先使用您想要的属性创建一个 ShapeDrawable。然后将其设置为标准 View 对象的背景,并添加带有布局参数的视图以调整其大小。所以如果你想要

    ShapeDrawable sd = new ShapeDrawable(new RectShape());
    sd.getPaint().setColor(0xFFFFFFFF);
    sd.getPaint().setStyle(Paint.Style.STROKE);
    sd.getPaint().setStrokeWidth(1);
    View shapeView = new View(context);
    shapeView.setBackground(sd);
    
    
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(100, 100);
    params.setMargins(left, top, 0, 0);
    frameLayout.addView(shapeView, params);
    

    在这种特殊情况下,我将其设置为 100x100 视图,因此该形状将自动调整为视图大小。我已经设置了它,使它从顶角偏移了 left 和 top 的值。

    有很多方法可以做到这一点,但这似乎是最简单的。当然,您也可以在 XML 中完成所有这些工作。有很多关于如何做到这一点的教程。

    【讨论】:

    • 我之前没用过ShapeDrawable。它应该在画布上绘制。我应该重写 onDraw 方法以使其在 FrameLayout 上绘制吗?
    猜你喜欢
    • 2017-03-30
    • 2013-02-20
    • 2020-06-18
    • 1970-01-01
    • 2014-06-17
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    相关资源
    最近更新 更多