使用面部检测将xy位置设置为imageView

时间:2011-05-26 20:50:25

标签: android android-layout position imageview

我正在我的应用中检测到一张脸。我使用两个ImageView,第一个用于面部,第二个用于面具。我需要在计算的面部位置上设置遮罩位置,然后可以使用触摸事件移动遮罩。对于触摸事件,我使用了本教程:http://blahti.wordpress.com/2011/01/17/moving-views-part-2/

面具疤痕的方法可以返回面部位置。我尝试将XY位置设置为蒙版Imageview,但始终显示在0.0位置。在脸上。然后我可以将面具ImageView移到脸上。 “视图”是我的面具。理想情况下,我需要在面部位置初始化蒙版imageview。

PD:抱歉我的英语不好。

protected Bitmap draw(int mode) {

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.c10);

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Point position = new Point();

    xRatio = anchoCara * 1.0f / anchoCara;
    yRatio = altoCara * 1.0f / altoCara;


    Bitmap resizedBitmap = null;

    float factor = 9.6f;//9.6

    for (int i = 0; i < eyesMidPts.length; i++) {
        if (eyesMidPts[i] != null) {
            pOuterBullsEye.setStrokeWidth(eyesDistance[i] / 6);

            float newWidth = eyesDistance[i] * factor;
            float newHeight = eyesDistance[i] * factor;

            float scaleWidth = (newWidth) / width;
            float scaleHeight = (newHeight) / height;

            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);

            resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
                    height, matrix, true);

            position.set(
                            (int) ((eyesMidPts[i].x * xRatio) - (eyesDistance[i] * factor) ),//2
                            (int) ((eyesMidPts[i].y * yRatio) - (eyesDistance[i] * factor) ));

            Log.e("Face", "positio x : " + position.x);
            Log.e("Face", "positio y : " + position.y);


            Log.e("Face", "mascara width : " + resizedBitmap.getWidth());
            Log.e("Face", "mascara heigth : " + resizedBitmap.getHeight());
//              view.setBackgroundDrawable(new BitmapDrawable(resizedBitmap));
//              view.setAdjustViewBounds(true);
//              Log.i("Face", "positio x : " + view.getScrollX());
//              Log.i("Face", "positio y : " + view.getScrollY());
        }
    }
    return resizedBitmap;
}

1 个答案:

答案 0 :(得分:2)

最后我使用了下一个代码:

@Override
public void onGlobalLayout() {
    // TODO Auto-generated method stub
    faceDetect(MODO_VIEW);      
    Display m=getWindowManager().getDefaultDisplay();
    anchoPant=m.getWidth();
    Log.i("Face","separacion derecha: "+((anchoPant-cara.getWidth())/2)+"px" );
    MyAbsoluteLayout.LayoutParams mp=new MyAbsoluteLayout.LayoutParams(MyAbsoluteLayout.LayoutParams.WRAP_CONTENT, MyAbsoluteLayout.LayoutParams.WRAP_CONTENT, (anchoPant-cara.getWidth())/2, 0);
    cara.setLayoutParams(mp);

    //view.setBackgroundDrawable(new BitmapDrawable(draw(MODO_VIEW)));      
    view.setImageBitmap(draw(MODO_VIEW));       
    MyAbsoluteLayout.LayoutParams lp=new MyAbsoluteLayout.LayoutParams(MyAbsoluteLayout.LayoutParams.WRAP_CONTENT,MyAbsoluteLayout.LayoutParams.WRAP_CONTENT,position.x+((anchoPant-cara.getWidth())/2),position.y);
    view.setLayoutParams(lp);
    view.getViewTreeObserver().removeGlobalOnLayoutListener(this);

}

在我的问题中,该方法可以返回位置,然后在OnGlobalLayout()上使用MyAbsoluteLayout.LayoutParams,我可以毫无问题地设置XY位置。