我在玩FaceDetectionListener
,我的目的是在眼睛周围画一个圆圈。我有一个SurfaceView
名为的对象preview
和一个SurfaceHolder
名为previewHolder
.
一切都很好,我可以检测到我SurfaceView
使用后置摄像头代表的人脸。我还想在左右眼周围画一些圆圈。我的drawCanvas
方法如下所示:
private void drawCanvas(float x, float y) {
Canvas canvas = previewHolder.lockCanvas();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
paint.setDither(true);
paint.setColor(0xFFFFFF00);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(3);
canvas.drawPoint(x, y, paint);
previewHolder.unlockCanvasAndPost(canvas);
}
此方法从以下位置调用FaceDetectionListener
:
@Override
public void onFaceDetection(Face[] faces, Camera camera) {
if (faces.length > 0) {
tv.setText(String.valueOf(faces.length) + " Face(s) Detected");
for(Face f : faces) {
int leftEyeXCoordinate = f.leftEye.x;
int leftEyeYCoordinate = f.leftEye.y;
drawCanvas(leftEyeXCoordinate, leftEyeYCoordinate);
int rightEyeXCoordinate = f.rightEye.x;
int rightEyeYCoordinate = f.rightEye.y;
drawCanvas(rightEyeXCoordinate, rightEyeYCoordinate);
}
}
else
tv.setText("No Faces Detected!");
}};
我不断收到 NullPointerException int leftEyeXCoordinate = f.leftEye.x;
我可以在相机四处移动时画点、矩形和其他东西吗?还是我必须先拍摄照片?
所以我的问题是:以前有没有人完成过这个并想告诉我我是怎么做到的?
谢谢!