FaceView.java revision 41ecefdc167ca88d785e6a1d58b706cc7f03c2bd
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 Matrix mMatrix = new Matrix();
38    private RectF mRect = new RectF();
39    private Face[] mFaces;
40    private Drawable mFaceRect;
41
42    public FaceView(Context context, AttributeSet attrs) {
43        super(context, attrs);
44        mFaceRect = getResources().getDrawable(R.drawable.focus_focused);
45    }
46
47    public void setFaces(Face[] faces) {
48        if (LOGV) Log.v(TAG, "Num of faces=" + faces.length);
49        mFaces = faces;
50        invalidate();
51    }
52
53    public void setDisplayOrientation(int orientation) {
54        mDisplayOrientation = orientation;
55        if (LOGV) Log.v(TAG, "mDisplayOrientation=" + orientation);
56    }
57
58    public void setMirror(boolean mirror) {
59        mMirror = mirror;
60        if (LOGV) Log.v(TAG, "mMirror=" + mirror);
61    }
62
63    public boolean faceExists() {
64        return (mFaces != null && mFaces.length > 0);
65    }
66
67    public void clearFaces() {
68        mFaces = null;
69        invalidate();
70    }
71
72    private void dumpRect(RectF rect, String msg) {
73        Log.v(TAG, msg + "=(" + rect.left + "," + rect.top
74                + "," + rect.right + "," + rect.bottom + ")");
75    }
76
77    @Override
78    protected void onDraw(Canvas canvas) {
79        if (mFaces != null && mFaces.length > 0) {
80            // Prepare the matrix.
81            Util.prepareMatrix(mMatrix, mMirror, mDisplayOrientation, getWidth(),
82                    getHeight());
83
84            for (int i = 0; i < mFaces.length; i++) {
85                // Transform the coordinates.
86                mRect.set(mFaces[i].rect);
87                if (LOGV) dumpRect(mRect, "Original rect");
88                mMatrix.mapRect(mRect);
89                if (LOGV) dumpRect(mRect, "Transformed rect");
90
91                mFaceRect.setBounds(Math.round(mRect.left), Math.round(mRect.top),
92                        Math.round(mRect.right), Math.round(mRect.bottom));
93                mFaceRect.draw(canvas);
94            }
95        }
96        super.onDraw(canvas);
97    }
98}
99