android 圆形人脸识别框

本文介绍了如何在Android中创建圆形人脸识别框,利用讯飞的人脸检测功能,并给出了设置圆角、全屏显示Dialog以及在Fragment和Activity间传递识别值的方法。
摘要由CSDN通过智能技术生成

                                                                               android 圆形人脸识别框

圆形人脸识别框

识别框

使用讯飞人脸检测功能,参照我的另一篇博客

https://blog.csdn.net/baidu_35824246/article/details/103878405

首先通过shape设置圆角,背景设置透明

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="@color/transparent" />

    <corners android:radius="120dp" />

</shape>
Android提供了FaceDetector类用于人脸检测,但是它并不支持圆形检测。如果需要实现圆形检测,可以通过以下步骤实现: 1. 使用FaceDetector检测人脸并获取人脸矩形的位置信息。 2. 根据人脸矩形的位置信息计算出圆心坐标和半径大小。 3. 使用Canvas绘制一个圆形,并设置画笔的颜色和宽度。 4. 在圆形上绘制一个透明的矩形,使得圆形中间部分变成透明。 5. 最后绘制人脸矩形,使得圆形人脸。 以下是一个示例代码: ```java // 创建FaceDetector对象 FaceDetector detector = new FaceDetector.Builder(context) .setTrackingEnabled(false) .setLandmarkType(FaceDetector.ALL_LANDMARKS) .setClassificationType(FaceDetector.ALL_CLASSIFICATIONS) .build(); // 获取Bitmap对象 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face); // 创建Canvas对象 Canvas canvas = new Canvas(bitmap); // 检测人脸 Frame frame = new Frame.Builder().setBitmap(bitmap).build(); SparseArray<Face> faces = detector.detect(frame); // 绘制圆形 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); for (int i = 0; i < faces.size(); i++) { Face face = faces.valueAt(i); // 计算圆心坐标和半径大小 PointF center = new PointF(face.getPosition().x + face.getWidth() / 2, face.getPosition().y + face.getHeight() / 2); float radius = Math.min(face.getWidth(), face.getHeight()) / 2; // 绘制圆形 canvas.drawCircle(center.x, center.y, radius, paint); // 绘制透明矩形 paint.setStyle(Paint.Style.FILL); paint.setColor(Color.TRANSPARENT); canvas.drawRect(center.x - radius, center.y - radius, center.x + radius, center.y + radius, paint); // 绘制人脸矩形 paint.setColor(Color.GREEN); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3); canvas.drawRect(face.getPosition().x, face.getPosition().y, face.getPosition().x + face.getWidth(), face.getPosition().y + face.getHeight(), paint); } // 显示Bitmap图像 imageView.setImageBitmap(bitmap); ``` 这样就可以实现一个圆形人脸检测了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值