【问题标题】:how to create image from integer array in Android如何在Android中从整数数组创建图像
【发布时间】:2014-08-26 14:23:29
【问题描述】:

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

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

我尝试过 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

【问题讨论】:

    标签: android arrays image bitmap bitmapfactory


    【解决方案1】:
    // 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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多