2

我正在尝试获取 leftEye 和 rightEye 的坐标并从 leftEye 到 rightEye 画一条线,但返回的坐标为空。我正在使用 Nexus 4 来测试应用程序。Nexus 4 不支持这个功能??我可以在检测到的人脸周围画一个矩形,没有任何问题。作为参考,我附上了用于检测眼睛坐标的代码。

try{
         float x1 = detectedFaces[i].leftEye.x;
         float y1 = detectedFaces[i].leftEye.y;
         float x2 = detectedFaces[i].rightEye.y;
         float y2 = detectedFaces[i].rightEye.y;

         //Converting from driver coordinate to view coordinate
         float Xx1 = (x1+1000) * vWidth/2000;
         float Yy1  = (y1+1000) * vHeight/2000;
         float Xx2 = (x2+1000) * vWidth/2000;
         float Yy2  = (y2+1000) * vHeight/2000;

         canvas.drawLine(Xx1, Yy1, Xx2, Yy2, drawingPaint);
         }
         catch(Exception e){
             Log.e(TAG, "Error: " +e.getMessage());
         }

日志猫

11-15 16:37:52.895: E/Take_Picture(1304): Error: null
11-15 16:37:53.115: E/Take_Picture(1304): Error: null
11-15 16:37:53.286: E/Take_Picture(1304): Error: null
4

1 回答 1

1

阅读Android参考:“这是一个可选字段,可能并非所有设备都支持。如果不支持,该值将始终设置为null。”

我已经在几种设备(Nexus 4 和 5、三星 Galaxy S4 和 S5、索尼 Experia、HTC One 等)上进行了测试,但从未工作过。对于 Google 的 Android 开发团队来说,这可能是一个很好的问题。

您可以选择使用 OpenCv 库(http://opencv.org/platforms/android.html)。

另一种选择是在拍照后进行检测,而不是使用 FaceDetector 类进行实时检测,不确定您是否要实时进行检测。

看一个例子:

public class TutorialOnFaceDetect1 extends Activity {
private MyImageView mIV;
private Bitmap mFaceBitmap;
private int mFaceWidth = 200;
private int mFaceHeight = 200;   
private static final int MAX_FACES = 10;
private static String TAG = "TutorialOnFaceDetect";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mIV = new MyImageView(this);
    setContentView(mIV, new LayoutParams(LayoutParams.WRAP_CONTENT,    LayoutParams.WRAP_CONTENT));       

    // load the photo
    Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.face3); 
    mFaceBitmap = b.copy(Bitmap.Config.RGB_565, true); 
    b.recycle();

    mFaceWidth = mFaceBitmap.getWidth();
    mFaceHeight = mFaceBitmap.getHeight();  
    mIV.setImageBitmap(mFaceBitmap); 

    // perform face detection and set the feature points
    setFace();

    mIV.invalidate();
}

public void setFace() {
    FaceDetector fd;
    FaceDetector.Face [] faces = new FaceDetector.Face[MAX_FACES];
    PointF midpoint = new PointF();
    int [] fpx = null;
    int [] fpy = null;
    int count = 0;

    try {
        fd = new FaceDetector(mFaceWidth, mFaceHeight, MAX_FACES);        
        count = fd.findFaces(mFaceBitmap, faces);
    } catch (Exception e) {
        Log.e(TAG, "setFace(): " + e.toString());
        return;
    }

    // check if we detect any faces
    if (count > 0) {
        fpx = new int[count];
        fpy = new int[count];

        for (int i = 0; i < count; i++) { 
            try {                 
                faces[i].getMidPoint(midpoint);                  

                fpx[i] = (int)midpoint.x;
                fpy[i] = (int)midpoint.y;
            } catch (Exception e) { 
                Log.e(TAG, "setFace(): face " + i + ": " + e.toString());
            }            
        }      
    }

    mIV.setDisplayPoints(fpx, fpy, count, 0);
} 

}

于 2014-11-11T13:33:33.127 回答