local_rembg 1.1.3 local_rembg: ^1.1.3 copied to clipboard
A Flutter plugin for offline background removal in images, ideal for enhancing photo editing capabilities within mobile apps.
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:local_rembg/local_rembg.dart';
enum ProcessStatus {
loading,
success,
failure,
none,
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Local Remove Background',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blueAccent),
useMaterial3: true,
),
home: const MyHomePage(title: 'Background remover'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
super.key,
required this.title,
});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final ImagePicker picker = ImagePicker();
ProcessStatus status = ProcessStatus.none;
String? message;
Uint8List? imageBytes;
Future<void> _pickPhoto() async {
final XFile? pickedFile = await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
setState(() {
status = ProcessStatus.loading;
});
// Uint8List pickedFileBytes = await pickedFile.readAsBytes();
LocalRembgResultModel localRembgResultModel = await LocalRembg.removeBackground(
imagePath: pickedFile.path,
// imageUint8List: pickedFileBytes,
);
if (localRembgResultModel.status == 1) {
imageBytes = Uint8List.fromList(localRembgResultModel.imageBytes!);
setState(() {
status = ProcessStatus.success;
});
} else {
message = localRembgResultModel.errorMessage;
setState(() {
status = ProcessStatus.failure;
});
}
}
}
@override
Widget build(BuildContext context) {
final Size screenSize = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
if (imageBytes != null && status == ProcessStatus.success)
SizedBox(
height: screenSize.width * 0.8,
child: Image.memory(
imageBytes!,
),
),
if (status == ProcessStatus.loading)
const CupertinoActivityIndicator(
color: Colors.black,
),
if (status == ProcessStatus.failure)
Text(
message ?? 'Failed to process image',
style: const TextStyle(
color: Colors.red,
fontSize: 18,
),
textAlign: TextAlign.center,
),
if (status == ProcessStatus.none)
const Text(
'Select your image',
style: TextStyle(
color: Colors.black,
fontSize: 18,
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _pickPhoto,
child: const Icon(Icons.add_photo_alternate_outlined),
),
);
}
}