歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android人臉檢測類FaceDetector

根據文檔描述,輸入圖片必須為Bitmap RGB565格式。
Android人臉的檢測方法是用雙眼來檢測人臉的位置,也就是說無法檢測到嘴、側臉等,雙眼必須同時可見,並且眼鏡會影響檢測的效果。
實際上,FaceDetector檢測到的並不是人的全臉,而只是雙眼。
實際測試中,發現圖片太小的話檢測不到人臉,試驗中使用小於100x100的圖片檢測不到人臉,但是由於Android內存有限,圖片太大的話,會出現無法加載圖片的異常。
檢測到的人臉存放到FaceDetector.Face類中,該類無法再擴展。從該類可以獲取到人眼的中心位置和雙眼之間的具體。  

package name.feisky.face;

 

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.PointF;

import android.media.FaceDetector;

import android.media.FaceDetector.Face;

import android.os.Bundle;

import android.view.View;

 

public class AndroidFaceDetector extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(new myView(this));

    }

 

    private class myView extends View{

 

        private int imageWidth, imageHeight;

        private int numberOfFace = 5;

        private FaceDetector myFaceDetect;

        private FaceDetector.Face[] myFace;

        float myEyesDistance;

        int numberOfFaceDetected;

 

        Bitmap myBitmap;

 

        public myView(Context context) {

            super(context);

            BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();

            BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;

            myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pp, BitmapFactoryOptionsbfo);

            imageWidth = myBitmap.getWidth();

            imageHeight = myBitmap.getHeight();

            myFace = new FaceDetector.Face[numberOfFace];

            myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);

            numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);

        }

 

        protected void onDraw(Canvas canvas) {

            // TODO Auto-generated method stub

 

            canvas.drawBitmap(myBitmap, 0, 0, null);

 

            Paint myPaint = new Paint();

            myPaint.setColor(Color.GREEN);

            myPaint.setStyle(Paint.Style.STROKE);

            myPaint.setStrokeWidth(3);

 

            for(int i=0; i < numberOfFaceDetected; i++)

            {

                Face face = myFace[i];

                PointF myMidPoint = new PointF();

                face.getMidPoint(myMidPoint);

                myEyesDistance = face.eyesDistance();

                canvas.drawRect(

                        (int)(myMidPoint.x - myEyesDistance),

                        (int)(myMidPoint.y - myEyesDistance),

                        (int)(myMidPoint.x + myEyesDistance),

                        (int)(myMidPoint.y + myEyesDistance),

                        myPaint);

            }

        }

    }

}

Copyright © Linux教程網 All Rights Reserved