Android Bitmap crop/compress/scale to limited maximum width and height, Kotlin

Android Bitmap crop/compress/scale to limited maximum width and height, Kotlin

    private fun cropImage(image: Bitmap): Bitmap {
        val maxWidth = 1024 //假设宽度最大值1024
        val maxHeight = 1024 //假设高度最大值1024

        val width = image.width
        val height = image.height

        if (width <= maxWidth && height <= maxHeight) {
            // 如果图片的宽高都小于等于目标裁剪尺寸,则无需裁剪,直接返回原图。
            return image
        }

        val scale = Math.min(maxWidth.toFloat() / width, maxHeight.toFloat() / height)
        val scaledWidth = (width * scale).toInt()
        val scaledHeight = (height * scale).toInt()

        return Bitmap.createScaledBitmap(image, scaledWidth, scaledHeight, true)
    }

Android Bitmap is saved as a mobile phone image file, Kotlin-CSDN blogThe article has been viewed and read 348 times, liked 3 times, and collected 3 times. Android splicing and merging pictures to generate a long picture code realizes merging two pictures, using the width of the first picture as the standard. If the width of the second picture to be merged is different from the first picture, then the width of the first picture shall prevail. line to scale the second image. Assume that there are already two pictures zhang.jpg and phil.jpg under Pictures in the root directory. Finally, these two pictures are merged into the long picture of zhangphil.jpg: package zhangphil.test;https: //blog.csdn.net/zhangphil/article/details/134603333Android gets the width and height of the original image Bitmap, Kotlin_android gets the width and height of the bitmap - CSDN BlogThe article has been viewed and read 1.4k times, liked 19 times, and collected 22 times. The article has been viewed and read 593 times. The article has been viewed and read 5.3k times."Convert thumbnails of Android large pictures, and crop the original large pictures into thumbnails according to the specified width and height" In the process of loading image resources in Android's ImageView, due to the needs of performance and memory overhead, sometimes it is necessary to The original oversized image is scaled into smaller thumbnails according to a certain proportion, or the original oversized image needs to be cropped into a smaller image with a specified width and height. For this kind of development needs, you can use the tools provided by the Android SDK itself. Class: ThumbnailUtils Complete. The article has been viewed and read 1.8k times. _android Get bitmap width and heighthttps://blog.csdn.net/zhangphil/article/details/134448886

Guess you like

Origin blog.csdn.net/zhangphil/article/details/134693021