gpt4 book ai didi

android - "Bitmap.createScaledBitmap"是否将 32 位图像转换为 24 位图像?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:55:51 25 4
gpt4 key购买 nike

在我的应用程序中,我以这种方式加载 32 位 (ARGB_8888) 图像:

 Bitmap.Config mBitmapConfig;
mBitmapConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = mBitmapConfig;
mBitmap = BitmapFactory.decodeFile(SourceFileName, options);

然后缩放:

mBitmap = Bitmap.createScaledBitmap(mBitmap, iW, iH, true); 

如果我用于缩放原始位图的相同宽度和高度,则它是大小的 1/2(以兆字节为单位)(我正在观察堆大小)。将值“ARGB_8888”更改为“RGB_565”(24 位)在缩放后给出相同的大小(以兆字节为单位)。

有人可以解释这种现象并给我一个建议,如何在 32 位颜色空间中缩放位图?谢谢!

最佳答案

我在 Bitmap 类 ( Link ) 的源代码中查找了 createScaledBitmap 方法:

public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,
int dstHeight, boolean filter) {
Matrix m;
synchronized (Bitmap.class) {
// small pool of just 1 matrix
m = sScaleMatrix;
sScaleMatrix = null;
}

if (m == null) {
m = new Matrix();
}

final int width = src.getWidth();
final int height = src.getHeight();
final float sx = dstWidth / (float)width;
final float sy = dstHeight / (float)height;
m.setScale(sx, sy);
Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter);

synchronized (Bitmap.class) {
// do we need to check for null? why not just assign everytime?
if (sScaleMatrix == null) {
sScaleMatrix = m;
}
}

return b;
}

并且调用 createBitmap() 应该返回未更改的源位图,因为在方法主体中进行了此检查:

    if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
height == source.getHeight() && (m == null || m.isIdentity())) {
return source;
}

仅看一下,您的原始位图似乎已返回,但是,如果您的位图恰好是可变的,您实际上可以跳过此检查并在此处结束:

    if (m == null || m.isIdentity()) {
bitmap = createBitmap(neww, newh,
source.hasAlpha() ? Config.ARGB_8888 : Config.RGB_565);
paint = null; // not needed
}

由于您没有执行任何缩放,您的矩阵将是单位矩阵,并且满足条件。如您所见,创建的位图取决于源位图中的 alpha。如果不存在 alpha,您最终会得到 RGB_565 格式而不是 ARGB_8888 格式的结果位图。

因此,要缩放并保留 32 位格式,您的位图应该是不可变的或使用 Alpha channel 。

关于android - "Bitmap.createScaledBitmap"是否将 32 位图像转换为 24 位图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6278992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com