Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import 'dart:io';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:image_picker/image_picker.dart';
  5. import 'package:firebase_ml_vision/firebase_ml_vision.dart';
  6.  
  7. void main() => runApp(MyApp());
  8.  
  9. class MyApp extends StatelessWidget {
  10. // This widget is the root of your application.
  11. @override
  12. Widget build(BuildContext context) {
  13. return MaterialApp(
  14. title: 'Flutter Demo',
  15. theme: ThemeData(
  16. primarySwatch: Colors.blue,
  17. ),
  18. home: MyHomePage(title: 'Flutter Demo Home Page'),
  19. );
  20. }
  21. }
  22.  
  23. class MyHomePage extends StatefulWidget {
  24. MyHomePage({Key key, this.title}) : super(key: key);
  25. final String title;
  26.  
  27. @override
  28. _MyHomePageState createState() => _MyHomePageState();
  29. }
  30.  
  31. class _MyHomePageState extends State<MyHomePage> {
  32. final BarcodeDetector barcodeDetector = FirebaseVision.instance.barcodeDetector();
  33. final FaceDetector faceDetector = FirebaseVision.instance.faceDetector();
  34.  
  35. File _image;
  36. List<Barcode> _barcodes;
  37. List<Face> _faces;
  38.  
  39. getImageFromGallary() {
  40. getImage(ImageSource.gallery);
  41. }
  42.  
  43. getImageFromCamera() {
  44. getImage(ImageSource.camera);
  45. }
  46.  
  47. Future getImage(ImageSource source) async {
  48. var image = await ImagePicker.pickImage(source: source);
  49.  
  50. setState((){
  51. _image = image;
  52. });
  53.  
  54.  
  55. // 取得した画像を元に機械学習の推論を実行する
  56.  
  57. if (image != null) {
  58. // 画像ファイル形式をFirebaseVisionImageに変換
  59. FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(image);
  60. // 画像からバーコードを検出
  61. List<Barcode> barcodes = await barcodeDetector.detectInImage(visionImage);
  62. if (barcodes.length > 0) {
  63. // バーコードの読み取り結果を格納
  64. setState(() => _barcodes = barcodes);
  65. }
  66. // 画像から顔を検出
  67. List<Face> faces = await faceDetector.processImage(visionImage);
  68. if (faces.length > 0) {
  69. // 顔の検出結果を格納
  70. setState(() => _faces = faces);
  71. }
  72.  
  73. }
  74. }
  75.  
  76. @override
  77. Widget build(BuildContext context) {
  78. String result = '<結果>\n';
  79.  
  80. // バーコードの認識結果
  81. if (_barcodes != null && _barcodes.length > 0) {
  82. result += 'バーコードの認識結果: ';
  83. _barcodes.forEach((barcode) {
  84. result += barcode.displayValue + ", ";
  85. });
  86. result += '\n';
  87. }
  88. if (_faces != null && _faces.length > 0) {
  89. result += '顔の認識結果: ';
  90. _faces.forEach((face) {
  91. var boundingBox = face.boundingBox;
  92. result += boundingBox.toString() + ', ';
  93. });
  94. result += '\n';
  95. }
  96. return Scaffold(
  97. appBar: AppBar(
  98. title: Text(widget.title),
  99. ),
  100. body: ListView(children: <Widget>[
  101. Center(
  102. child: _image == null ? Text('画像が選択されていません'):
  103. Image.file(_image)
  104. ),
  105. Text(
  106. result,
  107. overflow: TextOverflow.ellipsis,
  108. maxLines: 6
  109. ),
  110. ]),
  111. floatingActionButton: Column(
  112. crossAxisAlignment: CrossAxisAlignment.end,
  113. mainAxisAlignment: MainAxisAlignment.end,
  114. children: <Widget> [
  115. FloatingActionButton(
  116. onPressed: getImageFromCamera,
  117. tooltip: 'カメラ撮影',
  118. child:
  119. Icon(Icons.add_a_photo),
  120. ),
  121. FloatingActionButton(
  122. onPressed: getImageFromGallary,
  123. tooltip: '写真',
  124. child:
  125. Icon(Icons.photo_album),
  126. ),
  127.  
  128. ],
  129. ),
  130. );
  131. }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement