在Android上将int数组转换为位图

2022-01-09 00:00:00 opengl-es android java

我有一个表示颜色的 MxN 整数数组(比如 RGBA 格式,但很容易更改).我想将它们转换为可以渲染到屏幕的 MxN 位图或其他东西(例如 OpenGL 纹理).有没有快速的方法来做到这一点?循环遍历数组并将它们绘制到画布上太慢了.

I have an MxN array of ints representing colors (say RGBA format, but that is easily changeable). I would like to convert them to an MxN Bitmap or something else (such as an OpenGL texture) that I can render to the screen. Is there a fast way to do this? Looping through the array and drawing them to the canvas is far too slow.

推荐答案

试试这个,它会给你位图:

Try this, it will give you the bitmap:

 // You are using RGBA that's why Config is ARGB.8888 
    bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
 // vector is your int[] of ARGB 
    bitmap.copyPixelsFromBuffer(IntBuffer.wrap(vector));

或者您可以从以下本地方法生成 IntBuffer:

Or you can generate IntBuffer from the following native method:

private IntBuffer makeBuffer(int[] src, int n) {
    IntBuffer dst = IntBuffer.allocate(n*n);
    for (int i = 0; i < n; i++) {
        dst.put(src[i]);
    }
    dst.rewind();
    return dst;
}

相关文章