Android Bitmap裁剪/压缩/缩放到限定的最大宽高值,Kotlin

Android Bitmap裁剪/压缩/缩放到限定的最大宽高值,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保存成至手机图片文件,Kotlin-CSDN博客文章浏览阅读348次,点赞3次,收藏3次。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。假设根目录的Pictures下已经有两张图片zhang.jpg和phil.jpg,最终把这两张图片合并成zhangphil.jpg的长图:package zhangphil.test;https://blog.csdn.net/zhangphil/article/details/134603333Android获取原始图片Bitmap的宽高大小尺寸,Kotlin_android 获取bitmap宽高-CSDN博客文章浏览阅读1.4k次,点赞19次,收藏22次。文章浏览阅读593次。文章浏览阅读5.3k次。《Android大图片之变换缩略图,以及对原始大图片按照指定宽、高裁剪成缩略图》在Android的ImageView加载图像资源过程中,出于性能和内存开销的需要,有时候需要把一个原始的超大图片按照一定比例等比例缩放成较小的缩略图,或者需要把原始的超大图片,裁剪成指定宽高值的较小图片,针对这种开发需求,可以使用Android SDK自身提供的工具类:ThumbnailUtils完成。文章浏览阅读1.8k次。_android 获取bitmap宽高https://blog.csdn.net/zhangphil/article/details/134448886

  • 18
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,你可以使用BitmapFactory.Options类的inSampleSize属性来实现同时等比例缩放压缩Bitmap的功能。具体步骤如下: 1. 通过BitmapFactory.decodeFile或BitmapFactory.decodeStream方法获取原始Bitmap对象。 2. 计算出缩放比例,公式为:inSampleSize = 原始宽度 / 目标宽度(或者原始高度 / 目标高度,取两者中比例较小的值)。 3. 设置BitmapFactory.Options对象的inSampleSize属性为计算出的缩放比例。 4. 通过BitmapFactory.decodeFile或BitmapFactory.decodeStream方法,传入设置了inSampleSize属性的BitmapFactory.Options对象,获取压缩后的Bitmap对象。 下面是示例代码: ```java public static Bitmap compressBitmap(String filePath, int targetWidth, int targetHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); int width = options.outWidth; int height = options.outHeight; int inSampleSize = 1; if (width > targetWidth || height > targetHeight) { int widthRatio = Math.round((float) width / (float) targetWidth); int heightRatio = Math.round((float) height / (float) targetHeight); inSampleSize = Math.min(widthRatio, heightRatio); } options.inJustDecodeBounds = false; options.inSampleSize = inSampleSize; Bitmap compressedBitmap = BitmapFactory.decodeFile(filePath, options); return compressedBitmap; } ``` 在上述示例代码中,compressBitmap方法接受三个参数:文件路径、目标宽度和目标高度。方法内部首先通过设置options.inJustDecodeBounds为true来获取原始Bitmap对象的宽度和高度,然后根据目标宽度和高度计算出缩放比例inSampleSize,最后再通过设置options.inSampleSize属性来获取压缩后的Bitmap对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值