portrait_validator_and_enhancer 1.0.2
portrait_validator_and_enhancer: ^1.0.2 copied to clipboard
A comprehensive Flutter package for portrait quality validation, face analysis, and image enhancement.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:portrait_validator_and_enhancer/portrait_validator_and_enhancer.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Portrait Enhancer Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _qualityGate = ImageQualityGateService();
final _enhancer = ImageEnhancementService();
final _recenter = FaceRecenterService();
final _bgRemoval = BackgroundRemovalService();
final _picker = ImagePicker();
bool _isProcessing = false;
String _status = 'Select a photo to begin';
File? _originalImage;
File? _processedImage;
@override
void dispose() {
_qualityGate.dispose();
_recenter.dispose();
_bgRemoval.dispose();
super.dispose();
}
Future<void> _pickAndProcess() async {
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
if (image == null) return;
final File file = File(image.path);
setState(() {
_originalImage = file;
_processedImage = null;
_isProcessing = true;
_status = 'Validating portrait quality...';
});
try {
// 1. Validate
final validation = await _qualityGate.validate(file);
if (!validation.isAccepted) {
setState(() {
_status = 'Validation Failed:\n${validation.rejectionReason}';
_isProcessing = false;
});
return;
}
// 2. Recenter & Crop
setState(() => _status = 'Recentering and cropping...');
final cropped = await _recenter.recenterFace(file);
// 3. Enhance
setState(() => _status = 'Applying professional enhancement...');
final enhanced = await _enhancer.enhanceImage(cropped);
// 4. Remove Background (Optional)
setState(() => _status = 'Removing background...');
final finalImage = await _bgRemoval.removeBackground(enhanced);
setState(() {
_processedImage = finalImage;
_status = 'Processing complete!';
_isProcessing = false;
});
} catch (e) {
setState(() {
_status = 'Error: $e';
_isProcessing = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Portrait Enhancer'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_isProcessing) ...[
const CircularProgressIndicator(),
const SizedBox(height: 20),
Text(_status, textAlign: TextAlign.center),
] else ...[
if (_processedImage != null) ...[
const Text('Processed Result', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 10),
Image.file(_processedImage!),
] else if (_originalImage != null) ...[
const Text('Original Image', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 10),
Image.file(_originalImage!),
] else
const Icon(Icons.face, size: 100, color: Colors.grey),
const SizedBox(height: 20),
Text(_status, textAlign: TextAlign.center),
const SizedBox(height: 40),
ElevatedButton.icon(
onPressed: _pickAndProcess,
icon: const Icon(Icons.photo_library),
label: const Text('Pick Image & Process'),
),
],
],
),
),
),
);
}
}