如何在Android中从整数数组创建图像

时间:2014-07-05 15:35:43

标签: android arrays image bitmap bitmapfactory

我正在尝试编写一个细胞自动机程序来处理数组中的整数值,然后将该数组显示为图像。

整数数组 - >图像 - >在屏幕上显示

我已经尝试过BitmapFactory.decodeByteArray()和Bitmap.createBitmap(),但是所有示例都需要将现有的.jpg或.png读入byte [],然后将其转换回位图。

有没有人有一个从头开始构建数组,转换为图像然后显示的明确示例?即使是最简单的50x50像素的蓝色方块也是有用的。

如果BitmapFactory.decodeByteArray()不是最佳选择,我会接受任何替代方案。

谢谢!

我的代码到目前为止,来自onClick()方法:

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 个答案:

答案 0 :(得分: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