简体   繁体   English

如何从Android中的图像裁剪脸

[英]how to crop face from image in android

I want to crop face from image in android . 我想从android中的图像裁剪脸部。 After searching a lot in internet , I have come to know about this tutorial . 在Internet上搜索了很多之后, 我才知道了本教程。 I have an imageview . 我有一个imageview。

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

When I tap this imageview I pick an image from Gallery . 当我点击此图像视图时,我从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 : 在onActivityResult方法中,我尝试根据以下代码裁剪人脸图像:

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 . 但是,此代码无法从Gallery获得的图像中裁剪脸部。 How can I crop face from image ? 如何从图像中裁剪脸部? Any advice is of great help . 任何建议都会有很大帮助。

You can try Facedetector built in class 您可以尝试在类中内置的Facedetector

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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