facsimile 1.0.0
facsimile: ^1.0.0 copied to clipboard
Removes the paper background from photos of stamps and signatures to make transparent facsimiles for documents, printing or e-signing.
import 'package:facsimile/facsimile.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Facsimile Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(title: 'Facsimile Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Uint8List? _original;
Uint8List? _processed;
bool _loading = false;
String? _error;
Future<void> _loadAndProcess() async {
setState(() {
_loading = true;
_error = null;
});
try {
final buf = await rootBundle.load('assets/img1_137.jpg');
final bytes = buf.buffer.asUint8List();
final processed = const Facsimile().removeBackground(bytes);
setState(() {
_original = bytes;
_processed = processed;
});
} catch (e) {
setState(() {
_error = e.toString();
});
} finally {
setState(() {
_loading = false;
});
}
}
@override
Widget build(BuildContext context) {
const size = Size(320, 320);
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
floatingActionButton: FloatingActionButton.extended(
onPressed: _loading ? null : _loadAndProcess,
icon: const Icon(Icons.auto_fix_high),
label: const Text('Make facsimile'),
),
body: Center(
child: _loading
? const CircularProgressIndicator()
: _error != null
? Padding(
padding: const EdgeInsets.all(16),
child: Text(_error ?? 'Error'),
)
: Wrap(
spacing: 16,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
if (_original != null)
_Preview(label: 'Original', image: _original!, size: size),
if (_processed != null) ...[
_Preview(
label: 'Facsimile',
image: _processed!,
size: size,
),
_Preview(
label: 'Stamped on green',
image: _processed!,
size: size,
showOnColor: Colors.green,
),
] else
const Padding(
padding: EdgeInsets.all(16),
child: Text(
'Press the button to load the sample '
'signature photo and remove its background.',
),
),
],
),
),
);
}
}
class _Preview extends StatelessWidget {
const _Preview({
required this.label,
required this.image,
required this.size,
this.showOnColor,
});
final String label;
final Uint8List image;
final Size size;
final Color? showOnColor;
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(label),
const SizedBox(height: 4),
Container(
width: size.width,
height: size.height,
color: showOnColor,
child: Image.memory(image, fit: BoxFit.contain),
),
],
);
}
}