简单的集成可以参照:https://github.com/RedApparat/FaceDetector,基本的使用和介绍已经很易懂了。下面是整理的更为全面一些的学习总结。
下载github的源码之后,要到Example文件夹中去运行Demo项目,而不是-master那个层级的项目,如下图所示:
打开项目之后可以看到demo很简单,只有两个类:MainActivity和PermissionsDelegate这个运行时权限委托类,MainActivity的源码如下,由如下的代码可见,要启用人脸检测功能仅仅是调用了createFotoapparat()这一个自定义函数:
package io.fotoapparat.facedetector.example;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import java.util.List;
import io.fotoapparat.Fotoapparat;
import io.fotoapparat.FotoapparatSwitcher;
import io.fotoapparat.facedetector.Rectangle;
import io.fotoapparat.facedetector.processor.FaceDetectorProcessor;
import io.fotoapparat.facedetector.view.RectanglesView;
import io.fotoapparat.parameter.LensPosition;
import io.fotoapparat.view.CameraView;
import static io.fotoapparat.log.Loggers.fileLogger;
import static io.fotoapparat.log.Loggers.logcat;
import static io.fotoapparat.log.Loggers.loggers;
import static io.fotoapparat.parameter.selector.LensPositionSelectors.lensPosition;
public class MainActivity extends AppCompatActivity {
private final PermissionsDelegate permissionsDelegate = new PermissionsDelegate(this);
private boolean hasCameraPermission;
private CameraView cameraView;
private RectanglesView rectanglesView;
private FotoapparatSwitcher fotoapparatSwitcher;
private Fotoapparat frontFotoapparat;
private Fotoapparat backFotoapparat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraView = (CameraView) findViewById(R.id.camera_view);
rectanglesView = (RectanglesView) findViewById(R.id.rectanglesView);
hasCameraPermission = permissionsDelegate.hasCameraPermission();
if (hasCameraPermission) {
cameraView.setVisibility(View.VISIBLE);
} else {
permissionsDelegate.requestCameraPermission();
}
frontFotoapparat = createFotoapparat(LensPosition.FRONT);
backFotoapparat = createFotoapparat(LensPosition.BACK);
fotoapparatSwitcher = FotoapparatSwitcher.withDefault(backFotoapparat);
View switchCameraButton = findViewById(R.id.switchCamera);
switchCameraButton.setVisibility(
canSwitchCameras()
? View.VISIBLE
: View.GONE
);
switchCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchCamera();
}
});
}
private boolean canSwitchCameras() {
return frontFotoapparat.isAvailable() == backFotoapparat.isAvailable();
}
private Fotoapparat createFotoapparat(LensPosition position) {
return Fotoapparat
.with(this)
.into(cameraView)
.lensPosition(lensPosition(position))
.frameProcessor(
FaceDetectorProcessor.with(this)
.listener(new FaceDetectorProcessor.OnFacesDetectedListener() {
@Override
public void onFacesDetected(List<Rectangle> faces) {
Log.d("&&&", "Detected faces: " + faces.size());
rectanglesView.setRectangles(faces);
}
})
.build()
)
.logger(loggers(
logcat(),
fileLogger(this)
))
.build();
}
private void switchCamera() {
if (fotoapparatSwitcher.getCurrentFotoapparat() == frontFotoapparat) {
fotoapparatSwitcher.switchTo(backFotoapparat);
} else {
fotoapparatSwitcher.switchTo(frontFotoapparat);
}
}
@Override
protected void onStart() {
super.onStart();
if (hasCameraPermission) {
fotoapparatSwitcher.start();
}
}
@Override
protected void onStop() {
super.onStop();
if (hasCameraPermission) {
fotoapparatSwitcher.stop();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,