简体   繁体   中英

How to Crop only face from an image in android?

I got a requirement to display only face from an image. But, I am trying to use only android native methods to implement this.

I have gone through the following link

Crop Image with face detection in android

But, the code mentioned in the above link is not working for all images.

Please guide me if anyone has already got the same requirement.

Thanks.

A face detection algorithm is never (and will never be) accurate. Even humans sometimes see the face of jezus in a toast. How can you expect computers to detect a face 100% correctly, if even the reference implementation (the human brain) contains bugs.

You should include a fallback scenario, when there is no (or more as one) face detected. Or do some quality check on the pictures before they are used.

You can use this rounded rectangle code with bigger corner radius to achieve a circle ( recipe by Romain Guy ):

BitmapShader shader;
shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);

RectF rect = new RectF(0.0f, 0.0f, width, height);

// rect contains the bounds of the shape
// radius is the radius in pixels of the rounded corners
// paint contains the shader that will texture the shape
canvas.drawRoundRect(rect, radius, radius, paint);

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