FaceView.java revision d480002ccad05cf992c628c72884091c36cc654c
1/* 2 * Copyright (C) 2011 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 17package com.android.camera.ui; 18 19import com.android.camera.R; 20import com.android.camera.Util; 21 22import android.content.Context; 23import android.graphics.Canvas; 24import android.graphics.Matrix; 25import android.graphics.RectF; 26import android.graphics.drawable.Drawable; 27import android.hardware.Camera.Face; 28import android.util.AttributeSet; 29import android.util.Log; 30import android.view.View; 31 32public class FaceView extends View { 33 private final String TAG = "FaceView"; 34 private final boolean LOGV = false; 35 private int mDisplayOrientation; 36 private boolean mMirror; 37 private boolean mPause; 38 private Matrix mMatrix = new Matrix(); 39 private RectF mRect = new RectF(); 40 private Face[] mFaces; 41 private Drawable mFaceRect; 42 43 public FaceView(Context context, AttributeSet attrs) { 44 super(context, attrs); 45 mFaceRect = getResources().getDrawable(R.drawable.focus_focused); 46 } 47 48 public void setFaces(Face[] faces) { 49 if (LOGV) Log.v(TAG, "Num of faces=" + faces.length); 50 if (mPause) return; 51 mFaces = faces; 52 invalidate(); 53 } 54 55 public void setDisplayOrientation(int orientation) { 56 mDisplayOrientation = orientation; 57 if (LOGV) Log.v(TAG, "mDisplayOrientation=" + orientation); 58 } 59 60 public void setMirror(boolean mirror) { 61 mMirror = mirror; 62 if (LOGV) Log.v(TAG, "mMirror=" + mirror); 63 } 64 65 public boolean faceExists() { 66 return (mFaces != null && mFaces.length > 0); 67 } 68 69 public void clearFaces() { 70 mFaces = null; 71 invalidate(); 72 } 73 74 public void pause() { 75 mPause = true; 76 } 77 78 public void resume() { 79 mPause = false; 80 } 81 82 private void dumpRect(RectF rect, String msg) { 83 Log.v(TAG, msg + "=(" + rect.left + "," + rect.top 84 + "," + rect.right + "," + rect.bottom + ")"); 85 } 86 87 @Override 88 protected void onDraw(Canvas canvas) { 89 if (mFaces != null && mFaces.length > 0) { 90 // Prepare the matrix. 91 Util.prepareMatrix(mMatrix, mMirror, mDisplayOrientation, getWidth(), 92 getHeight()); 93 94 for (int i = 0; i < mFaces.length; i++) { 95 // Transform the coordinates. 96 mRect.set(mFaces[i].rect); 97 if (LOGV) dumpRect(mRect, "Original rect"); 98 mMatrix.mapRect(mRect); 99 if (LOGV) dumpRect(mRect, "Transformed rect"); 100 101 mFaceRect.setBounds(Math.round(mRect.left), Math.round(mRect.top), 102 Math.round(mRect.right), Math.round(mRect.bottom)); 103 mFaceRect.draw(canvas); 104 } 105 } 106 super.onDraw(canvas); 107 } 108} 109