Skip to content

Commit 390130e

Browse files
committed
update: add drawBoxes util
1 parent 0bc82d5 commit 390130e

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

core/src/main/java/com/fpf/smartscansdk/core/media/ImageUtils.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,27 @@ private fun computeIoU(boxA: FloatArray, boxB: FloatArray): Float {
9595
val areaB = max(0f, boxB[2] - boxB[0]) * max(0f, boxB[3] - boxB[1])
9696
val unionArea = areaA + areaB - intersectionArea
9797
return if (unionArea <= 0f) 0f else intersectionArea / unionArea
98-
}
98+
}
99+
100+
fun drawBoxes(bitmap: Bitmap, boxes: List<FloatArray>, color: Int, margin: Int = 0, strokeWidth: Float = 2f): Bitmap {
101+
val mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
102+
val canvas = Canvas(mutableBitmap)
103+
104+
val paint = Paint().apply {
105+
this.color = color
106+
this.strokeWidth = strokeWidth
107+
this.style = Paint.Style.STROKE
108+
}
109+
110+
for (box in boxes) {
111+
val x1 = max(0, box[0].toInt() -margin)
112+
val y1 = max(0, box[1].toInt() -margin)
113+
val x2 = min(mutableBitmap.width, box[2].toInt() + margin)
114+
val y2 = min(mutableBitmap.height, box[3].toInt() + margin)
115+
116+
canvas.drawRect(x1.toFloat(), y1.toFloat(), x2.toFloat(), y2.toFloat(), paint)
117+
}
118+
119+
return mutableBitmap
120+
}
121+

0 commit comments

Comments
 (0)