Last active
January 29, 2020 08:13
-
-
Save ashishrawat2911/07c53c9a8eb16d36889ff8382dedbfae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright 2018 The Chromium Authors. All rights reserved. | |
| import 'dart:async'; | |
| import 'dart:typed_data'; | |
| import 'dart:ui'; | |
| import 'package:camera/camera.dart'; | |
| import 'package:firebase_ml_vision/firebase_ml_vision.dart'; | |
| import 'package:flutter/foundation.dart'; | |
| class ScannerUtils { | |
| ScannerUtils._(); | |
| static Future<CameraDescription> getCamera(CameraLensDirection dir) async { | |
| return await availableCameras().then( | |
| (List<CameraDescription> cameras) => cameras.firstWhere( | |
| (CameraDescription camera) => camera.lensDirection == dir, | |
| ), | |
| ); | |
| } | |
| static Future<dynamic> detect({ | |
| @required CameraImage image, | |
| @required Future<dynamic> Function(FirebaseVisionImage image) detectInImage, | |
| @required int imageRotation, | |
| }) async { | |
| return detectInImage( | |
| FirebaseVisionImage.fromBytes( | |
| _concatenatePlanes(image.planes), | |
| _buildMetaData(image, _rotationIntToImageRotation(imageRotation)), | |
| ), | |
| ); | |
| } | |
| static Uint8List _concatenatePlanes(List<Plane> planes) { | |
| final WriteBuffer allBytes = WriteBuffer(); | |
| for (Plane plane in planes) { | |
| allBytes.putUint8List(plane.bytes); | |
| } | |
| return allBytes.done().buffer.asUint8List(); | |
| } | |
| static FirebaseVisionImageMetadata _buildMetaData( | |
| CameraImage image, | |
| ImageRotation rotation, | |
| ) { | |
| return FirebaseVisionImageMetadata( | |
| rawFormat: image.format.raw, | |
| size: Size(image.width.toDouble(), image.height.toDouble()), | |
| rotation: rotation, | |
| planeData: image.planes.map( | |
| (Plane plane) { | |
| return FirebaseVisionImagePlaneMetadata( | |
| bytesPerRow: plane.bytesPerRow, | |
| height: plane.height, | |
| width: plane.width, | |
| ); | |
| }, | |
| ).toList(), | |
| ); | |
| } | |
| static ImageRotation _rotationIntToImageRotation(int rotation) { | |
| switch (rotation) { | |
| case 0: | |
| return ImageRotation.rotation0; | |
| case 90: | |
| return ImageRotation.rotation90; | |
| case 180: | |
| return ImageRotation.rotation180; | |
| default: | |
| assert(rotation == 270); | |
| return ImageRotation.rotation270; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment