addImageWatermarkUint8List method

  1. @override
Future<Uint8List?> addImageWatermarkUint8List(
  1. String filePath,
  2. Uint8List? bytes,
  3. Uint8List watermarkImagePath,
  4. int x,
  5. int y,
  6. int watermarkWidth,
  7. int watermarkHeight,
)
override

Adds an image watermark to the image at the specified location with the given parameters.

Returns a Uint8List representing the watermarked image.

Implementation

@override
Future<Uint8List?> addImageWatermarkUint8List(
  String filePath,
  Uint8List? bytes,
  Uint8List watermarkImagePath,
  int x,
  int y,
  int watermarkWidth,
  int watermarkHeight,
) async {
  // Decode the original image
  final originalImage = await ui
      .instantiateImageCodec(bytes!)
      .then((codec) => codec.getNextFrame())
      .then((frame) => frame.image);

  // Decode the watermark image
  final watermarkImage = await ui
      .instantiateImageCodec(watermarkImagePath)
      .then((codec) => codec.getNextFrame())
      .then((frame) => frame.image);

  // Create a recorder
  final recorder = ui.PictureRecorder();
  final canvas = Canvas(recorder);

  // Draw the original image
  canvas.drawImage(originalImage, Offset.zero, Paint());

  // Draw the watermark image
  canvas.drawImageRect(
    watermarkImage,
    Rect.fromLTRB(0, 0, watermarkImage.width.toDouble(),
        watermarkImage.height.toDouble()),
    Rect.fromLTRB(x.toDouble(), y.toDouble(), (x + watermarkWidth).toDouble(),
        (y + watermarkHeight).toDouble()),
    Paint(),
  );

  // Convert the canvas to image
  final picture = recorder.endRecording();
  final img =
      await picture.toImage(originalImage.width, originalImage.height);
  final byteData = await img.toByteData(format: ui.ImageByteFormat.png);

  return byteData?.buffer.asUint8List();
}