Capturing camera frame in android after face detection

风格不统一 提交于 2019-12-05 05:55:10

I could not figure out a direct way to save the camera frame within FaceDetectionListener. Therefore, for my application, I changed the way in which I was handling the camera preview data. I used the PreviewCallback interface of Camera class and implemented the logic in onPreviewFrame method of the interface. The outline of implementation is as follows:

class SaveFaceFrames extends Activity implements Camera.PreviewCallback, Camera.FaceDetectionListener {

    boolean lock = false; 

    public void onPreviewFrame(byte[] data, Camera camera) {
        ...
        if(lock) {
            Camera.Parameters parameters = camera.getParameters();
            Camera.Size size = parameters.getPreviewSize();
            YuvImage image = new YuvImage(data, parameters.getPreviewFormat(), size.width, size.height, null);
            ByteArrayOutputStream outstr = new ByteArrayOutputStream();
            image.compressToJpeg(new Rect(0, 0, image.getWidth(), image.getHeight()), 100, outstr);
            Bitmap bmp = BitmapFactory.decodeByteArray(outstr.toByteArray(), 0, outstr.size());
            lock = false;
        }
    }

    public void onFaceDetection(Camera.Face[] faces, Camera camera) {
        ...
        if(!lock) {
            if(faces.length() != 0) lock = true;
        }
    }
}

This is not an ideal solution, but it worked in my case. There are third party libraries which can be used in these scenarios. One library which I have used and works very well is Qualcomm's Snapdragon SDK. I hope someone finds this useful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!