Calculate Average Color from a SVG Image (SvgPicture.network(""))

176

It transfers from Image url to Uint8List.

Future<Uint8List> getUint8ListFromImage(imgUrl) async {
    Uint8List bytes = (await NetworkAssetBundle(Uri.parse(imgUrl)).load(imgUrl))
        .buffer
        .asUint8List();
    print(bytes);
    return bytes;
  }
Share:
176
Mohammad Shamsi
Author by

Mohammad Shamsi

Updated on December 31, 2022

Comments

  • Mohammad Shamsi
    Mohammad Shamsi over 1 year

    I try to calculate the average color from SVG format Image, but I don't know how can an SVG Image from the network convert to Unit8List, ImageProvider, or BitMap! for any of these types that I say, I can calculate the average color with the below code :

    (I use image package)

    import 'package:image/image.dart' as imgPack;
    //
    Unit8List _myHunit8List =  ...
    imgPack.Image bitmap = imgPack.decodeImage(_myHunit8List );
    
      int redBucket = 0;
      int greenBucket = 0;
      int blueBucket = 0;
      int pixelCount = 0;
    
      for (int y = 0; y < bitmap.height; y++) {
        for (int x = 0; x < bitmap.width; x++) {
          int c = bitmap.getPixel(x, y);
    
          pixelCount++;
          redBucket += img.getRed(c);
          greenBucket += img.getGreen(c);
          blueBucket += img.getBlue(c);
        }
      }
    
      Color averageColor = Color.fromRGBO(redBucket ~/ pixelCount,
          greenBucket ~/ pixelCount, blueBucket ~/ pixelCount, 1);
    

    how can I an SVG Image from the network

    ( I use flutter_svg package like:

    SvgPicture.network(url);
    

    ) convert to Unit8List ?

    • enxaneta
      enxaneta over 2 years
      I would use canvas for this. Canvas is allowing you to loop all the pixels in an image and retrive the red green blue and alpha values for every pixel. If you must you can draw an svg image onto the canvas.