我正确地检测到脸部,但我注意到即使检测到的脸部完全静止,边界框的坐标也会受到微小变化的影响。我想知道这是正常行为还是我做错了什么。我正在使用两个TextureView来显示相机预览和面部检测覆盖。VIDEO
我的代码示例:
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
cameraTextureView.setSurfaceTextureListener(null);
try {
if (camera == null) {
camera = Camera.open(CID);
}
//camera parameters init .... code ...code....
camera.setPreviewCallback(new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
final byte[] frame = data;
camera.setFaceDetectionListener(new Camera.FaceDetectionListener() {
@Override
public void onFaceDetection(Camera.Face[] faces, Camera c2) {
if (faces.length > 0) {
Face face = faces[0];
Canvas canvas = cameraOverlay.lockCanvas(null);
if (canvas == null) return;
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
RectF bounds = new RectF(face.rect.left, face.rect.top, face.rect.right, face.rect.bottom);
canvas.drawRect(bounds, faceBoxPaint);
/*START - convert driver coordinates to View coordinates in pixels*/
matrix.setScale(-1, 1); // for front facing camera (matrix.setScale(1, 1); otherwise)
matrix.postRotate(displayOrientation);
// Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
// UI coordinates range from (0, 0) to (width, height).
matrix.postScale(cameraPrevWidthBox / 2000f, cameraPrevHeightBox / 2000f);
matrix.postTranslate(cameraPrevWidthBox / 2f, cameraPrevHeightBox / 2f);
matrix.mapRect(bounds);
//END
cameraOverlay.unlockCanvasAndPost(canvas);
}
}
});
}
});
camera.setPreviewTexture(surface);
camera.startPreview();
camera.startFaceDetection();
} catch (Exception ioe) {
// ~~
}
}
我认为这可能与自动对焦或稳定功能有关,但显然情况并非如此。
我在带有Android 7.0的Samsung S7上运行我的代码。