0

I'm trying to write a cellular automata program that manipulates integer values in an array and then displays the array as an image.

array of integers --> image --> display on screen

I've tried BitmapFactory.decodeByteArray(), and Bitmap.createBitmap() but all the examples require reading an existing .jpg or .png into a byte[], which is then converted back into a bitmap.

Does anyone have a clear example of building an array from scratch, converting to an image and then displaying? Even the simplest example of an entirely blue square 50x50 pixels would be helpful.

If BitmapFactory.decodeByteArray() is not the best option, I'm open to any alternatives.

Thanks!

my code so far, from an onClick() method:

display = (ImageView) findViewById(R.id.imageView1);
Bitmap bMap = null;
int w = 50, h = 50; // set width = height = 50 
byte[] input = new byte[w * h]; // initialize input array

for (int y = 0; y < h; y++) { // fill input with blue
    for (int x = 0; x < w; x++) {
         input[y * w + x] = (byte) Color.BLUE;
        }
    }
bMap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, Bitmap.Config.ARGB_8888); // convert byte array to bitmap
display.setImageBitmap(bMap); // post bitmap to imageview

1 Answers1

0
// 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 value .      
    bitmap.copyPixelsFromBuffer(makeBuffer(vector, vector.length));

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

creating an empty bitmap and drawing though canvas in android

Community
  • 1
  • 1
Faisal
  • 364
  • 1
  • 8
  • thanks for the clues! I also found this helpful and got it to work for my purpose. http://stackoverflow.com/questions/18630080/android-byte-array-to-bitmap-how-to – user3489590 Jul 06 '14 at 01:26