composeTextImage method

  1. @override
Future<Uint8List> composeTextImage({
  1. required Uint8List inputImage,
  2. required String text,
  3. String anchor = 'bottomRight',
  4. double margin = 16.0,
  5. String marginUnit = 'px',
  6. double offsetX = 0.0,
  7. double offsetY = 0.0,
  8. String offsetUnit = 'px',
  9. double widthPercent = 0.18,
  10. double opacity = 0.6,
  11. String format = 'jpeg',
  12. double quality = 0.9,
  13. String fontFamily = '.SFUI',
  14. double fontSizePt = 24.0,
  15. int fontWeight = 600,
  16. int colorArgb = 0xFFFFFFFF,
})
override

Composes inputImage with a text watermark and returns encoded bytes.

Options (defaults mirror composeImage where applicable):

  • text: watermark text (required)
  • anchor: 'topLeft'|'topRight'|'bottomLeft'|'bottomRight'|'center' (default: 'bottomRight')
  • margin: logical px (default: 16.0) with marginUnit 'px'|'percent'
  • offsetX/offsetY: offsets with offsetUnit 'px'|'percent' (default: 0)
  • widthPercent: 0..1 of base width to fit text (default: 0.18)
  • opacity: 0..1 applied post‑raster (default: 0.6)
  • format: 'jpeg' | 'png' (default: 'jpeg'), JPEG quality 0..1
  • fontFamily: default '.SFUI'
  • fontSizePt: default 24.0
  • fontWeight: 100..900 (default 600)
  • colorArgb: ARGB32 integer (default 0xFFFFFFFF)

Implementation

@override
Future<Uint8List> composeTextImage({
  required Uint8List inputImage,
  required String text,
  String anchor = 'bottomRight',
  double margin = 16.0,
  String marginUnit = 'px',
  double offsetX = 0.0,
  double offsetY = 0.0,
  String offsetUnit = 'px',
  double widthPercent = 0.18,
  double opacity = 0.6,
  String format = 'jpeg',
  double quality = 0.9,
  String fontFamily = '.SFUI',
  double fontSizePt = 24.0,
  int fontWeight = 600,
  int colorArgb = 0xFFFFFFFF,
}) async {
  // Primary path: Pigeon-generated API
  final api = pigeon.WatermarkApi();
  final req = pigeon.ComposeTextRequest(
    baseImage: inputImage,
    text: text,
    anchor: _anchorFromString(anchor),
    margin: margin,
    marginUnit: _unitFromString(marginUnit),
    offsetX: offsetX,
    offsetY: offsetY,
    offsetUnit: _unitFromString(offsetUnit),
    widthPercent: widthPercent,
    textStyle: pigeon.TextStyleDto(
      fontFamily: fontFamily,
      fontSizePt: fontSizePt,
      fontWeight: fontWeight,
      colorArgb: colorArgb,
    ),
    style: pigeon.WmStyleDto(
      opacity: opacity,
      stroke: false,
      strokeWidth: 1.0,
      shadowBlur: 0.0,
    ),
    format: _formatFromString(format),
    quality: quality,
  );
  try {
    final res = await api.composeText(req);
    return res.imageBytes;
  } on PlatformException catch (_) {
    // Fallback to legacy MethodChannel if Pigeon channel isn't set up.
    final args = <String, dynamic>{
      'inputImage': inputImage,
      'text': text,
      'anchor': anchor,
      'margin': margin,
      'marginUnit': marginUnit,
      'offsetX': offsetX,
      'offsetY': offsetY,
      'offsetUnit': offsetUnit,
      'widthPercent': widthPercent,
      'opacity': opacity,
      'format': format,
      'quality': quality,
      'fontFamily': fontFamily,
      'fontSizePt': fontSizePt,
      'fontWeight': fontWeight,
      'colorArgb': colorArgb,
    };
    final bytes = await methodChannel.invokeMethod<Uint8List>('composeText', args);
    if (bytes == null) {
      throw PlatformException(code: 'compose_text_failed', message: 'No bytes returned');
    }
    return bytes;
  }
}