• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps FaceDetector issue using gallery

bumdeal2

Lurker
Hey guys,

I have a program that successfully accesses a gallery, allows a user
to select a picture from that gallery, and display that pic to the
user. I have tried to add a facedetector into this program but some
issues have arised, the picture is still displayed to the user but
when I try to use canvas to draw a square around the face within a
picture it doesnt and the picture is shown without any square. Im not
sure whether its an issue with Identifying the face or drawing a
square around the face, the areas in blue are the code of interest and the code in read is a hort description of each method. Any help would be really appreciated and feedback. Thanks
for your time.

The code is as follows

Code:
public class Activity3 extends Activity {

       private static final int ACTIVITY_SELECT_IMAGE = 1;
       private static final int GALLERY_ID = Menu.FIRST + 1;
       public String mCurrentImagePath = null;
       public int imageWidth, imageHeight;
   public int numberOfFace = 5;
   public FaceDetector myFaceDetect;
   public FaceDetector.Face[] myFace;
   float myEyesDistance;
   int numberOfFaceDetected;
   Bitmap bitmap;
   public Canvas canvas;



       private ImageView mImageView;



         /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       mImageView = new ImageView(this);
       setContentView(mImageView);


}

   // the canvas is below


   [COLOR="blue"]public void onDraw(Canvas c) {
       // TODO Auto-generated method stub

                canvas.drawBitmap(bitmap, 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();
                 c.drawRect(
                   (int)(myMidPoint.x - myEyesDistance),
                   (int)(myMidPoint.y - myEyesDistance),
                   (int)(myMidPoint.x + myEyesDistance),
                   (int)(myMidPoint.y + myEyesDistance),
                   myPaint);

                }
   }
[/COLOR]

   [COLOR="red"] //adds a menu[/COLOR]

   @Override
       public boolean onCreateOptionsMenu(Menu menu) {
               super.onCreateOptionsMenu(menu);

               menu.add(0, GALLERY_ID, 0, "Gallery");
               return true;
       }




   //if gallery is chosen begin activity and allow user to select a pic from the gallery

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
       switch (item.getItemId()) {

           case GALLERY_ID:
               Intent galleryIntent = new Intent(Intent.ACTION_PICK,
               Images.Media.INTERNAL_CONTENT_URI);
               startActivityForResult(galleryIntent, ACTIVITY_SELECT_IMAGE);
               return true;
       }

       return super.onMenuItemSelected(featureId, item);
}


[COLOR="Red"]//when the user has selected an image and no error has been occurred retrieve the image detect faces within the image and display the image.[/COLOR]


@Override
public void onActivityResult(int requestCode, int resultCode, Intent
data) {

       if (requestCode == ACTIVITY_SELECT_IMAGE && resultCode == RESULT_OK)
{
               try {

                       Uri currImageURI = data.getData();
                       String[] proj = { Images.Media.DATA, Images.Media.ORIENTATION };
                       Cursor cursor = managedQuery(currImageURI, proj, null, null,null);
                       int columnIndex = cursor.getColumnIndex(proj[0]);
                       cursor.moveToFirst();
                       mCurrentImagePath = cursor.getString(columnIndex);


                     [COLOR="Lime"][COLOR="Blue"]  BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();

                       BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;


                       bitmap = BitmapFactory.decodeFile(mCurrentImagePath);
                       int width = bitmap.getWidth();
                       int height = bitmap.getHeight();


                       myFace = new FaceDetector.Face[numberOfFace];
                       myFaceDetect = new FaceDetector(width, height, numberOfFace);
                       numberOfFaceDetected = myFaceDetect.findFaces(bitmap, myFace);
                       canvas= new Canvas();

                   mImageView.setImageBitmap(bitmap);[/COLOR][/COLOR]





                   }

                catch (Exception e) {
               }

       }

}

  }
 
Back
Top Bottom