Converting array of int to Bitmap on Android

后端 未结 3 1238
谎友^
谎友^ 2021-01-02 02:08

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

3条回答
  •  囚心锁ツ
    2021-01-02 02:39

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

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

提交回复
热议问题