processImage static method

Future<Uint8List> processImage(
  1. Map<String, dynamic> params
)

Implementation

static Future<Uint8List> processImage(Map<String, dynamic> params) async {
  print('Isolate: Starting image processing');
  try {
    final path = params['path'] as String;
    print('Isolate: Processing file at path: $path');

    final dateTime = DateTime.parse(params['dateTime'] as String);
    final locationData = params['locationData'] != null
        ? ImageLocation(
            latitude: (params['locationData']
                as Map<String, dynamic>)['latitude'] as double,
            longitude: (params['locationData']
                as Map<String, dynamic>)['longitude'] as double,
            timestamp: DateTime.now(),
            accuracy: 0,
            altitude: 0,
            altitudeAccuracy: 0,
            heading: 0,
            headingAccuracy: 0,
            speed: 0,
            speedAccuracy: 0,
          )
        : null;
    final angleForText = params['angleForText'] as num;
    final dateStringWidth = params['dateStringWidth'] as double;
    final locationStringWidth = params['locationStringWidth'] as double?;

    print('Isolate: Reading image file');
    final capturedImage = image.decodeImage(await File(path).readAsBytes())!;
    print(
        'Isolate: Image decoded, size: ${capturedImage.width}x${capturedImage.height}');

    final orientedImage = image.bakeOrientation(capturedImage);
    print('Isolate: Orientation baked');

    final file =
        await File(path).writeAsBytes(image.encodeJpg(orientedImage));
    print('Isolate: Oriented image written');

    if (file.existsSync()) {
      print('Isolate: Processing oriented image');
      final i = image.decodeImage(await file.readAsBytes())!;
      image.Image orientedImage;

      if (angleForText != 0) {
        orientedImage = image.copyRotate(i, angle: angleForText);
        print('Isolate: Image rotated for text');
      } else {
        orientedImage = i;
      }

      final dateSting = DateFormat('dd MMM, yyyy HH:mm').format(dateTime);

      var drawString = image.drawString(
        orientedImage,
        font: image.arial48,
        x: (orientedImage.width - dateStringWidth - 320).toInt(),
        y: orientedImage.height - 120,
        dateSting,
        color: image.ColorRgb8(225, 19, 40),
      );
      print('Isolate: Date string drawn');

      if (locationData != null && locationStringWidth != null) {
        final locationSting =
            '${locationData.latitude}, ${locationData.longitude}';

        drawString = image.drawString(
          orientedImage,
          font: image.arial48,
          x: (orientedImage.width - locationStringWidth - 400).toInt(),
          y: orientedImage.height - 70,
          locationSting,
          color: image.ColorRgb8(225, 19, 40),
        );
        print('Isolate: Location string drawn');
      }

      image.Image originalOrientation;

      if (angleForText != 0) {
        originalOrientation =
            image.copyRotate(drawString, angle: 360 - angleForText);
        print('Isolate: Image rotated back to original');
      } else {
        originalOrientation = drawString;
      }

      final bytes = Uint8List.fromList(
          image.encodeJpg(originalOrientation, quality: 100));
      print('Isolate: Final JPEG encoded, size: ${bytes.length} bytes');

      await File(path).delete();
      print('Isolate: Original file deleted');

      print('Isolate: Processing completed successfully');
      return bytes;
    } else {
      print('Isolate: File does not exist');
      throw Exception('No file.');
    }
  } catch (e, stack) {
    print('Isolate: Error during processing: $e');
    print('Isolate: Stack trace: $stack');
    rethrow;
  }
}