Vinay Gaba
Vinay Gaba

Reputation: 1214

Face Detection in Android without user interaction

I want to detect the numbers of faces in the front camera frame. I can detect the face once I get the image using this :http://www.developer.com/ws/android/programming/face-detection-with-android-apis.html. But I don't know how to capture an image using the front camera every 30 seconds without an user interaction.Can someone please help me?

Upvotes: 8

Views: 4908

Answers (3)

Martin Cazares
Martin Cazares

Reputation: 13705

You can create manually a SurfaceView and preview camera on it as follows:

//First get a reference to the SurfaceView displaying the camera preview
cameraSurface = (SurfaceView) findViewById(R.id.cameraSurface);
cameraSurfaceHolder = cameraSurface.getHolder();
cameraSurfaceHolder.addCallback(cameraSurfaceHolderCallbacks);
.
.
.
private SurfaceHolder.Callback cameraSurfaceHolderCallbacks = new SurfaceHolder.Callback() {

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if(mCamera == null)return;
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        try {
            mCamera = Camera.open();
            mCamera.setPreviewDisplay(holder);
        } catch (Exception exception) {
            if(mCamera == null)return;
            mCamera.release();
            mCamera = null; 
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Parameters cameraParameters = mCamera.getParameters();
        cameraParameters.setPreviewSize(320, 240);
        cameraParameters.setPictureSize(320, 240);
        int avrgExposure = (cameraParameters.getMinExposureCompensation() + cameraParameters.getMaxExposureCompensation())/2;
        cameraParameters.setExposureCompensation(avrgExposure);
        mCamera.setParameters(cameraParameters);
        mCamera.startPreview();
        mCamera.takePicture(null, null, mPictureCallback);
    }
};

Do not forget to add the proper camera permission in your manifest:

<uses-permission android:name="android.permission.CAMERA"/>

And finally if you are using an Android 4.0 device or above you can use the method:

mCamera.startFaceDetection();
.
.
.
private FaceDetectionListener faceDetectionListener = new FaceDetectionListener() {

    @Override
    public void onFaceDetection(Face[] faces, Camera camera) {
        //Faces have been detected...
    }
};
.
.
.
mCamera.setFaceDetectionListener(faceDetectionListener);

You can go to this post which explains everything related to that specific functionality and even provides a functional Android Source Code that you can download to do it yourself.

Regards!

Upvotes: 1

Chintan Rathod
Chintan Rathod

Reputation: 26034

Following code will capture photo from your camera after every 5 secs.

if (TIMER_STARTED) {
    multishotTimer.cancel();
    multishotTimer.purge();
    TIMER_STARTED = false;
} else {
    multishotTimer = new Timer();
    multishotTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            TIMER_STARTED = true;
            Camera camera = surfaceView.getCamera();
            camera.takePicture(null, null,
                new HandlePictureStorage());
        }
    }, 1000, 5000L);
}

Here, TIMER_STARTED is boolean which indicate whether timer is running or not.

Following is code for HandlePictureStorage

private class HandlePictureStorage implements PictureCallback {
    @Override
    public void onPictureTaken(byte[] picture, final Camera camera) {
    //do something when picture is captured...
    }
}

Upvotes: 4

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72321

You should be scheduling an RTC_WAKEUP Alarm using the AlarmManager, at every 30 seconds, set a PendingIntent to the Alarm to launch a Service and inside the Service you should access the Camera to capture the image.

You should probably look at this post : Open/Run camera from a background Service.

Upvotes: -1

Related Questions