jiofidelus / codingparty

Association rule project in level 2, university of Yaounde I, department of computer science
10 stars 1 forks source link

Food annotation image app with Flutter - Guide lines #19

Open main-c opened 1 year ago

main-c commented 1 year ago

Food Annotation Image app with Flutter

This present guidelines of our coding party about build a food annotation image app with flutter.

Part 1 : Get a food image from gallery or camera

Step 1 : Setup flutter project

Step 2 : Install and use Image_picker package

[image_picker](https://pub.dev/packages/image_picker/) is a flutter package that use to pick image from gallery or camera.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const PickerImage(),
    );
  }
}
return Scaffold(
    body: Center(
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[]
        ),
    ),
);
Container(
    margin: const EdgeInsets.only(left: 100, right: 100),
    child: ElevatedButton(
        style: ElevatedButton.styleFrom(
            shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                    Radius.circular(10)),
                  ),
            padding: const EdgeInsets.only(
                top: 20, bottom: 20
            ),
        ),
        onPressed: () {},
        child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const <Widget>[
                Icon(Icons.camera_alt),
                SizedBox(width: 10),
                Text('Camera'),
            ],
        ),
    ),
),
Future pickImage() async {
    try {

      final image = await ImagePicker().pickImage(source: ImageSource.camera);
      if (image == null) return;

      final imageFinal = File(image.path);
      setState(() {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => DisplayImage(image: imageFinal)));
      });
    } on PlatformException catch (e) {
      print("Failed to pick image $e");
    }
  }
onPressed: () => pickImage(ImageSource.camera),
import 'package:flutter/material.dart';

class DisplayImage extends StatefulWidget {
  DisplayImage({Key? key, required this.image}) : super(key: key);

  File? image;

  @override
  State<DisplayImage> createState() => _DisplayImageState();
}

class _DisplayImageState extends State<DisplayImage> {
    final _imageKey = GlobalKey<ImagePainterState>();

  @override
  Widget build(BuildContext context) {
    return ImagePainter.file(
          widget.image!,
          key: _imageKey,
          scalable: true,
          textDelegate: TextDelegate(),
          initialPaintMode: PaintMode.freeStyle,
        ),
  }
}

Part 2: Integration of image painter package

Image painter is a popular flutter package that uses to paint on an image, we will uses customize it according of our needs

Step 1 : Clone the package

Step 2 : Add the package in the project

Part 3 : Adapt the package according to our wishes

At this level, you can already paint on an image picked from camera or from gallery, now we'll custom package by adding some form for naming a selection and save it.

Step 1 : Name a selection

The package give us some method like onColorChanged or onPaintModeChanged that are functions call when color or paint mode changed resp. We want to add an another function that will can when drawing on image end. In our case we will show a dialog with a form to name the current selection.

Step 2 : Get Coordinates of an annotation

Now that our app can register a food, we need to get coordinates of the current annotation

Image painter package defines the path of an annotation with the class PaintInfo define in lib/src/_image_painter.dart

Path 4 : Save in Json file

Now to that we can get coordinates of an annotation, the next steps is saving in a json file

Step 1: use path_provider to create a file in local storage

Step 2 : Call our method after submit an annotation

jiofidelus commented 1 year ago

Hello, Great job,

I hope that all the coders will be ready with great system on Friday

noutcheu1 commented 1 year ago

thank you for this tutorial but for everyone who uses this tutorial don't forget to add these dependencies but before tape this on your command line

flutter pub add image_painter 
and on pubspec.yaml add this 
dependencies:
   image_painter: latest
noutcheu1 commented 1 year ago

please in place of this

ImagePainter.file(
          widget.image!,
          key: _imageKey,
          scalable: true,
          textDelegate: TextDelegate(),
          initialPaintMode: PaintMode.freeStyle,
        ),

add this code if else you will have this error No Material widget was found due to his forgetting to add a Scaffold widget and container widget on your phone when you'll build the application


Scaffold(
        body:Container(
        padding: EdgeInsets.all(20),
        child:ImagePainter.file(
        widget.image!,
        key: _imageKey,
        scalable: true,
        textDelegate: TextDelegate(),
        initialPaintMode: PaintMode.freeStyle,
    )));
  }
main-c commented 1 year ago

thank you for this tutorial but for everyone who uses this tutorial don't forget to add these dependencies but before tape this on your command line

flutter pub add image_painter 
and on pubspec.yaml add this 
dependencies:
   image_painter: latest

You don't have to install it

noutcheu1 commented 1 year ago
//Constructor for loading image from [File].
  factory ImagePainter.file(
    File file, {
    ...
    AlertDialog? onDrawingEnd,
    ...
  }) {...

directly after adding this will you find it after the constructor

ImagePainter._(
    ....
      onDrawingEnd:onDrawingEnd,
    );

add this line also if else you will never initialize the Alertdialogue