Bitmap is giving back width and height reversed

66 views Asked by At

The app adjust the canvas comparing its aspect ratio with the image aspect ratio. It works fine in mobile and tablet horizontally, but when the tablet is vertical, the img parameters width and height are not corresponding with the actual img orientation.

Any clue?, could it be that the bitmap sets what is width and height based on img metadata?

Here is the code:

 //Resizes the canvas so it adjust to image on it perfectly
private fun resizeCanvas() {
    //Initializes image to bitmap
    imageBitmap = BitmapFactory.decodeFile(imagePath)
    //Aspect ratios of both canvas and image
    val canvasAspectRatio =
        drawingView.width.toFloat() / drawingView.height.toFloat()
    val imageAspectRatio = imageBitmap.width.toFloat() / imageBitmap.height.toFloat()

    var finalWidth: Int
    var finalHeight: Int

    //Horizontal images
    if (canvasAspectRatio < imageAspectRatio) {

        finalWidth = drawingView.width
        finalHeight = (drawingView.width * imageBitmap.height) / imageBitmap.width
    }
    //Vertical images
    else {
        finalWidth = (drawingView.height * imageBitmap.width) / imageBitmap.height
        finalHeight = drawingView.height
    }
    //Sets the canvas width to the width of the image, on the screen already...
    val layoutParams = drawingView.layoutParams as ConstraintLayout.LayoutParams
    layoutParams.width = finalWidth
    layoutParams.height = finalHeight
    drawingView.layoutParams = layoutParams
}
0

There are 0 answers