2011-10-21 21 views
8

Tengo una matriz MxN de ints que representan colores (por ejemplo, formato RGBA, pero que es fácilmente modificable). Me gustaría convertirlos a un mapa de bits MxN u otra cosa (como una textura OpenGL) que pueda mostrar a la pantalla. ¿Hay una manera rápida de hacer esto? Looping a través de la matriz y dibujarlos en el lienzo es demasiado lento.Convertir matriz de int a Bitmap en Android

Respuesta

15

Prueba esto le dará el mapa de bits.

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

EDIT:

//OR , you can generate IntBuffer from 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); 
     } 
     dst.rewind(); 
     return dst; 
    }*/ 

espero que le ayudará.

+0

Hay un error en tu 'makeBuffer' en alguna parte. Llene el mapa de bits de esta manera en su lugar: 'bmp.copyPixelsFromBuffer (IntBuffer.wrap (vector));' –

3

Sí, parece que tiene toda la información que necesita. Si M es el ancho y N es la altura, puede crear un nuevo mapa de bits con Bitmap.createBitmap, y puede completar los valores ARGB con el método setPixels que toma una matriz int.

Bitmap.createBitmap

Bitmap.setPixels

8

¿Por qué no utilizar Bitmap.setPixel? Incluso es nivel API 1:

int[] array = your array of pixels here... 
int width = width of "array"... 
int height = height of "array"... 

// Create bitmap 
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

// Set the pixels 
bitmap.setPixels(array, 0, width, 0, 0, width, height); 

Puede jugar con desplazamiento/paso/x/y según sea necesario.
Sin vueltas. Sin asignaciones adicionales.