BG Remove Flutter Plugin

A Flutter plugin that brings powerful AI-powered background removal to your mobile apps. Remove backgrounds from images with WebGPU acceleration for lightning-fast processing.

Features

  • 🚀 Fast Processing: Uses WebGPU when available, falls back to WASM
  • 📱 Cross-Platform: Works on Android and iOS
  • 📊 Progress Tracking: Real-time progress callbacks
  • 💾 Flexible Output: Support for PNG and WebP formats
  • 🔧 Easy Integration: Simple API with just a few lines of code

Installation

Add this to your pubspec.yaml:

dependencies:
  bgremove_flutter: ^0.0.5
  image_picker: ^1.0.7  # For image selection in examples

Android Setup

Add internet permission to android/app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

iOS Setup

Add photo library usage description to ios/Runner/Info.plist:

<key>NSPhotoLibraryUsageDescription</key>
<string>We need access to your photos to remove backgrounds</string>

Quick Start

import 'package:bgremove_flutter/bgremove_flutter.dart';

// Initialize the plugin (call once at app start)
await BgremoveFlutter.initialize();

// Remove background from image
final result = await BgremoveFlutter.removeBackground(
  imageData: yourImageBytes,
  outputFormat: 'png',
);

// Use the result
File('output.png').writeAsBytesSync(result.imageData);

Usage Examples

Basic Usage

import 'dart:io';
import 'package:bgremove_flutter/bgremove_flutter.dart';

Future<void> removeBackground() async {
  // Initialize
  await BgremoveFlutter.initialize();
  
  // Load image
  final imageBytes = File('input.jpg').readAsBytesSync();
  
  // Process
  final result = await BgremoveFlutter.removeBackground(
    imageData: imageBytes,
    outputFormat: 'png',
  );
  
  // Save
  File('output.png').writeAsBytesSync(result.imageData);
}

With Progress Tracking

final result = await BgremoveFlutter.removeBackground(
  imageData: imageBytes,
  onProgress: (phase, progress) {
    print('$phase: ${progress.toStringAsFixed(1)}%');
    // Update your UI with progress
  },
);

Check Device Capabilities

final caps = await BgremoveFlutter.getCapabilities();
print('Backend: ${caps.device}'); // 'webgpu' or 'wasm'
print('Precision: ${caps.dtype}'); // 'fp16', 'fp32', etc.

final isWebGpuAvailable = await BgremoveFlutter.isWebGpuSupported();
print('WebGPU supported: $isWebGpuAvailable');

Process from File Path

final result = await BgremoveFlutter.removeBackgroundFromFile(
  filePath: '/path/to/image.jpg',
  outputFormat: 'webp',
);

API Reference

BgremoveFlutter

initialize()

Initialize the plugin. Must be called before using other methods.

await BgremoveFlutter.initialize();

removeBackground()

Remove background from image data.

Future<BgRemoveResult> removeBackground({
  required Uint8List imageData,
  String outputFormat = 'png',
  ProgressCallback? onProgress,
})

Parameters:

  • imageData: Image bytes (JPEG, PNG, WebP supported)
  • outputFormat: Output format ('png' or 'webp')
  • onProgress: Optional callback for progress updates

Returns: BgRemoveResult with processed image data

removeBackgroundFromFile()

Remove background from image file.

Future<BgRemoveResult> removeBackgroundFromFile({
  required String filePath,
  String outputFormat = 'png',
  ProgressCallback? onProgress,
})

getCapabilities()

Get device processing capabilities.

Future<BgRemoveCapabilities> getCapabilities()

Returns: Device and precision information

isWebGpuSupported()

Check if WebGPU is available on the device.

Future<bool> isWebGpuSupported()

Classes

BgRemoveResult

class BgRemoveResult {
  final Uint8List imageData;
  final String format;
}

BgRemoveCapabilities

class BgRemoveCapabilities {
  final String device;  // 'webgpu' or 'wasm'
  final String dtype;   // 'fp16', 'fp32', etc.
}

ProgressCallback

typedef ProgressCallback = void Function(String phase, double progress);

Performance Tips

  1. Resize large images before processing to improve speed
  2. Use WebP format for smaller output file sizes
  3. Cache processed images to avoid reprocessing
  4. Run on background isolate for heavy workloads

Example App

Check out the complete example app in example/ directory that demonstrates:

  • Image picking from gallery
  • Real-time progress tracking
  • Before/after comparison
  • Saving processed images

Troubleshooting

Android WebView Issues

If you encounter WebView issues on Android, ensure:

  • Internet permission is added
  • WebView is updated on the device
  • Hardware acceleration is enabled

iOS Memory Issues

For large images on iOS:

  • Resize images before processing
  • Process images one at a time
  • Clear cache between operations

Requirements

  • Flutter SDK: >=3.0.0
  • Dart SDK: >=3.0.0
  • Android: API 21+ (Android 5.0+)
  • iOS: 12.0+

License

MIT License - see LICENSE file for details