ViseFace
簡易人臉檢測庫,不依賴三方庫,可快速接入人臉檢測功能。
- 項目依賴:
compile 'com.vise.xiaoyaoyou: viseface:1.0.0'
為什么打造該庫
1、想簡單快速接入人臉檢測功能;
2、Google 提供的人臉檢測功能部分手機無法適配;
3、第三方提供的人臉檢測功能接入門檻過高;
4、依賴第三方庫會增加 APK 大小。
功能介紹
1、可快速識別人臉;
2、可適配所有機型;
3、可配置最大檢測人臉數;
4、可配置是否顯示人臉檢測框;
5、可配置當前檢測人臉攝像頭為前置和后置;
6、可檢測到最近人臉范圍的光照值,光照范圍 0 - 255;
7、可檢測到的最近人臉相對於屏幕寬度的比例。
效果演示
項目結構
使用介紹
1、導入人臉檢測庫
在工程的 build 文件中添加如下依賴:
compile 'com.vise.xiaoyaoyou:viseface:1.0.0'
2、創建相機預覽布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--相機預覽界面,必須設置-->
<com.vise.face.CameraPreview
android:id="@+id/face_detector_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--繪制人臉識別框,可依需配置-->
<com.vise.face.FaceRectView
android:id="@+id/face_detector_face"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--拍照按鈕,點擊后進行拍照,按照需要進行添加-->
<Button
android:id="@+id/face_detector_take_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="30dp"
android:layout_gravity="bottom|center_horizontal"
android:background="@android:drawable/ic_menu_camera"
/>
</FrameLayout>
注意:最外層布局必須是 FrameLayout
;如果代碼中配置需要繪制人臉檢測框,那么布局必須添加 FaceRectView
。
3、創建人臉檢測實現對象
IFaceDetector mFaceDetector = new NormalFaceDetector();
4、創建權限檢查監聽
ICameraCheckListener mCameraCheckListener = new ICameraCheckListener() {
@Override
public void checkPermission(boolean isAllow) {
//權限是否允許
ViseLog.i("checkPermission" + isAllow);
}
@Override
public void checkPixels(long pixels, boolean isSupport) {
//手機像素是否滿足要求
ViseLog.i("checkPixels" + pixels);
}
};
5、創建檢測數據監聽
IDataListener mDataListener = new IDataListener() {
@Override
public void onDetectorData(DetectorData detectorData) {
//回調識別到的數據
ViseLog.i("識別數據:" + detectorData);
}
};
6、設置相關配置,創建人臉檢測代理
該庫的核心思想就是快速接入人臉檢測功能,所以該庫的功能都是通過 DetectorProxy
代理類來實現,使用簡單明了。具體使用場景如下:
//創建代理類,必須傳入相機預覽界面
DetectorProxy mDetectorProxy = new DetectorProxy.Builder(mFace_detector_preview)
//設置權限檢查監聽
.setCheckListener(mCameraCheckListener)
//設置人臉檢測實現
.setFaceDetector(mFaceDetector)
//設置檢測數據回調監聽
.setDataListener(mDataListener)
//設置繪制人臉識別框界面
.setFaceRectView(mFace_detector_face)
//設置是否繪制人臉檢測框
.setDrawFaceRect(true)
//設置預覽相機的相機ID
.setCameraId(Camera.CameraInfo.CAMERA_FACING_BACK)
//設置可檢測的最大人臉數
.setMaxFacesCount(5)
//設置人臉識別框是否為完整矩形
.setFaceIsRect(false)
//設置人臉識別框的RGB顏色
.setFaceRectColor(Color.rgb(255, 203, 15))
//創建代理類
.build();
7、開啟人臉檢測
if (mDetectorProxy != null) {
mDetectorProxy.detector();
}
8、釋放資源
if (mDetectorProxy != null) {
mDetectorProxy.release();
}
基於Android平台的簡易人臉檢測庫
注:本文著作權歸作者,由demo大師代發,拒絕轉載,轉載需要作者授權