怎样对相机返回的图片进行人脸检测?

波波淋淋头 发布于 2012/04/07 10:13
阅读 1K+
收藏 0
调用相机以后在onActivityResult中,利用bitmap = (Bitmap) bundle.get("data"); 得到了bitmap数据,怎样利用安卓自带的人脸检测函数,实现对相机返回的照片进行人脸检测呢?(用绿色方框标记出人脸,显示在手机界面上)。我是一只菜鸟,毕业设计急用,麻烦各位大神帮帮忙啊
加载中
0
fneg
fneg
看文档:http://developer.android.com/reference/android/media/FaceDetector.html
0
波波淋淋头

package zhen.face;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Activityone extends Activity {
  private Bitmap bitmap;
 protected void  onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         startActivityForResult(intent,1);
         setContentView(new myView(this)); 
 }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
   // TODO Auto-generated method stub
      menu.add(0,1,1,"推出");
      menu.add(0,2,2,"取消");
   return super.onCreateOptionsMenu(menu);
  }
     //当客户点击菜单的某一个选项时会调用该方法
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
   // TODO Auto-generated method stub
   if(item.getItemId()==1)
    finish();
   return super.onOptionsItemSelected(item);
  }
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
     Bundle bundle = data.getExtras();
       bitmap = (Bitmap) bundle.get("data"); //获取相机返回的数据,并转换为Bitmap图片格式
   }
  }
  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);
    // TODO Auto-generated constructor stub
        BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
              BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
              myBitmap=bitmap; 
              imageWidth = myBitmap.getWidth();
              imageHeight = myBitmap.getHeight();
              myFace = new FaceDetector.Face[numberOfFace];
              myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
              numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);
              System.out.println("传值");
          }    
          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);
                  }
             }
          }    
}
以上是我的代码,能帮我看看哪儿错了吗?大神帮帮忙吧

0
木有听禹
兄弟 你解决了嘛?我也想学习,能不能教我下
0
天蚕变
天蚕变

同问,楼主怎么解决的?? 预览视频的同时进行图片处理??我的qq 707798286 欢迎大家交流


返回顶部
顶部