Search code examples
androidflutterdartmobile-application

Flutter: Calculate the center point of an image


Hi everyone so I'm creating this app where I want to get the color of an image in the center point of an image but the problem is I don't know how to plot the coordinates properly

  Widget hanldeimagepixelColor(BuildContext context) {
Image img = Image.file(File(imagepathfile!.path));
return ImagePixels(
    imageProvider: img.image,
    builder: (context, img) {
      int? imgwidth = img.width;
      int xRelative = //this is where I want to calculate the point relative to the image width;
      int yRelative = //this is where I want to caclulate the point relative to the image width;
      Color color = img.pixelColorAt!(xRelative, yRelative);
      return Container(
        height: 30,
        width: 30,
        color: color,
      );
    });

}


Solution

  • To calculate center points, just divide imageHeight & imageWidth by half.

    Widget handleImagePixelColor(BuildContext context) {
        Image image = Image.file(File(imagePath.file));
        return ImagePixels(
        imageProvider: img.image,
        builder: (context, img) {
          double imgwidth = img.width;
          double imgHeight = img.height;
          double xRelative = imgwidth / 2;
          double yRelative = imgHeight / 2;
          Color color = img.pixelColorAt!(xRelative, yRelative);
          return Container(
            height: 30,
            width: 30,
            color: color,
          );
        });
      }