createScaledBitmap&QUOT

createScaledBitmap&QUOT

本文介绍了请问" Bitmap.createScaledBitmap"转换的32位图像转换成24位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我加载图像为32位(ARGB_8888)是这样的:

In my app I load an image as 32 bit (ARGB_8888) this way:

 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);

如果我使用缩放相同的宽度和原始位图的高度,它以MB为单位的大小(我看堆的大小)的1/2。改变价值ARGB_8888到RGB_565(24位)给出了相同的大小以MB为单位缩放后。

If I use for scaling the same Width and Height of the original bitmap, it is 1/2 of the size in megabytes (I'm watching the heap size).Changing the value "ARGB_8888" to "RGB_565" (24 bit) gives the same size in megabytes after scaling.

有人可以解释这种现象,并可以给我一个建议,如何扩大在32位色彩空间的位图?谢谢!

Can someone explain this phenomenon and may be give me an advice, how to scale bitmaps in 32 bit color space?Thanks!

推荐答案

我抬头的方法createScaledBitmap的源位图类(<一href="http://www.google.com/$c$csearch/p?hl=en#uX1GffpyOZk/graphics/java/android/graphics/Bitmap.java"相对=nofollow>链接):

I looked up the method createScaledBitmap in the source for the Bitmap class (Link):

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()应该返回您不变的源位​​图,由于该检查方法中:

And the call to createBitmap() should return your unchanged source bitmap due to this check in the method body:

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

看着眼前这个又好像是你原来的位图返回,但是的,如果你的位图恰好是可变的,您可以有效地跳过此检查,最终在这里:

Looking at just this it would seem that your original bitmap is returned, But, if your bitmap happens to be mutable, you effectively skip this check and end up here:

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

由于您没有进行任何缩放,你的矩阵将是单位矩阵,并满足该条件。创建位图,你可以看到,依赖于源位图的Alpha。如果没有alpha为present,你最终与RGB_565格式,而不是ARGB_8888结果位图。

As you are not performing any scaling, your matrix will be the identity matrix, and the condition is satisfied. The bitmap created is, as you can see, dependent on the alpha in the source bitmap. If no alpha is present, you end up with a result bitmap with the RGB_565 format rather than the ARGB_8888.

所以,规模和preserve 32位格式,您的位图要么是不可变的,或者使用Alpha通道。

So, to scale and preserve the 32-bit format, your bitmap should either be immutable or use an Alpha channel.

这篇关于请问&QUOT; Bitmap.createScaledBitmap&QUOT;转换的32位图像转换成24位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:53