开发者

Set xy position to a imageView using faceDetection

开发者 https://www.devze.com 2023-03-08 12:20 出处:网络
I am detecting a face in my app. I use two ImageView, the first for the face and the second for a mask. I need set the mask position over the calculated face position and then can move the mask with t

I am detecting a face in my app. I use two ImageView, the first for the face and the second for a mask. I need set the mask position over the calculated face position and then can move the mask with touch event. For the touch event I did use of this tutorial: http://blahti.wordpress.com/2011/01/17/moving-views-part-2/

The method for mask scalling can return the face position. I tried set the XY position to the mask Imageview but ever is showed in the position 0.0. over the face. Then I can move the mask ImageView over the face. "view" is my mask. Ideally I need initialize the mask imageview over the face position.

PD: Sorry my english is bad.

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


Finally I did using the next code:

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

}

In my question, the method can return the position, Then using MyAbsoluteLayout.LayoutParams over OnGlobalLayout() I can set XY Position without problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消