700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android人脸识别绘制人脸框自定义View显示

Android人脸识别绘制人脸框自定义View显示

时间:2021-04-05 12:30:08

相关推荐

Android人脸识别绘制人脸框自定义View显示

创建一个自定义的View,它将绘制人脸框,并重写 onDraw 方法以绘制矩形。 下面是一个示例代码:

public class FaceBoundsView extends View {private List<Rect> faceBounds;private Paint rectPaint;public FaceBoundsView(Context context) {super(context);init();}public FaceBoundsView(Context context, AttributeSet attrs) {super(context, attrs);init();}public FaceBoundsView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}private void init() {rectPaint = new Paint();rectPaint.setColor(Color.GREEN);rectPaint.setStyle(Paint.Style.STROKE);rectPaint.setStrokeWidth(5);}public void setFaceBounds(List<Rect> faceBounds) {this.faceBounds = faceBounds;invalidate();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (faceBounds != null) {for (Rect rect : faceBounds) {canvas.drawRect(rect, rectPaint);}}}}

在Activity或Fragment中,您可以按如下方式使用此视图:

FaceBoundsView faceBoundsView = findViewById(R.id.face_bounds_view);faceBoundsView.setFaceBounds(faceBounds);

在布局中,您可以按如下方式添加此视图

<com.example.FaceBoundsViewandroid:id="@+id/face_bounds_view"android:layout_width="match_parent"android:layout_height="match_parent"/>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。