skip to Main Content
late int alphaValue;
  late int redValue;
  late int greenValue;
  late int blueValue;

  int getAlpha(int pixel) {
    if (pixel is int) {
      return (pixel >> 24) & 0xFF;
    } else {
      // Gestione degli errori o valore di default nel caso in cui il valore di pixel non sia un intero
      return 0;
    }
  }

  int getRed(int pixel) {
    if (pixel is int) {
      return (pixel >> 16) & 0xFF;
    } else {
      // Gestione degli errori o valore di default
      return 0;
    }
  }

  int getGreen(int pixel) {
    if (pixel is int) {
      return (pixel >> 8) & 0xFF;
    } else {
      // Gestione degli errori o valore di default
      return 0;
    }
  }

  int getBlue(int pixel) {
    if (pixel is int) {
      return pixel & 0xFF;
    } else {
      // Gestione degli errori o valore di default
      return 0;
    }
  }



  Future<File> _createStartrail(List<File> images) async {
    if (images.isEmpty) {
      throw Exception('No images selected');
    }

    img.Image? startrailImage;

    for (final imageFile in images) {
      final bytes = await imageFile.readAsBytes();
      final image = img.decodeImage(Uint8List.fromList(bytes));

      if (image == null) continue;

      startrailImage ??= img.Image(width: image.width, height: image.height);

      for (int y = 0; y < image.height; y++) {
        for (int x = 0; x < image.width; x++) {
          final pixel = image.getPixel(x, y);

          alphaValue = getAlpha(pixel as int);
          redValue = getRed(pixel as int);
          greenValue = getGreen(pixel as int);
          blueValue = getBlue(pixel as int);

          final newPixel = (alphaValue << 24) | (redValue << 16) | (greenValue << 8) | blueValue;

          startrailImage.setPixel(x, y, newPixel as img.Color);
        }
      }
    }

    final output = await getTemporaryDirectory();
    final outputPath = '${output.path}/startrail.png';

    final outputFile = File(outputPath);
    await outputFile.writeAsBytes(img.encodePng(startrailImage!));

    return outputFile;
  }








  // Aggiungi una variabile per tenere traccia dell'immagine sommata
  File? _combinedImage;

// Aggiungi un metodo per combinare le immagini e mostrare l'anteprima
  void _combineImages() {
    if (_images.isNotEmpty) {
      _createStartrail(_images).then((combinedImage) {
        setState(() {
          _combinedImage = combinedImage;
        });
      }).catchError((error) {
        // Gestione degli errori in caso di fallimento della creazione dell'immagine
        print('Errore durante la creazione dell'immagine startrail: $error');
      });
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Camera Preview')),
      body: Column(
        children: [
          Expanded(
            child: FutureBuilder<void>(
              future: _initializeControllerFuture,
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  return AspectRatio(
                    aspectRatio: 16 / 9,
                    child: CameraPreview(_controller),
                  );
                } else {
                  return const Center(child: CircularProgressIndicator());
                }
              },
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: _pickImages,
                child: const Text('Select Images'),
              ),
              const SizedBox(width: 10),
              ElevatedButton(
                onPressed: _combineImages, // Combinare le immagini quando si preme il pulsante
                child: const Text('Create Startrail'),
              ),
            ],
          ),
          if (_imagePreviews.isNotEmpty)
            Container(
              padding: EdgeInsets.symmetric(vertical: 10),
              height: 100,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: _imagePreviews,
              ),
            ),
          if (_combinedImage != null)
            Container(
              padding: EdgeInsets.all(10),
              child: Image.file(_combinedImage!),
            ),
        ],
      ),
    );
  }
}xt`

Hi guys I’m creating an app with Dart and Flutter. I select the images from the gallery and put them on the screen (up to this point the app works), when I go to add the images together I get this error

I/flutter (17585): Error creating image: type ‘PixelUint8’ is not a subtype of type ‘int’ in type cast

2

Answers


  1. Chosen as BEST ANSWER

    This code does not generate errors but even if the images are added I don't see a startrail. Why?

    Future<File> _createStartrail(List<File> images) async {
    if (images.isEmpty) {
      throw Exception('No images selected');
    }
    
    img.Image? startrailImage;
    
    for (final imageFile in images) {
      final bytes = await imageFile.readAsBytes();
      final image = img.decodeImage(Uint8List.fromList(bytes));
    
      if (image == null) continue;
    
      startrailImage ??= img.Image(width: image.width, height: image.height);
    
      for (int y = 0; y < image.height; y++) {
        for (int x = 0; x < image.width; x++) {
          final pixel = image.getPixel(x, y);
    
          final alphaValue = pixel.a;
          final redValue = pixel.r;
          final greenValue = pixel.g;
          final blueValue = pixel.b;
    
          // Imposta il pixel combinato nella nuova immagine
          final newPixel = startrailImage.getPixel(x, y)
            ..setRgba(redValue, greenValue, blueValue, alphaValue);
    
          startrailImage.setPixel(x, y, newPixel);
        }
      }
    }
    
    // Salva l'immagine combinata come file
    final output = await getTemporaryDirectory();
    final outputPath = '${output.path}/startrail.png';
    
    final outputFile = File(outputPath);
    await outputFile.writeAsBytes(img.encodePng(startrailImage!));
    
    return outputFile;
    

    }

    Plus I get this error "Bottom overflowed by 253 pixels"

    `if (_imagePreviews.isNotEmpty)
            Expanded(
              child: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: Row(
                  children: _imagePreviews,
                ),
              ),
            ),
    
          if (_combinedImage != null)
            Container(
              padding: EdgeInsets.all(10),
              child: Image.file(_combinedImage!),
            ),`
    

    I tried using Flex but the app does not display the images chosen from the gallery on the screen


  2. Things start to go wrong here:

    final pixel = image.getPixel(x, y);
    alphaValue = getAlpha(pixel as int);
    

    pixel is not an int, it’s a Pixel. Luckily, Pixel has getters and setters.

    alphaValue = pixel.a;
    redValue = pixel.r;
    

    Looks like you want to set a pixel in another image. Use:

    final newPixel = startTrailImage.getPixel(x, y)
      ..setRgba(redValue, greenValue, blueValue, alphaValue);
    startrailImage.setPixel(x, y, newPixel);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search