0

Need help! I have this code. How can I draw 68 and see them on camera preview?

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        rgba = inputFrame.rgba();
       try {
            Bitmap bmp = matToBitmap(rgba);
           points = getLandmark(bmp, this, predictorPath); // getting 68 points

          drawPoints(bmp, points);

        } catch (Exception e) {
            Log.i(TAG, "bitmap error! " + e.getMessage());
        }
        return rgba;
    }

EDIT: Added this method, but nothing happens

public void drawPoints(Bitmap bitmap, List<Point> points) {

        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        float radius = 4f;

        // draw points
        for(Point point : points) {
            canvas.drawCircle(point.x, point.y, radius, paint);
        }
    }

2 Answers2

0

Try to use Canvas to draw something on the bitmap. Here you'll find more information about this.

Anton Potapov
  • 1,265
  • 8
  • 11
0

You can draw points on a Bitmap with the help of the Canvas class. An example:

public void drawPoints(Bitmap bitmap, List<Point> points) {
    // a canvas for drawing on the bitmap
    Canvas canvas = new Canvas(bitmap);
    // a paint to describe how points are drawn
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    float radius = 4f;

    // draw points
    for(Point point : points) {
        canvas.drawCircle(point.x, point.y, radius, paint);
    }
    // the bitmap has now been updated
}

This can be altered depending on how you receive the points and how you want the points to appear (size, color, shape etc.).
For live drawing you may want to cache the Paint object.

RobCo
  • 6,240
  • 2
  • 19
  • 26
  • I need live drawing. Can you help me, and explain to me how I can cache the Paint object. Now I add you method in my code, but nothing... – Sergey Rokitskiy Aug 03 '17 at 11:49
  • Well yes, you are drawing it in the Bitmap but not doing anything with it. It would have to be rendered in for example an ImageView on top of the preview surface. – RobCo Aug 03 '17 at 12:15
  • Alternatively, you could maybe draw directly in the Mat object of OpenCV. Perhaps the [circle](http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html#circle) method that should be somewhere in the Java api as well (I'm not sure in which class exactly, but it's some static method). You can drop the whole Bitmap then, but it also depends on how you render this in the first place. – RobCo Aug 03 '17 at 12:18