我正在尝试获取左眼和右眼的坐标,并从左眼到右眼画一条线,但返回的坐标为空。我正在使用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
阅读 Android 参考:"这是一个可选字段,可能并非在所有设备上都受支持。如果不支持,则该值将始终设置为 null。
我已经在几台设备上进行了测试(Nexus 4和5,三星Galaxy S4和S5,Sony Experia,HTC One等),但从未工作过。对于谷歌的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);
}
}