How do I save the detected images from object detection example in Android Kotlin?

The Object detection sample is working well in detecting objects and rounding the objects as rectangles, however I want to save those highlighted images as bitmaps and save to the gallery.

I was able to get the empty white bitmaps by taking the boundaries of the boxes. However I want the full image cropped and saved to my phone gallery or as a file. How do I do it?