我刚刚发现使用Camera2 Basic 示例,在正确设置捕获会话后,从 Camera2 预览中获取人脸数组相对容易。
在 Camera2 Basic 示例的 setUpCameraOutputs 方法中添加以下检查:
int[] FD =characteristics.get(CameraCharacteristics.STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES);
int maxFD=characteristics.get(CameraCharacteristics.STATISTICS_INFO_MAX_FACE_COUNT);
if (FD.length>0) {
List<Integer> fdList = new ArrayList<>();
for (int FaceD : FD
) {
fdList.add(FaceD);
Log.d(TAG, "setUpCameraOutputs: FD type:" + Integer.toString(FaceD));
}
Log.d(TAG, "setUpCameraOutputs: FD count" + Integer.toString(maxFD));
if (maxFD > 0) {
mFaceDetectSupported = true;
mFaceDetectMode = Collections.max(fdList);
}
}
然后在创建捕获会话的 createCameraPreviewSession() 方法中添加以下人脸检测方法调用:
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
// The camera is already closed
if (null == mCameraDevice) {
return;
}
// When the session is ready, we start displaying the preview. ...
// Flash is automatically enabled when necessary.
setAutoFlash(mPreviewRequestBuilder);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>>>>>
//Face detection setup
setFaceDetect(mPreviewRequestBuilder,mFaceDetectMode);...
setFaceDetect(mPreviewRequestBuilder,mFaceDetectMode); 代码可以是这样的:
private void setFaceDetect(CaptureRequest.Builder requestBuilder , int faceDetectMode){
if (mFaceDetectSupported){
requestBuilder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE,faceDetectMode);
}
}
只需添加几行代码,即可在 CameraCaptureSession.CaptureCallback 中完成人脸检测:
private CameraCaptureSession.CaptureCallback mCaptureCallback
= new CameraCaptureSession.CaptureCallback() {
private void process(CaptureResult result) {
switch (mState) {
case STATE_PREVIEW: {
// We have nothing to do when the camera preview is working normally.
//But we can for example detect faces
Face face[]=result.get(CaptureResult.STATISTICS_FACES);
if (face.length>0 ){
Log.d(TAG, "face detected " + Integer.toString(face.length));
takePicture();
}
break;
}
我测试了代码的这种更改并且它有效(一旦检测到我的脸,我就会捕捉自拍......您可以在此处查看 Face 类的更多选项:
Face