Search code examples
androidface-detectionboundingandroid-framelayout

How to draw bounding box around face on frameLayout android


I have a frameLayout in xml:

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

This preview is for displaying camera view:

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

It successfully displaying face from the front camera. And I have the face rectangle x and y coordinates. How can I display a bounding box of face on the frameLayout?

Thank you.


Solution

  • well, you could use a ShapeDrawable, and set it's layout parameters to be the size and location you need, and add it to the FrameLayout along with your CameraSurfacePreview.

    It's not all the difficult. First create a ShapeDrawable with the properties you want. Then set it as the background of a standard View object, and add the view with the layout parameters to size it how you want. So if you want the

    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);
    

    In this particular case, i've made it a 100x100 view, so the shape will auto size to the view. and i've set it up so that it's offset from the top corner by the values left and top.

    There's lots of ways to do this, but this seems the simplest. Of course you could do all this in XML too. There's lots of tutorials out there on how to do this.