title: Face detection & draw circle using Android Camera2 API
body: <p>Currently I am trying to convert Camera2.Face to actual view's rect in order to draw circle over the face detected by the Camera2 API.</p>
  
  <p>I am able to get number of faces and its data into Callback by below code:</p>
  
  <pre><code>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 &amp;&amp; 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);
      }
  }
  </code></pre>
  
  <p>I tried below code so far to convert Face rect to actual view co-ordinates(seems like it is not working):</p>
  
  <pre><code>/**
  * 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 + ") -&gt; " + 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 &gt; 0) {
          List&lt;Rect&gt; faceRects;
          faceRects = new ArrayList&lt;&gt;();
          for (int i = 0; i &lt; faces.length; i++) {
              Log.i("Test", "Activity face" + i + " bounds: " + faces[i].getBounds());
              if (faces[i].getScore() &gt; 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);
      }
  }
  </code></pre>