2016-05-25 11 views
0

Ich möchte Gesicht von Bild in Android zu ernten. Nachdem ich viel im Internet gesucht habe, I have come to know about this tutorial . habe ich einen Imageview.wie Gesicht aus Bild in Android

iv1 = (ImageView) MainActivity.this.findViewById(R.id.img1); 

Wenn ich auf diese Bildansicht tippe, wähle ich ein Bild aus der Galerie. Der Code lautet wie folgt:

iv1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
       photoPickerIntent.setType("image/*"); 
       startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
       imgNo = 1; 

      } 
     }); 

In onActivityResult Methode, ich versuche zu Ernte Gesichtsbild nach dem folgenden Code:

if (resultCode == RESULT_OK && imgNo == 1) 
       { 
        selectedImage = imageReturnedIntent.getData(); 
        try { 
         imageStream = getContentResolver().openInputStream(
           selectedImage); 
        } catch (FileNotFoundException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        yourSelectedImage = BitmapFactory.decodeStream(imageStream); 

        // iv1.setImageBitmap(yourSelectedImage); 
         path1 = selectedImage.getPath(); 

         viewHeight = part2.getMeasuredHeight(); 
         viewWidth = part2.getMeasuredWidth(); 
         try { 

          Paint paint = new Paint(); 
          paint.setFilterBitmap(true); 
          Bitmap bitmapOrg = yourSelectedImage; 

          int targetWidth = bitmapOrg.getWidth(); 
          int targetHeight = bitmapOrg.getHeight(); 

          Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
            targetHeight, Bitmap.Config.ARGB_8888); 

          RectF rectf = new RectF(0, 0, viewWidth, viewHeight); 

          Canvas canvas = new Canvas(targetBitmap); 
          Path path = new Path(); 

          path.addRect(rectf, Path.Direction.CW); 
          canvas.clipPath(path); 

          canvas.drawBitmap(
            bitmapOrg, 
            new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg 
              .getHeight()), new Rect(0, 0, targetWidth, 
              targetHeight), paint); 

          Matrix matrix = new Matrix(); 
          matrix.postScale(1f, 1f); 

          BitmapFactory.Options bitmapFatoryOptions = new BitmapFactory.Options(); 
          bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.RGB_565; 

          bitmapOrg = yourSelectedImage; 

          myFace = new FaceDetector.Face[5]; 
          myFaceDetect = new FaceDetector(targetWidth, targetHeight, 
            5); 
          int numberOfFaceDetected = myFaceDetect.findFaces(
            bitmapOrg, myFace); 
          Bitmap resizedBitmap = null; 
          if (numberOfFaceDetected > 0) { 
           PointF myMidPoint = null; 
           Face face = myFace[0]; 
           myMidPoint = new PointF(); 
           face.getMidPoint(myMidPoint); 
           myEyesDistance = face.eyesDistance() + 20; 

           if (myMidPoint.x + viewWidth > targetWidth) { 
            while (myMidPoint.x + viewWidth > targetWidth) { 
             myMidPoint.x--; 
            } 
           } 
           if (myMidPoint.y + viewHeight > targetHeight) { 
            while (myMidPoint.y + viewHeight > targetHeight) { 
             myMidPoint.y--; 
            } 
           } 
           resizedBitmap = Bitmap.createBitmap(bitmapOrg, 
             (int) (myMidPoint.x - myEyesDistance), 
             (int) (myMidPoint.y - myEyesDistance), 
             viewWidth, viewHeight, matrix, true); 
          } else { 
           resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
             viewWidth, viewHeight, matrix, true); 
          } 
          /* convert Bitmap to resource */ 
          // Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 
          // 0, 
          // 0, viewWidth, viewHeight, matrix, true); 
          BitmapDrawable bd = new BitmapDrawable(resizedBitmap); 

          iv1.setImageBitmap(getCroppedBitmap(bd.getBitmap())); 


         } catch (Exception e) { 
          System.out.println("Error1 : " + e.getMessage() 
            + e.toString()); 
         } 
         iv1.invalidate(); 
        } 

Aber dieser Code ist zu Ernte Gesicht nicht in der Lage aus aufgenommenem Bild aus Galerie . Wie kann ich Gesicht von Bild beschneiden? Jeder Rat ist eine große Hilfe.

Antwort

1

können Sie versuchen, Facedetector in der Klasse gebaut

FaceDetector.Face[] face=new FaceDetector.Face[20]; 


    FaceDetector fd=new FaceDetector(200,200,2); 
    ImageView v=(ImageView)findViewById(R.id.imageView); 
    BitmapDrawable draw=(BitmapDrawable)v.getDrawable(); 

    fd.findFaces(draw.getBitmap(), face); 
    //Now face will hold array face image 
+0

I Facedetector in der Klasse gebaut bin mit –