addImageWatermarkUint8List method
Future<Uint8List?>
addImageWatermarkUint8List(
- Uint8List filePath,
- Uint8List watermarkImagePath,
- int x,
- int y,
- int watermarkWidth,
- 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(
Uint8List filePath,
Uint8List watermarkImagePath,
int x,
int y,
int watermarkWidth,
int watermarkHeight,
) async {
// Decode the original image
final originalImage = await ui
.instantiateImageCodec(filePath)
.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();
}