В настоящее время я пытаюсь преобразовать Camera2.Face в фактический вид rect, чтобы нарисовать круг по лицу, обнаруженному API Camera2.
Я могу получить количество граней и его данных в Callback по коду:
private CameraCaptureSession.CaptureCallback mCaptureCallback
= new CameraCaptureSession.CaptureCallback() {
private void process(CaptureResult result) {
Integer mode = result.get(CaptureResult.STATISTICS_FACE_DETECT_MODE);
Face [] faces = result.get(CaptureResult.STATISTICS_FACES);
if(faces != null && mode != null)
Log.e("tag", "faces : " + faces.length + " , mode : " + mode );
}
@Override
public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult) {
process(partialResult);
}
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
process(result);
}
}
Я пытался использовать код ниже, чтобы преобразовать Face rect в фактические координаты просмотра (похоже, что он не работает):
/**
* Callback from the CameraCaptureSession.CaptureCallback
*/
@Override
public void onFaceDetection(Face[] faces) {
if (mCameraView != null) {
setFaceDetectionMatrix();
setFaceDetectionLayout(faces);
}
}
/**
* This method gets the scaling values of the face in matrix
*/
private void setFaceDetectionMatrix() {
// Face Detection Matrix
mFaceDetectionMatrix = new Matrix();
// Need mirror for front camera.
boolean mirror = mCameraView.getFacing() == CameraView.FACING_FRONT;
mFaceDetectionMatrix.setScale(mirror ? -1 : 1, 1);
mFaceDetectionMatrix.postRotate(mCameraDisplayOrientation);
Rect activeArraySizeRect = mCameraView.getCameraCharacteristics().get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Log.i("Test", "activeArraySizeRect1: (" + activeArraySizeRect + ") -> " + activeArraySizeRect.width() + ", " + activeArraySizeRect.height());
Log.i("Test", "activeArraySizeRect2: " + cameraOverlayDrawingView.getWidth() + ", " + cameraOverlayDrawingView.getHeight());
float s1 = cameraOverlayDrawingView.getWidth() / activeArraySizeRect.width();
float s2 = cameraOverlayDrawingView.getHeight() / activeArraySizeRect.height();
mFaceDetectionMatrix.postScale(s1, s2);
mFaceDetectionMatrix.postTranslate(cameraOverlayDrawingView.getWidth() / 2, cameraOverlayDrawingView.getHeight() / 2);
}
/**
* This method set the matrix for translating rect
*/
private void setFaceDetectionLayout(Face[] faces) {
if (faces.length == 0) {
cameraOverlayDrawingView.setHaveFaces(false, null);
} else if (faces.length > 0) {
List<Rect> faceRects;
faceRects = new ArrayList<>();
for (int i = 0; i < faces.length; i++) {
Log.i("Test", "Activity face" + i + " bounds: " + faces[i].getBounds());
if (faces[i].getScore() > 50) {
int left = faces[i].getBounds().left;
int top = faces[i].getBounds().top;
int right = faces[i].getBounds().right;
int bottom = faces[i].getBounds().bottom;
Rect uRect = new Rect(left, top, right, bottom);
RectF rectF = new RectF(uRect);
mFaceDetectionMatrix.mapRect(rectF);
uRect.set((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom);
Log.i("Test", "Activity rect" + i + " bounds: " + uRect);
faceRects.add(uRect);
}
}
cameraOverlayDrawingView.setHaveFaces(true, faceRects);
}
}