百度现在功能较少——使用虹软,方便
虹软官网:https://ai.arcsoft.com.cn/ucenter/resource/build/index.html
申请应用,下载SDK即可
虹软已修改好的,可注册人脸,人脸验证成功——登陆的demo:https://download.csdn.net/download/meixi_android/18434756
1、首先去百度人脸识别创建应用
https://console.bce.baidu.com/ai/?_=1553067234412&fromai=1#/ai/face/client/index
2、下载SDK
https://download.csdn.net/download/meixi_android/11041930
3、导入module
4、人脸实时采集实现类
/*** 绘制人脸框。** @param model 追踪到的人脸*/private void showFrame(FaceFilter.TrackedModel model) {Canvas canvas = mTextureView.lockCanvas();if (canvas == null) {return;}// 清空canvascanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);if (model != null) {FaceInfo info = model.getInfo();model.getImageFrame().retain();RectF rectCenter = new RectF(info.mCenter_x - 2 - info.mWidth * 3 / 5,info.mCenter_y - 2 - info.mWidth * 3 / 5,info.mCenter_x + 2 + info.mWidth * 3 / 5,info.mCenter_y + 2 + info.mWidth * 3 / 5);previewView.mapFromOriginalRect(rectCenter);// 绘制框paint.setStrokeWidth(mRound);paint.setAntiAlias(true);canvas.drawRect(rectCenter, paint);if (model.meetCriteria()) {// 符合检测要求,绘制绿框paint.setColor(Color.GREEN);}mFrameIndex++;Log.d("lgq", "add face index is:" + mFrameIndex);if (mFrameIndex >= 10) {final Bitmap face = model.cropFace();// final Bitmap face =ImageUtil.bitmapFromArgb(model.getImageFrame());if (face != null) {int size = mList.size();// 释放一些,以防止太多if (size >= 6) {Bitmap bmp = mList.get(size - 6);if (bmp != null) {bmp.recycle();Log.d("lgq", "recycle size is:" + size);bmp = null;}}mList.add(face);Log.d("lgq", "add face ok");mHandler.postDelayed(scrollRunnable, 100);mFrameIndex = 0;faceDetectManager.stop();}}}mTextureView.unlockCanvasAndPost(canvas);}Runnable scrollRunnable = new Runnable() {@Overridepublic void run() {int count = mRecyAdapter.getItemCount();int curIndex = count - 1;mRecyclerview.scrollToPosition(curIndex);mRecyAdapter.setDatas(mList);
// mRecyclerview.invalidate();mRecyAdapter.notifyDataSetChanged();// Log.d("lgq", "in runnuable data size is:" + mList.size());}};/*** 初始化recycleView画截图得到的人脸图像*/private void initRecy() {mRecyAdapter = new RecyAdapter(this);mLayoutManager = new LinearLayoutManager(DetectActivity.this,LinearLayoutManager.HORIZONTAL, true);mRecyclerview.setLayoutManager(mLayoutManager);
// mRecyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL));mLayoutManager.setStackFromEnd(true);mRecyclerview.setAdapter(mRecyAdapter);}private void setCameraType(CameraImageSource cameraImageSource) {// TODO 选择使用前置摄像头cameraImageSource.getCameraControl().setCameraFacing(ICameraControl.CAMERA_FACING_FRONT);// TODO 选择使用usb摄像头
// cameraImageSource.getCameraControl().setCameraFacing(ICameraControl.CAMERA_USB);// 如果不设置,人脸框会镜像,显示不准// previewView.getTextureView().setScaleX(-1);// TODO 选择使用后置摄像头
// cameraImageSource.getCameraControl().setCameraFacing(ICameraControl.CAMERA_FACING_BACK);// previewView.getTextureView().setScaleX(-1);}
demo链接:https://download.csdn.net/download/meixi_android/11041930
在线回复bug:qq1085220040
运行效果
优化采集速度代码:
// 设置回调,回调人脸检测结果。
faceDetectManager.setOnFaceDetectListener((retCode, infos, frame) -> {LogPlus.e("###人脸检测结果:" + retCode + ",infos:" + GsonUtil.getInstance().parseObjToJsonStr(infos));// TODO 显示检测的图片。用于调试,如果人脸sdk检测的人脸需要朝上,可以通过该图片判断//final Bitmap bitmap = Bitmap.createBitmap(frame.getArgb(), frame.getWidth(), frame.getHeight(), Bitmap.Config.ARGB_8888);if (infos == null) {// null表示,没有人脸。LogPlus.e("###没有检测到人脸");setTips("没有检测到人脸");Bitmap faceBitmap = Bitmap.createBitmap(frame.getArgb(), frame.getWidth(), frame.getHeight(), Bitmap.Config.ARGB_8888);upload(faceBitmap);} else {/*有人脸*/setTips("人脸上传...");Bitmap faceBitmap = FaceCropper.getFace(frame.getArgb(), infos[0], frame.getWidth());upload(faceBitmap);}
});