I want to scan text with live camera . And for that I'm using google_ml_kit package for scanning. In ios platform everything is okay. But I cannot read data from CameraImage in android. And also I got this message :
'A camera has started streaming images., startImageStream was called while a camera was streaming images.'
My code is :
@override
void initState() {
super.initState();
controller = CameraController(widget.cameras[0], ResolutionPreset.medium,
enableAudio: false);
controller.initialize().then((_) {
if (!mounted) {
return;
}
setState(() {});
_timer = Timer.periodic(Duration(seconds: 3), (currentTimer) async {
await controller.startImageStream((CameraImage availableImage) async {
_scanText(availableImage);
});
});
});
}
void _scanText(CameraImage availableImage) async {
try {
if (_isScanBusy) return;
_isScanBusy = true;
final WriteBuffer allBytes = WriteBuffer();
for (Plane plane in availableImage.planes) {
allBytes.putUint8List(plane.bytes);
}
final bytes = allBytes.done().buffer.asUint8List();
final Size imageSize = Size(
availableImage.width.toDouble(), availableImage.height.toDouble());
final InputImageRotation imageRotation =
InputImageRotationMethods.fromRawValue(
widget.cameras[0].sensorOrientation) ??
InputImageRotation.Rotation_0deg;
final InputImageFormat inputImageFormat =
InputImageFormatMethods.fromRawValue(availableImage.format.raw) ??
InputImageFormat.YUV420;
final planeData = availableImage.planes.map(
(Plane plane) {
return InputImagePlaneMetadata(
bytesPerRow: plane.bytesPerRow,
height: plane.height,
width: plane.width,
);
},
).toList();
final inputImageData = InputImageData(
size: imageSize,
imageRotation: imageRotation,
inputImageFormat: inputImageFormat,
planeData: planeData,
);
final inputImage =
InputImage.fromBytes(bytes: bytes, inputImageData: inputImageData);
final textDetector = GoogleMlKit.vision.textDetector();
final RecognisedText recognisedText =
await textDetector.processImage(inputImage);
//do something
}
_isScanBusy = false;
} catch (e) {
print(e);
}
}