How to crop the image within a print screen?

Viewed 238

I am developing an application that chooses an image of a wound and displays it on the application screen. with this, the user marks the region of interest of the wound, so that later the algorithm can recognize and process the region of interest. I'm doing this using the lib implementation 'com.github.gcacace: signature-pad: 1.2.1' to demarcate the region and then I'm saving the screen's "printscreen" so I can save the markup along with the image of the wound. How I wish the image will look enter image description here

Exit: enter image description here

However, I want to cut the printscreen according to the image of the wound to send to the server to process the image. Can someone help me cut out the wound image after marking.

    private fun saveImage(myBitmap: Bitmap?): String? {

    try {
        // image naming and path  to include sd card  appending name you choose for file
        val mPath = Environment.getExternalStorageDirectory().toString() + "/imagesignature.jpg"

        // create bitmap screen capture
        val v1 = window.decorView.rootView
        v1.isDrawingCacheEnabled = true
        val bitmap = Bitmap.createBitmap(v1.drawingCache)
        v1.isDrawingCacheEnabled = false

        val imageFile = File(mPath)

        val outputStream = FileOutputStream(imageFile)
        val quality = 100
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
        outputStream.flush()
        outputStream.close()

        //setting screenshot in imageview
        val filePath = imageFile.path

        val ssbitmap = BitmapFactory.decodeFile(imageFile.absolutePath)

        imagem.setImageBitmap(ssbitmap)

    } catch (e: Throwable) {
        // Several error may come out with file handling or DOM
        e.printStackTrace()
    }
    return ""
}
3 Answers

As far as I know, I don't think it's possible to crop and image. In order to crop, you need to find the dimensions for the part that you want. I don't think you can tell the program the dimensions of what you want and then crop everything else off, as far as my knowledge goes. It might be possible to print an image, but I don't think Java can crop. Other coding programs might work better for this.

If you have the coordinates of the rectangle you want to save:

Bitmap croppedBmp = Bitmap.createBitmap(originalBmp, rectanglePositionX, rectanglePositionY, rectangleWidth, rectangleHeight);

Or you can try: BitmapFactory.decodeStream(InputStream is, Rect outPadding, Options opts) or BitmapFactory.decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts)

where in the Rect outPadding you will set the coordinates of the rectangle you want to save.

Related