cam_frame_scanner 2.0.0
cam_frame_scanner: ^2.0.0 copied to clipboard
A flutter plugin to detect edges of objects, scan paper, detect corner, detect rectangle. It allows cropping of the detected object image and returns the path of the cropped image.
import 'dart:async';
import 'dart:io';
import 'package:edge_detection/edge_detection.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? _imagePath;
@override
void initState() {
super.initState();
getPermission();
}
// get permission
void getPermission() async {
bool isCameraGranted = await Permission.camera.request().isGranted;
if (!isCameraGranted) {
isCameraGranted =
await Permission.camera.request() == PermissionStatus.granted;
}
if (!isCameraGranted) {
// Have not permission to camera
return;
}
}
Future<void> getImageFromCamera() async {
bool isCameraGranted = await Permission.camera.request().isGranted;
if (!isCameraGranted) {
isCameraGranted =
await Permission.camera.request() == PermissionStatus.granted;
}
if (!isCameraGranted) {
// Have not permission to camera
return;
}
// Generate filepath for saving
String imagePath = join((await getApplicationSupportDirectory()).path,
"${(DateTime.now().millisecondsSinceEpoch / 1000).round()}.jpeg");
bool success = false;
try {
//Make sure to await the call to detectEdge.
success = await EdgeDetection.detectEdge(
imagePath,
canUseGallery: true,
androidScanTitle: 'Scanning', // use custom localizations for android
androidCropTitle: 'Crop',
androidCropBlackWhiteTitle: 'Black White',
androidCropReset: 'Reset',
);
print("success: $success");
} catch (e) {
print(e);
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
if (success) {
_imagePath = imagePath;
}
});
}
Future<void> getImageFromGallery() async {
// Generate filepath for saving
String imagePath = join((await getApplicationSupportDirectory()).path,
"${(DateTime.now().millisecondsSinceEpoch / 1000).round()}.jpeg");
bool success = false;
try {
//Make sure to await the call to detectEdgeFromGallery.
success = await EdgeDetection.detectEdgeFromGallery(
imagePath,
androidCropTitle: 'Crop', // use custom localizations for android
androidCropBlackWhiteTitle: 'Black White',
androidCropReset: 'Reset',
);
print("success: $success");
} catch (e) {
print(e);
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
if (success) {
_imagePath = imagePath;
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(builder: (context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: ElevatedButton(
onPressed: getImageFromCamera,
child: Text('Scan'),
),
),
SizedBox(height: 20),
Center(
child: ElevatedButton(
onPressed: getImageFromGallery,
child: Text('Upload'),
),
),
SizedBox(height: 20),
Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const CameraView(),
),
).then((value) {
if (value != null) {
setState(() {
_imagePath = value as String;
});
}
});
},
child: Text('Scan with Widget'),
),
),
SizedBox(height: 20),
Text('Cropped image path:'),
Padding(
padding: const EdgeInsets.only(top: 0, left: 0, right: 0),
child: Text(
_imagePath.toString(),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 14),
),
),
Visibility(
visible: _imagePath != null,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.file(
File(_imagePath ?? ''),
),
),
),
],
),
),
);
}),
);
}
}
class CameraView extends StatefulWidget {
const CameraView({Key? key}) : super(key: key);
@override
State<CameraView> createState() => _CameraViewState();
}
class _CameraViewState extends State<CameraView> {
final EdgeDetectionCameraController _controller = EdgeDetectionCameraController();
String? _savePath;
@override
void initState() {
super.initState();
_initSavePath();
}
Future<void> _initSavePath() async {
final path = join((await getApplicationSupportDirectory()).path,
"${(DateTime.now().millisecondsSinceEpoch / 1000).round()}.jpeg");
setState(() {
_savePath = path;
});
}
@override
Widget build(BuildContext context) {
if (_savePath == null) return const Scaffold(body: Center(child: CircularProgressIndicator()));
return Scaffold(
appBar: AppBar(title: const Text('Edge Detection Camera')),
body: Stack(
children: [
EdgeDetectionCamera(
controller: _controller,
saveTo: _savePath!,
autoCapture: true,
onCapture: (path) {
print("Captured to: $path");
Navigator.pop(context, path);
},
scanFrameBorderColor: "#19a55c",
scanFrameBodyColor: "#44ccebda",
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: FloatingActionButton(
onPressed: () => _controller.capture(),
child: const Icon(Icons.camera_alt),
),
),
),
],
),
);
}
}