Search code examples
androidandroid-studioandroid-cameraface-detection

Display Circle on Face Detected- Android


I have made video capturing and face detection application. I want to show a circle on detected Face and have following method in Myview class, checking it working:

  protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawCircle(50f,50f,50f, paint);

    }

I have Relative Layout with CameraPreview on it. Where can I call the MyView Class to get desired functionality?


Solution

  • You can display circle on face detected using canvas here is an Example

    import com.google.android.gms.vision.face.Face;
    
    /**
    * Created by echessa on 8/31/15.
    */
    public class CustomView extends View {
    
    private Bitmap mBitmap;
    private SparseArray<Face> mFaces;
    
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    /**
     * Sets the bitmap background and the associated face detections.
     */
    void setContent(Bitmap bitmap, SparseArray<Face> faces) {
        mBitmap = bitmap;
        mFaces = faces;
        invalidate();
    }
    
    /**
     * Draws the bitmap background and the associated face landmarks.
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if ((mBitmap != null) && (mFaces != null)) {
            double scale = drawBitmap(canvas);
            drawFaceRectangle(canvas, scale);
        }
    }
    
    /**
     * Draws the bitmap background, scaled to the device size.  Returns the scale for future use in
     * positioning the facial landmark graphics.
     */
    private double drawBitmap(Canvas canvas) {
        double viewWidth = canvas.getWidth();
        double viewHeight = canvas.getHeight();
        double imageWidth = mBitmap.getWidth();
        double imageHeight = mBitmap.getHeight();
        double scale = Math.min(viewWidth / imageWidth, viewHeight / imageHeight);
    
        Rect destBounds = new Rect(0, 0, (int)(imageWidth * scale), (int)(imageHeight * scale));
        canvas.drawBitmap(mBitmap, null, destBounds, null);
        return scale;
    }
    
    /**
     * Draws a rectangle around each detected face
     */
    private void drawFaceRectangle(Canvas canvas, double scale) {
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
    
        for (int i = 0; i < mFaces.size(); ++i) {
            Face face = mFaces.valueAt(i);
            canvas.drawRect((float)(face.getPosition().x * scale),
                (float)(face.getPosition().y * scale),
                (float)((face.getPosition().x + face.getWidth()) * scale),
                (float)((face.getPosition().y + face.getHeight()) * scale),
                paint);
        }
    }
    

    }

    For more information http://www.sitepoint.com/face-detection-in-android-with-google-play-services/