Detect Barcode using Firebase ML kit
Introduction
So, I know Machine Learning is there in open and it’s been there for quite long. It goes means back in the previous century once it had been initially coined and brought into existence, currently, you discover it every place in existence therefore vital that your life is perhaps enclosed by it and you will even not understand, consider your smartphones they’re referred to as “smart” for a specific reason and nowadays they’re “smartest” while getting even more n more from day to day, as a result of they’re learning regarding you from you itself the additional you’re using it the more your smartphone gets to grasp you and every one of that’s simply the machine with lines of code evolving with time like humans. It’s not simply restricted to smartphones, it’s in-depth in technology.
Purpose of this article?
Nowadays every developer familiar with the term Machine Learning In this article, today we create a simple barcode reader application using Flutter and Firebase ML kit, also I explain all the steps in a simple manner so anybody can use the Firebase ML kit in a flutter project.
Setup Flutter SDK
If you don’t have a flutter set up in your machine so you have to need to install it first. Click here to learn how to set up flutter on Windows and for Linux click here.
Create a Firebase Project
For use firebase properties you have to need to register your app in firebase. First, integrate your flutter project to firebase, if you don’t know how to do it, read my Flutter Firebase Integration article.
Let’s Begin for the code.
- Add firebase_ml_vision and image_picker dependency in your project pubspec.yaml file.
dependencies: flutter: sdk: flutter firebase_ml_vision: <latest_version> image_picker: <latest_version>
firebase_ml_vision: This dependency used for access Firebase ML kit.
image_picker: This dependency used to get a barcode image.
- Add the below code in your main.dart file for import dependency.
import 'package:image_picker/image_picker.dart'; import 'package:firebase_ml_vision/firebase_ml_vision.dart';
Now we are ready for Coding the component.
Now we are ready to code the components, as in the below code I am taking barcode image from the gallery you can get from the camera also as you need, after getting the image we store all the barcodes in Barcode List and then print all the barcodes using a loop, you can perform any other task instead of print.
Future<void> getBarcodeImage() async { var image = await ImagePicker.pickImage(source: ImageSource.gallery); final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(image); final BarcodeDetector barcodeDetector = FirebaseVision.instance.barcodeDetector(); final List<Barcode> barcodes = await barcodeDetector.detectInImage(visionImage); for(int i=0;i<barcodes.length;i++) { String getBarcode=barcodes[i].rawValue; print(getBarcode); } }
Now call this function in your flutter application the same as calling other functions.
Get complete code click here.