Home | History | Annotate | Download | only in devcamera
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.devcamera;
     17 
     18 import android.graphics.PointF;
     19 import android.graphics.RectF;
     20 import android.hardware.camera2.params.Face;
     21 
     22 /**
     23  *
     24  * Face coordinates.  Normalized 0 to 1, and in native sensor orientation, which so far seems to be
     25  * landscape.
     26  *
     27  */
     28 public class NormalizedFace {
     29     public RectF bounds;
     30     public PointF leftEye;
     31     public PointF rightEye;
     32     public PointF mouth;
     33 
     34     public NormalizedFace(Face face, int dX, int dY, int offX, int offY) {
     35         if (face.getLeftEyePosition() != null) {
     36             leftEye = new PointF();
     37             leftEye.x = (float) (face.getLeftEyePosition().x - offX) / dX;
     38             leftEye.y = (float) (face.getLeftEyePosition().y - offY) / dY;
     39         }
     40         if (face.getRightEyePosition() != null) {
     41             rightEye = new PointF();
     42             rightEye.x = (float) (face.getRightEyePosition().x - offX) / dX;
     43             rightEye.y = (float) (face.getRightEyePosition().y - offY) / dY;
     44         }
     45         if (face.getMouthPosition() != null) {
     46             mouth = new PointF();
     47             mouth.x = (float) (face.getMouthPosition().x - offX) / dX;
     48             mouth.y = (float) (face.getMouthPosition().y - offY) / dY;
     49         }
     50         if (face.getBounds() != null) {
     51             bounds = new RectF();
     52             bounds.left = (float) (face.getBounds().left - offX) / dX;
     53             bounds.top = (float) (face.getBounds().top - offY) / dY;
     54             bounds.right = (float) (face.getBounds().right - offX) / dX;
     55             bounds.bottom = (float) (face.getBounds().bottom - offY) / dY;
     56         }
     57     }
     58 
     59     public void mirrorInX() {
     60         if (leftEye != null) {
     61             leftEye.x = 1f - leftEye.x;
     62         }
     63         if (rightEye != null) {
     64             rightEye.x = 1f - rightEye.x;
     65         }
     66         if (mouth != null) {
     67             mouth.x = 1f - mouth.x;
     68         }
     69         float oldLeft = bounds.left;
     70         bounds.left = 1f - bounds.right;
     71         bounds.right = 1f - oldLeft;
     72     }
     73 
     74     /**
     75      * Typically required for front camera
     76      */
     77     public void mirrorInY() {
     78         if (leftEye != null) {
     79             leftEye.y = 1f - leftEye.y;
     80         }
     81         if (rightEye != null) {
     82             rightEye.y = 1f - rightEye.y;
     83         }
     84         if (mouth != null) {
     85             mouth.y = 1f - mouth.y;
     86         }
     87         float oldTop = bounds.top;
     88         bounds.top = 1f - bounds.bottom;
     89         bounds.bottom = 1f - oldTop;
     90     }
     91 }
     92