在ImageView

时间:2016-12-08 05:47:21

标签: android canvas imageview paint face-detection

我有一个女性的形象。我使用FaceDetector找到她的眼睛点。现在我想用那些眼睛点在头发上添加头发图像。

我正在使用以下代码

从图库加载该图片
btnLoad.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, RQS_LOADIMAGE);
    }
});

在onActivityResult中,我正在检查面部坐标

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    try {
        InputStream inputStream = getContentResolver().openInputStream(data.getData());
        myBitmap = BitmapFactory.decodeStream(inputStream);
        inputStream.close();
        imgView.setImageBitmap(myBitmap);

        if (myBitmap == null) {
            Toast.makeText(MainActivity.this, "myBitmap == null", Toast.LENGTH_LONG).show();
        } else {
            detectFace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  

人脸检测方法

private void detectFace() {

    Paint myRectPaint = new Paint();
    myRectPaint.setStrokeWidth(5);
    myRectPaint.setColor(Color.RED);
    myRectPaint.setStyle(Paint.Style.STROKE);

    tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
    Canvas tempCanvas = new Canvas(tempBitmap);
    tempCanvas.drawBitmap(myBitmap, 0, 0, null);

    FaceDetector faceDetector = new FaceDetector.Builder(this)
            .setTrackingEnabled(true)
            .setLandmarkType(FaceDetector.ALL_LANDMARKS)
            .setMode(FaceDetector.ACCURATE_MODE)
            .setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
            .build();

    Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
    SparseArray<Face> faces = faceDetector.detect(frame);

    imgView.setImageDrawable(new BitmapDrawable(getResources(), drawOnFace(faces)));
}
  

使用以下代码获取眼睛坐标: -

private Bitmap drawOnFace(SparseArray<Face> faceArray) {

    tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(tempBitmap);
    canvas.drawBitmap(myBitmap, 0, 0, null);

    for (int i = 0; i < faceArray.size(); i++) {

        Face face = faceArray.get(i);

        for (Landmark landmark : face.getLandmarks()) {
            switch (landmark.getType()) {
                case Landmark.LEFT_EYE:
                    drawPoint(canvas, landmark.getPosition());
                    break;
                case Landmark.RIGHT_EYE:
                    drawPoint(canvas, landmark.getPosition());
                    break;
            }
        }
    }
    return tempBitmap;
}
  

使用以下代码在眼睛上画圈: -

private void drawPoint(Canvas canvas, PointF point) {

    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setStrokeWidth(8);
    paint.setStyle(Paint.Style.STROKE);

    float x = point.x;
    float y = point.y;

    canvas.drawCircle(x, y, 10, paint);
}

现在在DrawPoint方法中,我有眼睛坐标。我想用这些点将头发图像放在脸上。 我真的不知道接下来该做什么。感谢帮助伙伴。 提前谢谢

1 个答案:

答案 0 :(得分:0)

要将图像放在相机预览上,请使用此代码

 float left=0,top=0;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.mustache);
 //if you are in non activity then use context.getResources()
canvas.drawBitmap(bitmap,left,top,paint);