从位图android实现人脸检测

时间:2014-01-03 10:24:55

标签: java android bitmap face-detection

  • 人脸检测

我有一个位图想要从java中的位图检测到面部。任何人都有想法从图像中获取面孔。

3 个答案:

答案 0 :(得分:4)

您可以尝试使用此代码在位图图像中检测到的每个面周围绘制一个矩形。

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;

public class FaceDetection extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(new myView(this));
 }

 private class myView extends View {

  private int imageWidth, imageHeight;
  private int numberOfFace = 5;
  private FaceDetector myFaceDetect;
  private FaceDetector.Face[] myFace;
  float myEyesDistance;
  int numberOfFaceDetected;

  Bitmap myBitmap;

  public myView(Context context) {
   super(context);

   BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
   BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
   myBitmap = BitmapFactory.decodeResource(getResources(),
     R.drawable.jennifer_lopez, BitmapFactoryOptionsbfo);
   imageWidth = myBitmap.getWidth();
   imageHeight = myBitmap.getHeight();
   myFace = new FaceDetector.Face[numberOfFace];
   myFaceDetect = new FaceDetector(imageWidth, imageHeight,
     numberOfFace);
   numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);

  }

  @Override
  protected void onDraw(Canvas canvas) {

   canvas.drawBitmap(myBitmap, 0, 0, null);

   Paint myPaint = new Paint();
   myPaint.setColor(Color.GREEN);
   myPaint.setStyle(Paint.Style.STROKE);
   myPaint.setStrokeWidth(3);

   for (int i = 0; i < numberOfFaceDetected; i++) {
    Face face = myFace[i];
    PointF myMidPoint = new PointF();
    face.getMidPoint(myMidPoint);
    myEyesDistance = face.eyesDistance();

    canvas.drawRect((int) (myMidPoint.x - myEyesDistance * 2),
      (int) (myMidPoint.y - myEyesDistance * 2),
      (int) (myMidPoint.x + myEyesDistance * 2),
      (int) (myMidPoint.y + myEyesDistance * 2), myPaint);
   }
  }
 }

}

答案 1 :(得分:0)

您在Android SDK中支持此功能。您应该查看this

答案 2 :(得分:0)

您可以使用以下代码获取面部位图

私人位图cropBitmap1(位图bmp2,脸部脸部)         {

        Matrix mat = new Matrix();
        int x = (int) face.getPosition().x/2;
        int y = (int) face.getPosition().y/2;
        int width = (int) face.getWidth() / 2;
        int height = (int) face.getHeight() / 2;

        Bitmap bmOverlay = Bitmap.createBitmap(bmp2, x , y,width,height,mat,true);
        return bmOverlay;
    }