removeBackground method
Implementation
@override
Future<File> removeBackground(File imageFile) async {
await initialize();
try {
final bytes = await imageFile.readAsBytes();
final ui.Image resultImage =
await BackgroundRemover.instance.removeBg(bytes);
final byteData = await resultImage.toByteData(
format: ui.ImageByteFormat.png);
resultImage.dispose();
if (byteData == null) {
CloudLogger.warning(
'BackgroundRemover: byteData null, using original');
return imageFile;
}
final pngBytes = byteData.buffer.asUint8List();
final tempDir = await getTemporaryDirectory();
final outputPath =
'${tempDir.path}/cm_bg_removed_${DateTime.now().millisecondsSinceEpoch}.png';
final outputFile = File(outputPath);
await outputFile.writeAsBytes(pngBytes);
CloudLogger.debug('Background removed: $outputPath');
return outputFile;
} catch (e, st) {
CloudLogger.error('Background removal failed',
error: e, stackTrace: st);
return imageFile;
}
}