从图像中检测脸部并从该照片中裁剪脸部

时间:2012-09-22 09:35:43

标签: android face-detection

我已经完成了编码,因为我现在成功地获得了矩形绘图,我想要裁剪那个​​矩形区域。如果有很多矩形(意味着很多面),用户可以选择其中一个面或矩形,那个矩形面应该被裁剪,任何身体都可以帮助我......

以下是我的代码

   class AndroidFaceDetector extends Activity {
public String path;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = this.getIntent().getExtras();
    path = bundle.getString("mypath");
    setContentView(new myView(this));

}

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);
        System.out.println("CONSTRUCTOR");
        System.out.println("path = "+path);
        if (path != null) {
            BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
            BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
            myBitmap = BitmapFactory.decodeFile(path, BitmapFactoryOptionsbfo);
            imageWidth = myBitmap.getWidth();
            imageHeight = myBitmap.getHeight();
            myFace = new FaceDetector.Face[numberOfFace];
            myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
            numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);

        } else {
            Toast.makeText(AndroidFaceDetector.this, "Please Try again", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        System.out.println("ON DRAW IS CALLED");
        if (myBitmap != null) {

            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), (int) (myMidPoint.y - myEyesDistance),
                        (int) (myMidPoint.x + myEyesDistance), (int) (myMidPoint.y + myEyesDistance), myPaint);


            }


        }
    }
}

}

0 个答案:

没有答案