然后,分别在 onResume 与 onPause 函数中完成人脸检测类对象的初始化和释放:
@Override public void onResume() { super.onResume(); startBackgroundThread(); mFaceDet = new FaceDet(); if (mTextureView.isAvailable()) { openCamera(mTextureView.getWidth(), mTextureView.getHeight()); } else { mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); } } @Override public void onPause() { closeCamera(); stopBackgroundThread(); if (mFaceDet != null) { mFaceDet.release(); } super.onPause(); }最后,在 TextureView 的回调函数 onSurfaceTextureUpdated 完成调用:
@Override public void onSurfaceTextureUpdated(SurfaceTexture texture) { if (!mIsDetecting) { Bitmap bp = mTextureView.getBitmap(); // 保证图片方向与预览方向一致 bp = Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), mTextureView.getTransform(null), true ); new detectAsync().execute(bp); } } 6 测试结果经测试,960x720的 bitmap 图片在华为手机(Android 6.0,8核1.2GHz,2G内存)上执行一次检测约耗时800~850ms。Demo 运行效果如下:
7 Demo 源码Github:FaceDetection
8. 参考https://github.com/tzutalin/dlib-android
https://github.com/gv22ga/dlib-face-recognition-android
https://blog.csdn.net/yanzi1225627/article/details/7934710
https://blog.csdn.net/hjimce/article/details/64127654