Barcode Scanner using firebase ML + imageStream

215 views
Skip to first unread message

irvan hendrik

unread,
Aug 8, 2020, 5:04:56 PM8/8/20
to Flutter Development (flutter-dev)
hi, I have trouble on using Firebase ML.
Here is my code:

import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'dart:async';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
final BarcodeDetector _barcodeDetector =
FirebaseVision.instance.barcodeDetector();
String data = 'EMPTY DATA';

class ScanScreen extends StatefulWidget {
static String id = 'scan_screen';
//final CameraDescription camera = cameras.first;
@override
_ScanScreenState createState() => _ScanScreenState();
}

class _ScanScreenState extends State<ScanScreen> {
CameraController _controller;
Future<void> _initializeControllerFuture;
CameraImage _savedImage;
bool _cameraInitialized = false;
void _initializeCamera() async {
List<CameraDescription> cameras = await availableCameras();

_controller = CameraController(
cameras[0],
ResolutionPreset.medium,
);
_initializeControllerFuture = _controller.initialize().then((_) async {
// Start ImageStream
await _controller
.startImageStream((CameraImage image) => _processCameraImage(image));
setState(() {
_cameraInitialized = true;
});
});
}

void _processCameraImage(CameraImage image) async {
setState(() async {
_savedImage = image;
final barcodes =
await _barcodeDetector.detectInImage(FirebaseVisionImage.fromBytes(
_concatenatePlanes(image.planes),
FirebaseVisionImageMetadata(
rawFormat: image.format.raw,
size: Size(image.width.toDouble(), image.height.toDouble()),
rotation: ImageRotation.rotation0,
planeData: image.planes.map(
(plane) => FirebaseVisionImagePlaneMetadata(
bytesPerRow: plane.bytesPerRow,
height: plane.height,
width: plane.width,
),
),
),
));
if (barcodes!=null && barcodes.length>0)
{
print(barcodes);
data=barcodes.toString();
}
else{
print ('no barcode');
data='no barcode found';
}
});
}

Uint8List _concatenatePlanes(List<Plane> planes) {
final WriteBuffer allBytes = WriteBuffer();
planes.forEach((plane) => allBytes.putUint8List(plane.bytes));
return allBytes.done().buffer.asUint8List();
}

@override
void initState() {
super.initState();
_initializeCamera();
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
final deviceRatio = size.width / size.height;

return Scaffold(
appBar: AppBar(
title: Text('Scan Product'),
),
body: Container(
color: Colors.black,
child: Column(
children: [
Expanded(
flex: 2,
child: Stack(
children: [
MaterialApp(
home: Padding(
padding:
EdgeInsets.symmetric(horizontal: 7.0, vertical: 10.0),
child: AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: CameraPreview(_controller),
),
),
),
// Positioned.fill(
// child: Opacity(
// opacity: 0.3,
// child: Image.network(
// 'https://picsum.photos/3000/4000',
// fit: BoxFit.fill,
// ),
// ))
],
),
),
SizedBox(
height: 10.0,
),
Expanded(
flex: 2,
child: Container(
color: Colors.deepPurple,
child: Text(data,style: TextStyle(
fontSize: 50.0,
fontWeight: FontWeight.bold,
),),
),
),
],
),
),
);
}
}

I want to achieve:
where the top part is the camera and the bottom part is the barcode data captured from the camera above it..

Can any one help me solve this issue?
Thanks.
Irvan

Suzuki Tomohiro

unread,
Aug 8, 2020, 5:11:25 PM8/8/20
to irvan hendrik, Flutter Development (flutter-dev)
The problem is processCameraImage is never called unexpectedly. Is this correct?

Do you observe _cameraInitialized is set to true? (Set breakpoint to the line that sets the variable to true)


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/038f5643-e62e-4717-936b-f15c473f1322o%40googlegroups.com.

irvan hendrik

unread,
Aug 8, 2020, 5:35:44 PM8/8/20
to Flutter Development (flutter-dev)
I have the processCameraImage in the _initializedCamera function which run in initstate function.
Can you elaborate more on the error that you saw?

Thanks. 

Irvan. 
Reply all
Reply to author
Forward
0 new messages