Skip to content

Example of using Face Detector

Husayn Hakeem edited this page Mar 18, 2019 · 1 revision

Your xml layout:

<io.fotoapparat.view.CameraView
    android:id="@+id/cameraView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--Optionally add tap to focus-->
    <io.fotoapparat.view.FocusView
        android:id="@+id/focusView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <husaynhakeem.io.facedetector.FaceBoundsOverlay
        android:id="@+id/facesBoundsOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</io.fotoapparat.view.CameraView>

Your Java file:

FaceBoundsOverlay faceBoundsOverlay = findViewById(R.id.facesBoundsOverlay);
husaynhakeem.io.facedetector.FaceDetector faceDetector = new husaynhakeem.io.facedetector.FaceDetector(faceBoundsOverlay);

cameraView = findViewById(R.id.cameraView);
focusView = findViewById(R.id.focusView); 

fotoapparat = Fotoapparat
                .with(this)
                .into(cameraView)
                .frameProcessor(new Custom2FrameProcessor())
                .focusView(focusView)
               ...
   
private class Custom2FrameProcessor implements FrameProcessor {
    @Override
    public void process(@NonNull Frame frame) {
        husaynhakeem.io.facedetector.models.Frame frame1 = new husaynhakeem.io.facedetector.models.Frame(
            frame.getImage(),
            frame.getRotation(),
            new Size(frame.getSize().width, frame.getSize().height),
            17,
            false);

        faceDetector.process(frame1);
    }
}
Clone this wiki locally