1 /* 2 * Copyright (C) 2009 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 17 package android.media.cts; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.BitmapFactory; 22 import android.graphics.Canvas; 23 import android.graphics.Color; 24 import android.graphics.Paint; 25 import android.graphics.PointF; 26 import android.graphics.Rect; 27 import android.media.FaceDetector; 28 import android.media.FaceDetector.Face; 29 import android.view.View; 30 31 import java.util.ArrayList; 32 33 public class FaceView extends View { 34 private static final int NUM_FACES = 10; 35 private FaceDetector mFaceDetector; 36 private Face[] mAllFaces = new Face[NUM_FACES]; 37 private Bitmap mSourceImage; 38 private Paint mTmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 39 private Paint mPOuterBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG); 40 private Paint mPInnerBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG); 41 private int mPicWidth; 42 private int mPicHeight; 43 44 public ArrayList<Face> detectedFaces = new ArrayList<Face>(); 45 46 public FaceView(Context context, int resId) { 47 super(context); 48 49 mPInnerBullsEye.setStyle(Paint.Style.FILL); 50 mPInnerBullsEye.setColor(Color.RED); 51 52 mPOuterBullsEye.setStyle(Paint.Style.STROKE); 53 mPOuterBullsEye.setColor(Color.RED); 54 55 mTmpPaint.setStyle(Paint.Style.STROKE); 56 mTmpPaint.setTextAlign(Paint.Align.CENTER); 57 58 BitmapFactory.Options bfo = new BitmapFactory.Options(); 59 bfo.inPreferredConfig = Bitmap.Config.RGB_565; 60 bfo.inScaled = false; 61 bfo.inDither = false; 62 63 mSourceImage = BitmapFactory.decodeResource(getResources(), resId, bfo); 64 65 mPicWidth = mSourceImage.getWidth(); 66 mPicHeight = mSourceImage.getHeight(); 67 68 mFaceDetector = new FaceDetector(mPicWidth, mPicHeight, NUM_FACES); 69 int numFaces = mFaceDetector.findFaces(mSourceImage, mAllFaces); 70 71 for (int i = 0; i < numFaces; i++) { 72 detectedFaces.add(mAllFaces[i]); 73 } 74 } 75 76 @Override 77 protected void onDraw(Canvas canvas) { 78 float scale = Math.min((float) getWidth() / mPicWidth, (float) getHeight() / mPicHeight); 79 Rect scaledRect = new Rect(0, 0, mPicWidth, mPicHeight); 80 scaledRect.scale(scale); 81 canvas.drawBitmap(mSourceImage, null, scaledRect, mTmpPaint); 82 for (Face face : detectedFaces) { 83 PointF eyesMP = new PointF(); 84 face.getMidPoint(eyesMP); 85 float centerX = eyesMP.x * scale; 86 float centerY = eyesMP.y * scale; 87 float eyesDistance = face.eyesDistance() * scale; 88 mPOuterBullsEye.setStrokeWidth(eyesDistance / 6); 89 canvas.drawCircle(centerX, centerY, eyesDistance / 2, mPOuterBullsEye); 90 canvas.drawCircle(centerX, centerY, eyesDistance / 6, mPInnerBullsEye); 91 } 92 } 93 } 94