Bitmap Channels order different in Android

2019-04-15 06:32发布

问题:

I got a problem with an Air Native Extension in Android.

The ANE receive a Bitmap from the Actionscript side, compress it in jpeg format and send it back to the Actionscript that will write it on the storage.

Everything is good but one last thing.

It seems that the channels order for Actionscript is different than in Android, so my compressed image have RED colors in place of BLUE.. here is the code:

Actionscript (I'm using a library called deng.fzip.FZipLibrary to get the bitmap from a zip package)

__image = __fl.getBitmapData(path);
__de = new DataExchange();
__ba = __de.bitmapEncode(__image) as ByteArray;

Android

...
try {
    inputValue = (FREBitmapData)arg1[0];
    inputValue.acquire();
    int srcWidth = inputValue.getWidth();
    int srcHeight = inputValue.getHeight();
    Bitmap bm = Bitmap.createBitmap(srcWidth, srcHeight, Config.ARGB_8888);
    bm.copyPixelsFromBuffer(inputValue.getBits());
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 80, os);
    compressed = os.toByteArray();
    inputValue.release();
} catch (Exception e) {
    e.printStackTrace();
}

try {
    returnValue = FREByteArray.newByteArray();
    returnValue.setProperty("length", FREObject.newObject(compressed.length));
    returnValue.acquire();
    ByteBuffer returnBytes = returnValue.getBytes();
    returnBytes.put(compressed, 0, compressed.length);
    returnValue.release();
}
...

Anyone have an idea on how to convert the red to blue in the android side before send the image back? Or it needs to be done in the actionscript side?

Thank you very much and regards,

Gianpiero

回答1:

You can switch the colors like this:

(This code does not origin from me, unfortunately I forgot where I found it, so credit goes somewhere else)

private Bitmap m_encodingBitmap         = null;
private Canvas m_canvas                 = null;
private Paint m_paint                   = null;    
private final float[] m_bgrToRgbColorTransform  =
    {
        0,  0,  1f, 0,  0, 
        0,  1f, 0,  0,  0,
        1f, 0,  0,  0,  0, 
        0,  0,  0,  1f, 0
    };
private final ColorMatrix               m_colorMatrix               = new ColorMatrix(m_bgrToRgbColorTransform);
private final ColorMatrixColorFilter    m_colorFilter               = new ColorMatrixColorFilter(m_colorMatrix);

...

try{

FREBitmapData as3Bitmap = (FREBitmapData)args[0];
as3Bitmap.acquire();
m_encodingBitmap    = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
m_canvas        = new Canvas(m_encodingBitmap);
m_paint         = new Paint();
m_paint.setColorFilter(m_colorFilter);

m_encodingBitmap.copyPixelsFromBuffer(as3BitmapBytes);
as3Bitmap.release();
//
// Convert the bitmap from BGRA to RGBA.
//
m_canvas.drawBitmap(m_encodingBitmap, 0, 0, m_paint);

...

}

Hope that helps ... Timm