您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
Android中怎么实现动态人脸检测,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
第一步
我们首先来定义一个surfaceview 盖在我们Carmen使用的surfaceview上 进行对人脸范围的绘制
public class FindFaceView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder holder; private int mWidth; private int mHeight; private float eyesDistance; public FindFaceView(Context context, AttributeSet attrs) { super(context, attrs); holder = getHolder(); holder.addCallback(this); holder.setFormat(PixelFormat.TRANSPARENT); this.setZOrderOnTop(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { mWidth = width; mHeight = height; } @Override public void surfaceCreated(SurfaceHolder holder) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } public void drawRect(FaceDetector.Face[] faces, int numberOfFaceDetected) { Canvas canvas = holder.lockCanvas(); if (canvas != null) { Paint clipPaint = new Paint(); clipPaint.setAntiAlias(true); clipPaint.setStyle(Paint.Style.STROKE); clipPaint .setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawPaint(clipPaint); canvas.drawColor(getResources().getColor(color.transparent)); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.GREEN); paint.setStyle(Style.STROKE); paint.setStrokeWidth(5.0f); for (int i = 0; i < numberOfFaceDetected; i++) { Face face = faces[i]; PointF midPoint = new PointF(); // 获得两眼之间的中间点 face.getMidPoint(midPoint); // 获得两眼之间的距离 eyesDistance = face.eyesDistance(); // 换算出预览图片和屏幕显示区域的比例参数 float scale_x = mWidth / 500; float scale_y = mHeight / 600; Log.e("eyesDistance=", eyesDistance + ""); Log.e("midPoint.x=", midPoint.x + ""); Log.e("midPoint.y=", midPoint.y + ""); // 因为拍摄的相片跟实际显示的图像是镜像关系,所以在图片上获取的两眼中间点跟手机上显示的是相反方向 canvas.drawRect((int) (240 - midPoint.x - eyesDistance) * scale_x, (int) (midPoint.y * scale_y), (int) (240 - midPoint.x + eyesDistance) * scale_x, (int) (midPoint.y + 3 * eyesDistance) * scale_y, paint); } holder.unlockCanvasAndPost(canvas); } } }
重要的地方
1. holder = getHolder();获取surfaceholder与我们要绘制人脸范围的画布进行绑定Canvas canvas = holder.lockCanvas();这样我们就可以愉快的进行绘制了,当然前提是我们要拿到人脸的坐标位置。
2. 还有重要的一点,就是要让我们用来盖在Carema上的Surfaceview可以同名,并且设置起在视图树的层级为最高。
holder.setFormat(PixelFormat.TRANSPARENT); this.setZOrderOnTop(true);
第二步
就是我们对人脸进行检测了,当然前提是我们要获得帧图
public class FaceRecognitionDemoActivity extends Activity implements OnClickListener { private SurfaceView preview; private Camera camera; private Camera.Parameters parameters; private int orientionOfCamera;// 前置摄像头的安装角度 private int faceNumber;// 识别的人脸数 private FaceDetector.Face[] faces; private FindFaceView mFindFaceView; private ImageView iv_photo; private Button bt_camera; TextView mTV; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected void onStart() { super.onStart(); iv_photo = (ImageView) findViewById(R.id.iv_photo); bt_camera = (Button) findViewById(R.id.bt_camera); mTV = (TextView) findViewById(R.id.show_count); bt_camera.setOnClickListener(this); mFindFaceView = (FindFaceView) findViewById(R.id.my_preview); preview = (SurfaceView) findViewById(R.id.preview); // 设置缓冲类型(必不可少) preview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // 设置surface的分辨率 preview.getHolder().setFixedSize(176, 144); // 设置屏幕常亮(必不可少) preview.getHolder().setKeepScreenOn(true); preview.getHolder().addCallback(new SurfaceCallback()); } private final class MyPictureCallback implements PictureCallback { @Override public void onPictureTaken(byte[] data, Camera camera) { try { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); Matrix matrix = new Matrix(); matrix.setRotate(-90); Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap .getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); iv_photo.setImageBitmap(bmp); camera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } } private final class SurfaceCallback implements Callback { @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (camera != null) { parameters = camera.getParameters(); parameters.setPictureFormat(PixelFormat.JPEG); // 设置预览区域的大小 parameters.setPreviewSize(width, height); // 设置每秒钟预览帧数 parameters.setPreviewFrameRate(20); // 设置预览图片的大小 parameters.setPictureSize(width, height); parameters.setJpegQuality(80); } } @Override public void surfaceCreated(SurfaceHolder holder) { int cameraCount = 0; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); //设置相机的参数 for (int i = 0; i < cameraCount; i++) { Camera.getCameraInfo(i, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { try { camera = Camera.open(i); camera.setPreviewDisplay(holder); setCameraDisplayOrientation(i, camera); //最重要的设置 帧图的回调 camera.setPreviewCallback(new MyPreviewCallback()); camera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } } } @Override public void surfaceDestroyed(SurfaceHolder holder) { //记得释放,避免OOM和占用 if (camera != null) { camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera = null; } } } private class MyPreviewCallback implements PreviewCallback { @Override public void onPreviewFrame(byte[] data, Camera camera) { //这里需要注意,回调出来的data不是我们直接意义上的RGB图 而是YUV图,因此我们需要 //将YUV转化为bitmap再进行相应的人脸检测,同时注意必须使用RGB_565,才能进行人脸检测,其余无效 Camera.Size size = camera.getParameters().getPreviewSize(); YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvImage.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, baos); byte[] byteArray = baos.toByteArray(); detectionFaces(byteArray); } } /** * 检测人脸 * * @param data 预览的图像数据 */ private void detectionFaces(byte[] data) { BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap1 = BitmapFactory.decodeByteArray(data, 0, data.length, options); int width = bitmap1.getWidth(); int height = bitmap1.getHeight(); Matrix matrix = new Matrix(); Bitmap bitmap2 = null; FaceDetector detector = null; //设置各个角度的相机,这样我们的检测效果才是最好 switch (orientionOfCamera) { case 0: //初始化人脸检测(下同) detector = new FaceDetector(width, height, 10); matrix.postRotate(0.0f, width / 2, height / 2); // 以指定的宽度和高度创建一张可变的bitmap(图片格式必须是RGB_565,不然检测不到人脸) bitmap2 = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); break; case 90: detector = new FaceDetector(height, width, 1); matrix.postRotate(-270.0f, height / 2, width / 2); bitmap2 = Bitmap.createBitmap(height, width, Bitmap.Config.RGB_565); break; case 180: detector = new FaceDetector(width, height, 1); matrix.postRotate(-180.0f, width / 2, height / 2); bitmap2 = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); break; case 270: detector = new FaceDetector(height, width, 1); matrix.postRotate(-90.0f, height / 2, width / 2); bitmap2 = Bitmap.createBitmap(height, width, Bitmap.Config.RGB_565); break; } //设置支持的面数(最大支持检测多少人的脸 ,可以根据需要调整,不过需要与findFaces中的参数数值相同,否则会抛出异常) faces = new FaceDetector.Face[10]; Paint paint = new Paint(); paint.setDither(true); Canvas canvas = new Canvas(); canvas.setBitmap(bitmap2); canvas.setMatrix(matrix); // 将bitmap1画到bitmap2上(这里的偏移参数根据实际情况可能要修改) canvas.drawBitmap(bitmap1, 0, 0, paint); //这里通过向findFaces中传递帧图转化后的bitmap和最大检测的人脸数face,返回检测后的人脸数 faceNumber = detector.findFaces(bitmap2, faces); mTV.setText("facnumber----" + faceNumber); mTV.setTextColor(Color.RED); //这里就是我们的人脸识别,绘制识别后的人脸区域的类 if (faceNumber != 0) { mFindFaceView.setVisibility(View.VISIBLE); mFindFaceView.drawRect(faces, faceNumber); } else { mFindFaceView.setVisibility(View.GONE); } bitmap2.recycle(); bitmap1.recycle(); } /** * 设置相机的显示方向(这里必须这么设置,不然检测不到人脸) * * @param cameraId 相机ID(0是后置摄像头,1是前置摄像头) * @param camera 相机对象 */ private void setCameraDisplayOrientation(int cameraId, Camera camera) { Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int rotation = getWindowManager().getDefaultDisplay().getRotation(); int degree = 0; switch (rotation) { case Surface.ROTATION_0: degree = 0; break; case Surface.ROTATION_90: degree = 90; break; case Surface.ROTATION_180: degree = 180; break; case Surface.ROTATION_270: degree = 270; break; } orientionOfCamera = info.orientation; int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degree) % 360; result = (360 - result) % 360; } else { result = (info.orientation - degree + 360) % 360; } camera.setDisplayOrientation(result); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_camera: if (camera != null) { try { camera.takePicture(null, null, new MyPictureCallback()); } catch (Exception e) { e.printStackTrace(); } } break; } } }
关于Android中怎么实现动态人脸检测问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。