Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Create PNG from RGBA matrix in Flutter - Alpha value not working

$
0
0

I'm trying to create a PNG image from a list of RGBA values but the Alpha value isn't getting through.

My guess is that a pixel is always on or off and therefore do not use the Alpha.Is there any other way to create an image or a way to make it work? I can't find anything.

  void createPngFromMatrix(      List<List<List<int>>> rgbaMatrix, String outputPath) {    int height = rgbaMatrix.length;    int width = rgbaMatrix[0].length;    img.Image image = img.Image(width: width, height: height);    for (int y = 0; y < height; y++) {      for (int x = 0; x < width; x++) {        List<int> rgba = rgbaMatrix[y][x];        print('rgba: $rgba');        image.setPixelRgba(x, y, rgba[0], rgba[1], rgba[2], rgba[3]);        print('pixel: ${image.getPixel(x, y)}');      }    }    File(outputPath).writeAsBytesSync(img.encodePng(image));  }

Here are the parameters used :

rgbaMatrix = [[[70, 0, 184, 0], [161, 0, 93, 255]], [[150, 0, 104, 0], [167, 0, 87, 255]]] //ExampleoutputPath = 'output.png'

And here is what the prints are displaying. Also the image is well created but all pixels are displayed.

rgba: [72, 0, 182, 0]pixel: (72, 0, 182)rgba: [152, 0, 102, 255]pixel: (152, 0, 102)

Oh and i'm using this version image: ^4.2.0

I also tried to replace this line

image.setPixelRgba(x, y, rgba[0], rgba[1], rgba[2], rgba[3]);

with

final color = img.ColorRgba8(rgba[0], rgba[1], rgba[2], rgba[3]);image.setPixel(x, y, color);

but the result is the same.


Viewing all articles
Browse latest Browse all 12111

Trending Articles