使用faceDetection将xy位置设置为imageView



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

面具缩放的方法可以返回面部位置。我尝试将XY位置设置为遮罩Imageview,但始终显示在位置0.0。脸上。然后我可以在脸上移动遮罩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;
}

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

@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位置。

最新更新