Bitmap.createScaledBitmap造成的困扰

本文探讨了拍照识别项目中照片未全屏显示的问题,分析了Bitmap.createScaledBitmap方法导致的比例缩放原因,并提供了一个替代方案,使用Bitmap.createBitmap确保照片全屏显示。

最近做的项目是拍照识别,同事写的,老板反映拍的照片没有全屏,找了半天才发现是,相机返回的照片在创建bitmap的时候,同事用的是这个Bitmap.createScaledBitmap,大家看看源码

public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight,
        boolean filter) {
    Matrix m = new Matrix();

    final int width = src.getWidth();
    final int height = src.getHeight();
    if (width != dstWidth || height != dstHeight) {
        final float sx = dstWidth / (float) width;
        final float sy = dstHeight / (float) height;
        m.setScale(sx, sy);
    }
    return Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
}

final float sx = dstWidth / (float) width;

final float sy = dstHeight / (float) height;

m.setScale(sx, sy);

这三行代码对你传进去的宽高和bitmap的宽高进行了比例缩放,所以图片显示到 Imageview上不是实际bitmap的宽高,所以造成上面所说的拍出来的是全屏,实际显示不是全屏。

后面我用了下面这个方法就好了

public static Bitmap createBitmap(@NonNull Bitmap src) {
    return createBitmap(src, 0, 0, src.getWidth(), src.getHeight());
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值