android人脸识别项目FaceDetector--------研究之二

项目已经完工了,有时间我就把这些用到内容详细记录一下,今天重点就是说下人脸识别框的第一种实现方式,通过FaceDetector类,这个我相信大家随便百度一下资料很多,首先实例化

FaceDetector mFaceDetector = new FaceDetector(myBitmap.getWidth(),myBitmap.getHeight(),numOfFaces);
numOfFaces = mFaceDetector.findFaces(myBitmap, mFace);

myBitmap必须是Bitmap.Config.RGB_565的

再通过已实例化的FindFaceView调用drawRects方法即可实现

/**
 * com.honest.facerecognition.view.FindFaceView
 * @author crayfish   <br/>
 * create at 2015-3-27 下午3:42:56
 */
public class FindFaceView extends SurfaceView implements SurfaceHolder.Callback{

	 protected SurfaceHolder sh;
	 private  SurfaceHolder mCameraSh;
	 private int mWidth;
	 private int mHeight;

	 public FindFaceView(Context context, AttributeSet attrs) {
		  super(context, attrs);
		  // TODO Auto-generated constructor stub
		  sh = getHolder();
		  sh.addCallback(this);
		  sh.setFormat(PixelFormat.TRANSPARENT);
		  setZOrderOnTop(true);
	 }

	 public void surfaceChanged(SurfaceHolder arg0, int arg1, int w, int h) {
	  // TODO Auto-generated method stub
		  mWidth = w;
		  mHeight = h;
	 }

	 public void surfaceCreated(SurfaceHolder arg0) {
	  // TODO Auto-generated method stub

	 }

	 public void surfaceDestroyed(SurfaceHolder arg0) {
	  // TODO Auto-generated method stub

	 }

	 void setCameraPreviewSurfaceHolder(SurfaceHolder  sh) {
		 mCameraSh = sh;
	 }
	 public int getFaceViewWidth() {
		 return mWidth;
	 }
	 public int getFaceViewHeight() {
		 return mHeight;
	 }

	 public void clearDraw() {
		  Canvas canvas = sh.lockCanvas();
		  Paint clipPaint = new Paint();
		  clipPaint.setAntiAlias(true);
		  clipPaint.setStyle(Paint.Style.STROKE);
		  clipPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
		  canvas.drawPaint(clipPaint);
		  sh.unlockCanvasAndPost(canvas);
	 }
     public void drawRects(Face[] mFaces) {
		  Canvas canvas = sh.lockCanvas();
	
		  Paint clipPaint = new Paint();
		  clipPaint.setAntiAlias(true);
		  clipPaint.setStyle(Paint.Style.STROKE);
		  clipPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
		  canvas.drawPaint(clipPaint);
		 
		  canvas.drawColor(Color.TRANSPARENT);
		  Paint p = new Paint();
		  p.setAntiAlias(true);
		  p.setColor(Color.YELLOW);
		  p.setStrokeWidth(3f);
		  p.setStyle(Style.STROKE);
	
//		  for (int i = 0; i < mFaces.length; i++) {
			  //  if (0 == i) {
			  //   p.setColor(Color.WHITE);
			  //  } else {
			  //   p.setColor(Color.GRAY);
			  //  }
//			   Face face = mFace[i];
//			   PointF myMidPoint = new PointF();
//			   face.getMidPoint(myMidPoint);
//			   mEyesDistance = face.eyesDistance();
//			   Log.i("Harrison", "i="+i+"("+myMidPoint.x+", "+myMidPoint.y+")");
//			   canvas.drawRect((int)(myMidPoint.x-mEyesDistance),
//		      (int)(myMidPoint.y-mEyesDistance),
//		      (int)(myMidPoint.x+mEyesDistance),
//		      (int)(myMidPoint.y+mEyesDistance),
//		       p);
			  RectF mRect = new RectF();
			  Matrix matrix = new Matrix();
			  FaceDectionUtil.PrepareMatrix(matrix, false, 90, getWidth(), getHeight());//后置false
			  
			  for(int i = 0;i < mFaces.length; i++ ){
				  mRect.set(mFaces[i].rect);
				  matrix.mapRect(mRect);
				  canvas.drawRect(mRect, p);
			  }
			  
//		  }
		  sh.unlockCanvasAndPost(canvas);
	  }

	//测试两个View是否错移
	 public void drawBitmap(Bitmap myBitmap) {
		  Canvas canvas = sh.lockCanvas();
		  canvas.drawBitmap(myBitmap, 0, 0, null);
		  sh.unlockCanvasAndPost(canvas);
		  // mImage.setImageBitmap(myBitmap);
		  // mImage.invalidate();
	 }
	 public void doDraw() {
		  Canvas canvas = sh.lockCanvas();
		  canvas.drawColor(Color.TRANSPARENT);// 这里是绘制背景
		  Paint p = new Paint(); // 笔触
		  p.setAntiAlias(true); // 反锯齿
		  p.setColor(Color.RED);
		  p.setStyle(Style.STROKE);
		  canvas.drawLine(mWidth/2 - 100, 0, mWidth/2 - 100, mHeight, p);
		  canvas.drawLine(mWidth/2 + 100, 0, mWidth/2 + 100, mHeight, p);
		  // ------------------------
		  // 画边框---------------------
		  Rect rec = canvas.getClipBounds();
		  rec.bottom--;
		  rec.right--;
		  p.setColor(Color.GRAY);
		  // 颜色
		  p.setStrokeWidth(5);
		  canvas.drawRect(rec, p);
		  // 提交绘制
		  sh.unlockCanvasAndPost(canvas);
	 }
}

    原文作者:Crayfishxu
    原文地址: https://blog.csdn.net/xujie1991618/article/details/44959461
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞