0

I found the demo code here: https://github.com/googlesamples/android-vision/blob/master/visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/FaceTrackerActivity.java

and my question is how to take picture when face detected and save it to device, and when we take 1st picture next picture will be take after 5s when face detected because we can't save to many picture to device.

markalex
  • 8,623
  • 2
  • 7
  • 32
Trung Thành
  • 11
  • 1
  • 3

1 Answers1

0

You have to add FaceDetectionListener in camera API then call startFaceDetection() method,

CameraFaceDetectionListener fDListener = new CameraFaceDetectionListener();
mCamera.setFaceDetectionListener(fDetectionListener);
mCamera.startFaceDetection();

Implement Camera.FaceDetectionListener, you receive the detected face in onFaceDetection override method,

private class MyFaceDetectionListener 
          implements Camera.FaceDetectionListener {

@Override
public void onFaceDetection(Face[] faces, Camera camera) {

    if (faces.length == 0) {
        Log.i(TAG, "No faces detected");
    } else if (faces.length > 0) {
        Log.i(TAG, "Faces Detected = " + 
              String.valueOf(faces.length));

        public List<Rect> faceRects;
        faceRects = new ArrayList<Rect>();

        for (int i=0; i<faces.length; i++) {
            int left = faces[i].rect.left;
            int right = faces[i].rect.right;
            int top = faces[i].rect.top;
            int bottom = faces[i].rect.bottom;
            Rect uRect = new Rect(left0, top0, right0, bottom0);
            faceRects.add(uRect);
        }

        // add function to draw rects on view/surface/canvas
    }
}

As per your case, new Handler().postDelayed(new Runnable,long seconds) take 2nd picture inside runnable after 5 seconds. Please let me know if you have any queries.

Jayaprakash
  • 101
  • 5
  • thanks, but problem is onFaceDectiion call many many time so if using Handler it will raise exception: "Can't create handler inside thread that has not called Looper.prepare()" – Trung Thành Mar 10 '17 at 07:30
  • Looper.prepare() method is used to create a queue in the thread. So jus mention like this, mHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message message) { // This is where you do your work in the UI thread. // Your worker tells you in the message what to do. } }; Otherwise, I Suggest for AsyncTask that works well for most things running in the background. Keep method in doInBackground once done onPostExecute will call to update UI. – Jayaprakash Mar 13 '17 at 07:56