4

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

下面是我的代码

   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);


            }


        }
    }
}

}

4

0 回答 0