flutter_camera_ml_vision 1.2.0 flutter_camera_ml_vision: ^1.2.0 copied to clipboard
A flutter widget that show the camera stream and allow ML vision recognition on it, it allow you to detect barcodes, labels, text, faces...
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:flutter/material.dart';
import 'package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> data = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
RaisedButton(
child: Text('Scan product'),
onPressed: () async {
final barcode = await Navigator.of(context).push<Barcode>(
MaterialPageRoute(
builder: (c) {
return ScanPage();
},
),
);
if (barcode == null) {
return;
}
setState(() {
data.add(barcode.displayValue);
});
},
),
Expanded(
child: ListView(
children: data.map((d) => Text(d)).toList(),
),
),
],
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class ScanPage extends StatefulWidget {
@override
_ScanPageState createState() => _ScanPageState();
}
class _ScanPageState extends State<ScanPage> {
bool resultSent = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SizedBox(
width: MediaQuery.of(context).size.width,
child: CameraMlVision<List<Barcode>>(
detector: FirebaseVision.instance.barcodeDetector().detectInImage,
onResult: (List<Barcode> barcodes) {
if (!mounted || resultSent) {
return;
}
resultSent = true;
Navigator.of(context).pop<Barcode>(barcodes.first);
},
),
),
),
);
}
}