From bf4ae898f0b9a725ccdc831cca1ad58258ad3c38 Mon Sep 17 00:00:00 2001 From: Sindhu Somasundaram <56774226+sindhuvahinis@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:09:43 -0700 Subject: [PATCH] add drawMarks to android BitMapImageFactory (#3460) * add drawMarks to android BitMapImageFactory * change signature of drawBoundingBoxes --- .../djl/android/core/BitmapImageFactory.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/android/core/src/main/java/ai/djl/android/core/BitmapImageFactory.java b/android/core/src/main/java/ai/djl/android/core/BitmapImageFactory.java index c208b6ae431..8142edfa5af 100644 --- a/android/core/src/main/java/ai/djl/android/core/BitmapImageFactory.java +++ b/android/core/src/main/java/ai/djl/android/core/BitmapImageFactory.java @@ -25,6 +25,7 @@ import ai.djl.modality.cv.output.DetectedObjects; import ai.djl.modality.cv.output.Joints; import ai.djl.modality.cv.output.Mask; +import ai.djl.modality.cv.output.Point; import ai.djl.modality.cv.output.Rectangle; import ai.djl.modality.cv.util.NDImageUtils; import ai.djl.ndarray.NDArray; @@ -241,7 +242,7 @@ public List findBoundingBoxes() { /** {@inheritDoc} */ @Override - public void drawBoundingBoxes(DetectedObjects detections) { + public void drawBoundingBoxes(DetectedObjects detections, float opacity) { Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(mutableBitmap); // set the paint configure @@ -282,6 +283,29 @@ public void drawBoundingBoxes(DetectedObjects detections) { oldBitmap.recycle(); } + /** {@inheritDoc} */ + @Override + public void drawMarks(List points, int radius) { + Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); + Canvas canvas = new Canvas(mutableBitmap); + + // set the paint configure + Paint paint = new Paint(); + paint.setStrokeWidth(2); + paint.setStyle(Paint.Style.FILL); + paint.setAntiAlias(true); + + paint.setColor(darker(randomColor())); + for (Point point : points) { + int[][] star = createStar(point, radius); + drawPolygon(canvas, paint, star); + } + + Bitmap oldBitmap = bitmap; + bitmap = mutableBitmap; + oldBitmap.recycle(); + } + /** {@inheritDoc} */ @Override public void drawJoints(Joints joints) { @@ -395,5 +419,15 @@ private void drawMask(Bitmap image, Mask mask) { Canvas canvas = new Canvas(image); canvas.drawBitmap(maskedImage, x, y, null); } + + private void drawPolygon(Canvas canvas, Paint paint, int[][] polygon) { + android.graphics.Path polygonPath = new android.graphics.Path(); + polygonPath.moveTo(polygon[0][0], polygon[1][0]); + for (int i = 1; i < polygon[0].length; i++) { + polygonPath.lineTo(polygon[0][i], polygon[1][i]); + } + polygonPath.close(); + canvas.drawPath(polygonPath, paint); + } } }