简体   繁体   中英

how to crop face from image in android

I want to crop face from image in android . After searching a lot in internet , I have come to know about this tutorial . I have an imageview .

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

When I tap this imageview I pick an image from Gallery . The code is as follows :

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 method , I am trying to crop face image according to the following 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();
                    } 

But this code is not able to crop face from image picked from Gallery . How can I crop face from image ? Any advice is of great help .

You can try Facedetector built in class

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM