edge_detector 1.0.3
edge_detector: ^1.0.3 copied to clipboard
Edge detector plugin based on google ml kit object detection. Allows basic edge detection for given image.
example/lib/main.dart
import 'dart:io';
import 'package:edge_detector/edge_detector.dart';
import 'package:edge_detector_example/widgets/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(const ExampleApp());
}
class ExampleApp extends StatelessWidget {
const ExampleApp({
super.key,
});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: ExamplePage(),
);
}
}
class ExamplePage extends StatefulWidget {
const ExamplePage({
super.key,
});
@override
State<ExamplePage> createState() => _ExamplePageState();
}
class _ExamplePageState extends State<ExamplePage> {
static final GlobalKey _imageKey = GlobalKey();
File? _imageFile;
List<Offset>? _edges;
@override
void initState() {
super.initState();
_setInitialImage();
}
@override
Widget build(BuildContext context) {
final imageFile = _imageFile;
final edges = _edges;
return Scaffold(
appBar: AppBar(
title: const Text('Edge Detector Example'),
),
body: Padding(
padding: const EdgeInsets.all(16).copyWith(
bottom: MediaQuery.of(context).padding.bottom + 16,
),
child: ListView(
children: [
Row(
children: [
_ActionButton(
label: 'Pick image',
onPressed: _pickImage,
),
const SizedBox(width: 16),
_ActionButton(
label: 'Detect edges',
onPressed: _detectEdges,
),
],
),
const SizedBox(height: 16),
if (imageFile != null)
Stack(
children: [
Image.file(
imageFile,
key: _imageKey,
fit: BoxFit.contain,
),
if (edges != null) Lines(edges),
],
),
],
),
),
);
}
Future<void> _setInitialImage() async {
final bytes = await rootBundle.load('assets/images/1.jpg');
final imageData =
bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes);
final directory = (await getTemporaryDirectory()).path;
final path = '$directory/edge-detector-demo-image.jpg';
final file = File(path);
await file.writeAsBytes(imageData);
setState(() => _imageFile = file);
}
Future<void> _pickImage() async {
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null) return;
setState(() => _imageFile = File(image.path));
}
Future<void> _detectEdges() async {
final imageFile = _imageFile;
if (imageFile == null) return;
final originalImageEdges = await EdgeDetector().detectEdges(imageFile);
if (originalImageEdges == null) return;
final widgetEdges = await originalImageEdges.toWidgetEdges(
imageKey: _imageKey,
originalImageFile: imageFile,
);
setState(() => _edges = widgetEdges.values);
}
}
class _ActionButton extends StatelessWidget {
const _ActionButton({
required this.label,
required this.onPressed,
});
final String label;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
return Expanded(
child: ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
fixedSize: const Size.fromHeight(56),
),
child: Text(label),
),
);
}
}