diff --git a/.gitignore b/.gitignore index b0b8269b..80efbc58 100644 --- a/.gitignore +++ b/.gitignore @@ -1,111 +1,14 @@ +.DS_Store +.dart_tool/ -# Created by https://www.toptal.com/developers/gitignore/api/flutter,macos,dart -# Edit at https://www.toptal.com/developers/gitignore?templates=flutter,macos,dart +.packages +.pub/ -### Dart ### -# See https://www.dartlang.org/guides/libraries/private-files +build/ # Files and directories created by pub .dart_tool/ .packages build/ # If you're building an application, you may want to check-in your pubspec.lock -pubspec.lock - -# Directory created by dartdoc -# If you don't generate documentation locally you can remove this line. -doc/api/ - -# Avoid committing generated Javascript files: -*.dart.js -*.info.json # Produced by the --dump-info flag. -*.js # When generated by dart2js. Don't specify *.js if your - # project includes source files written in JavaScript. -*.js_ -*.js.deps -*.js.map - -### Flutter ### -# Flutter/Dart/Pub related -**/doc/api/ -.flutter-plugins -.flutter-plugins-dependencies -.pub-cache/ -.pub/ -lib/generated_plugin_registrant.dart - -# Android related -**/android/**/gradle-wrapper.jar -**/android/.gradle -**/android/captures/ -**/android/gradlew -**/android/gradlew.bat -**/android/key.properties -**/android/local.properties -**/android/**/GeneratedPluginRegistrant.java -.idea/ - -# iOS/XCode related -**/ios/**/*.mode1v3 -**/ios/**/*.mode2v3 -**/ios/**/*.moved-aside -**/ios/**/*.pbxuser -**/ios/**/*.perspectivev3 -**/ios/**/*sync/ -**/ios/**/.sconsign.dblite -**/ios/**/.tags* -**/ios/**/.vagrant/ -**/ios/**/DerivedData/ -**/ios/**/Icon? -**/ios/**/Pods/ -**/ios/**/.symlinks/ -**/ios/**/profile -**/ios/**/xcuserdata -**/ios/.generated/ -**/ios/Flutter/App.framework -**/ios/Flutter/Flutter.framework -**/ios/Flutter/Flutter.podspec -**/ios/Flutter/Generated.xcconfig -**/ios/Flutter/app.flx -**/ios/Flutter/app.zip -**/ios/Flutter/flutter_assets/ -**/ios/Flutter/flutter_export_environment.sh -**/ios/ServiceDefinitions.json -**/ios/Runner/GeneratedPluginRegistrant.* - -# Exceptions to above rules. -!**/ios/**/default.mode1v3 -!**/ios/**/default.mode2v3 -!**/ios/**/default.pbxuser -!**/ios/**/default.perspectivev3 -!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages - -### macOS ### -# General -.DS_Store -.AppleDouble -.LSOverride - -# Icon must end with two \r -Icon - -# Thumbnails -._* - -# Files that might appear in the root of a volume -.DocumentRevisions-V100 -.fseventsd -.Spotlight-V100 -.TemporaryItems -.Trashes -.VolumeIcon.icns -.com.apple.timemachine.donotpresent - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk - -# End of https://www.toptal.com/developers/gitignore/api/flutter,macos,dart \ No newline at end of file +pubspec.lock \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/fast_barcode_scanner.iml b/.idea/fast_barcode_scanner.iml new file mode 100644 index 00000000..55d90fcd --- /dev/null +++ b/.idea/fast_barcode_scanner.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml new file mode 100644 index 00000000..fcbce964 --- /dev/null +++ b/.idea/libraries/Dart_SDK.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml new file mode 100644 index 00000000..53449dae --- /dev/null +++ b/.idea/libraries/Flutter_Plugins.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..5c94cb21 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..08b19081 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/fast_barcode_scanner/.metadata b/.metadata similarity index 100% rename from fast_barcode_scanner/.metadata rename to .metadata diff --git a/fast_barcode_scanner/.vscode/launch.json b/.vscode/launch.json similarity index 100% rename from fast_barcode_scanner/.vscode/launch.json rename to .vscode/launch.json diff --git a/fast_barcode_scanner/CHANGELOG.md b/CHANGELOG.md similarity index 100% rename from fast_barcode_scanner/CHANGELOG.md rename to CHANGELOG.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 24222e22..00000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,3 +0,0 @@ -Contributing to fast_barcode_scanner - -Contributions are welcome by submitting a PR for to be reviewed. \ No newline at end of file diff --git a/fast_barcode_scanner/LICENSE b/LICENSE similarity index 100% rename from fast_barcode_scanner/LICENSE rename to LICENSE diff --git a/README.md b/README.md index 8ab08e38..76025330 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,107 @@ -# Flutter Barcode Scanner Plugin - -## Packages - -### fast_barcode_scanner: -code for the cross-platform facing plugin, used to display a camera view within Flutter applications - -[![pub package](https://img.shields.io/pub/v/fast_barcode_scanner.svg)](https://pub.dartlang.org/packages/fast_barcode_scanner) - -### fast_barcode_scanner_platform_interface: -code for the common platform interface - -[![pub package](https://img.shields.io/pub/v/fast_barcode_scanner_platform_interface.svg)](https://pub.dartlang.org/packages/fast_barcode_scanner_platform_interface) - -Most developers are likely here as they are looking to use the fast_barcode_scanner plugin. There is a readme file within each directory with more information. - - -## Issues - -If you run into bugs, please raise them on the GitHub repository. -Please do not email them to me, as GitHub is the appropriate place for them and allows for members of the community to answer questions, particularly if I miss the email. - -It would also be much appreciated if issues could be limited to actual bugs or feature requests. -If you're looking at how you could use the plugin to do a particular kind of notification, check the example app, which provides detailed code samples for each supported feature. -Also try to check the READMEs first in case you have missed something e.g. platform-specific setup. - - -## Contributions - -The guidelines around submitting pull requests can be found in [CONTRIBUTING.md](CONTRIBUTING.md). +# fast_barcode_scanner + +[![pub package](https://img.shields.io/pub/v/fast_barcode_scanner)](https://pub.dev/packages/fast_barcode_scanner) + +A fast barcode scanner using **MLKit** (and **CameraX**) on Android and **AVFoundation** on iOS. This package leaves the UI up to the user, but rather gives an access to a camera preview. + +*Note*: This plugin is still under development, and some APIs might not be available yet. If you have any issues, ideas or recommendendations, don't hesitate to create an issue or pull request on github. I am using this plugin in production myself and will actively develop and maintain it going forward. + +**This plugin required iOS 10.0 and Android sdk version 21 or higher.** + +## Installation +Add the following line to your **pubspec.yaml**: +```yaml +fast_barcode_scanner: ^1.1.0 +``` +### iOS +Add the `NSCameraUsageDescription` key to your `ios/Runner/Info.plist`, like so: +```xml +NSCameraUsageDescription +This app requires access to your phone’s camera solely for scanning barcodes +``` + +### Android +Change the minimum Android sdk version to 21 (or higher) in your `android/app/build.gradle` file. +``` +minSdkVersion 21 +``` + +## Usage +The barcode scanner consists of two main classes `CameraController` and `BarcodeCamera`. +A full example looks like this: +```dart +import 'package:fast_barcode_scanner/fast_barcode_scanner.dart'; + +class MyScannerScreen extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: Text('Barcode Scanner')), + body: BarcodeCamera( + types: const [ + BarcodeType.ean8, + BarcodeType.ean13, + BarcodeType.code128 + ], + resolution: Resolution.hd720, + framerate: Framerate.fps30, + mode: DetectionMode.pauseVideo, + onScan: (code) => print(code), + children: [ + MaterialPreviewOverlay(animateDetection: false), + BlurPreviewOverlay(), + Positioned( + child: ElevatedButton( + onPressed: () => + CameraController.instance.resumeDetector(), + child: Text('Resume'), + ), + ) + ], + ) + ) + } +} +``` +As you can see, there are two overlays in the childrens list. These two are included in the package. `MaterialPreviewOverlay` mimics the official [material barcode scanning example](https://material.io/design/machine-learning/barcode-scanning.html#usage). `BlurPreviewOverlay` blurs the screen when a barcode is detected and unblurs it on resuming. These are normal widget, which are shown above the camera preview. Look at their source code to find out, how to react to events from the barcode scanner. + +### CameraController +The `CameraController`-singleton manages the camera. It handles all the low level stuff like communicating with native code. It is implemented as a singleton to guarantee that there is always one and the same controller managing the camera. You can access the controller via the `CameraController.instance` attribute. These are the accessible methods: + +method |Description +----------------|------------------------------------------------- +`initialize` | Initialized the scanner with the provided config +`pauseDetector` | Actively pauses the scanner +`resumeDetector`| Resumes the scanner from the paused state +`toggleTorch` | toggles the torch on and off +`dispose` | Stops and resets the camera on platform level + +You do not have to call `initialize` yourself, if you use the `BarcodeCamera` widget. + +### CameraState +`CameraController.instance.state` contains the current state of the scanner. +You can use it to build your own overlay. The following information can be accessed: + +Attribute | Description +----------------|------------------------------------------------- +`isInitialized` | Indicated whether the camera is currently initialized +`previewConfig` | A `PreviewConfiguration` that is currently used +`eventNotifier` | A event notifier to react to init or detecting codes +`torchState` | The current state of the torch (on/off) +`hasError` | Indicates whether `error` is null or not +`error` | Access the error produced last + +### BarcodeCamera +The `BarcodeCamera` is a widget showing a preview of the camera feed. It calls the `CameraController` in the background for initialization and configuration of the barcode camera. + +An overview of all possible configurations (either passed to `BarcodeCamera` or `CameraController.initialize`): + +Attribute |Description +-------------|--------------------------------------------------------- +`types` | See code types to scan (see `BarcodeType`) +`mode` | Whether to pause the camera on detection +`resolution` | The resolution of the camera feed +`framerate` | The framerate of the camera feed +`position` | Choose between back and front camera (iOS) +`onScan` | The callback when a barcode is scanned +`children` | Widgets to display on top of the preview diff --git a/fast_barcode_scanner/analysis_options.yaml b/analysis_options.yaml similarity index 100% rename from fast_barcode_scanner/analysis_options.yaml rename to analysis_options.yaml diff --git a/fast_barcode_scanner/android/.gitignore b/android/.gitignore similarity index 100% rename from fast_barcode_scanner/android/.gitignore rename to android/.gitignore diff --git a/fast_barcode_scanner/android/build.gradle b/android/build.gradle similarity index 93% rename from fast_barcode_scanner/android/build.gradle rename to android/build.gradle index 61e24b96..009e2191 100644 --- a/fast_barcode_scanner/android/build.gradle +++ b/android/build.gradle @@ -54,7 +54,7 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" def camerax_version = "1.1.0-alpha10" - def mlkit_version = "17.0.0" + def mlkit_version = "17.0.2" implementation "androidx.camera:camera-camera2:$camerax_version" implementation "androidx.camera:camera-lifecycle:$camerax_version" diff --git a/fast_barcode_scanner/android/gradle.properties b/android/gradle.properties similarity index 100% rename from fast_barcode_scanner/android/gradle.properties rename to android/gradle.properties diff --git a/fast_barcode_scanner/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties similarity index 100% rename from fast_barcode_scanner/android/gradle/wrapper/gradle-wrapper.properties rename to android/gradle/wrapper/gradle-wrapper.properties diff --git a/fast_barcode_scanner/android/settings.gradle b/android/settings.gradle similarity index 100% rename from fast_barcode_scanner/android/settings.gradle rename to android/settings.gradle diff --git a/fast_barcode_scanner/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml similarity index 100% rename from fast_barcode_scanner/android/src/main/AndroidManifest.xml rename to android/src/main/AndroidManifest.xml diff --git a/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt b/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt new file mode 100644 index 00000000..681c1e33 --- /dev/null +++ b/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt @@ -0,0 +1,298 @@ +package com.jhoogstraat.fast_barcode_scanner + +import android.Manifest +import android.app.Activity +import android.content.pm.PackageManager +import android.util.Log +import android.view.Surface +import androidx.camera.core.* +import androidx.camera.lifecycle.ProcessCameraProvider +import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat +import androidx.core.util.Consumer +import androidx.lifecycle.LifecycleOwner +import com.google.android.gms.tasks.OnFailureListener +import com.google.android.gms.tasks.OnSuccessListener +import com.google.mlkit.vision.barcode.BarcodeScannerOptions +import com.google.mlkit.vision.barcode.common.Barcode +import io.flutter.plugin.common.MethodChannel.Result +import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener +import io.flutter.view.TextureRegistry +import java.util.ArrayList +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + +data class CameraConfig( + val formats: IntArray, + val mode: DetectionMode, + val resolution: Resolution, + val framerate: Framerate, + var position: CameraPosition +) + +class BarcodeReader( + private val flutterTextureEntry: TextureRegistry.SurfaceTextureEntry, + private val listener: (List) -> Unit +) : RequestPermissionsResultListener { + /* Android Lifecycle */ + private var activity: Activity? = null + + /* Camera */ + private lateinit var camera: Camera + private lateinit var cameraConfig: CameraConfig + private lateinit var cameraProvider: ProcessCameraProvider + private lateinit var cameraSelector: CameraSelector + private lateinit var cameraExecutor: ExecutorService + private lateinit var cameraSurfaceProvider: Preview.SurfaceProvider + + /* ML Kit */ + private lateinit var barcodeDetector: MLKitBarcodeDetector + + /* State */ + private var isInitialized = false + private var pauseDetection = false + private var pendingResult: Result? = null + + fun attachToActivity(activity: Activity) { + this.activity = activity + } + + fun detachFromActivity() { + stop(null) + this.activity = null + } + + fun start(args: HashMap, result: Result) { + // Make sure we are connected to an activity + if (activity == null) return result.error("0", "Activity not connected!", null) + + // Stop running camera and start new + stop(null) + + // Reset init state. + isInitialized = false + + // Convert arguments to CameraConfig + cameraConfig = + CameraConfig( + (args["types"] as ArrayList) + .map { barcodeFormatMap[it]!! } + .toIntArray(), + DetectionMode.valueOf(args["mode"] as String), + Resolution.valueOf(args["res"] as String), + Framerate.valueOf(args["fps"] as String), + CameraPosition.valueOf(args["pos"] as String) + ) + + if (allPermissionsGranted()) { + initCamera() + result.success( + hashMapOf( + "textureId" to flutterTextureEntry.id(), + "surfaceOrientation" to 0, + "surfaceHeight" to 1280, + "surfaceWidth" to 720 + ) + ) + } else { + pendingResult = result + ActivityCompat.requestPermissions( + activity!!, + REQUIRED_PERMISSIONS, + REQUEST_CODE_PERMISSIONS + ) + } + } + + fun stop(result: Result? = null) { + if (!isInitialized) return + cameraProvider.unbindAll() + result?.success(null) + } + + fun resume(result: Result) { + if (!isInitialized) return + bindCameraUseCases() + result.success(null) + } + + fun toggleTorch(result: Result) { + if (!isInitialized) return + camera.cameraControl + .enableTorch(camera.cameraInfo.torchState.value != TorchState.ON) + .addListener( + Runnable { + result.success(camera.cameraInfo.torchState.value == TorchState.ON) + }, + ContextCompat.getMainExecutor(activity) + ) + } + + fun canChangeCamera(result: Result) { + try { + val cameraProviderFuture = ProcessCameraProvider.getInstance(activity!!) + cameraProviderFuture.addListener( + Runnable { + val cameraProviderForChangeCamera = cameraProviderFuture.get() + val hasFrontCamera = + cameraProviderForChangeCamera.hasCamera( + CameraSelector.DEFAULT_FRONT_CAMERA + ) + val hasBackCamera = + cameraProviderForChangeCamera.hasCamera( + CameraSelector.DEFAULT_BACK_CAMERA + ) + result.success(hasFrontCamera && hasBackCamera) + }, + ContextCompat.getMainExecutor(activity!!) + ) + } catch (exc: Exception) { + result.success(false) + } + } + + fun changeCamera(position: String, result: Result) { + cameraConfig.position = + when (position) { + "front" -> CameraPosition.front + else -> CameraPosition.back + } + initCamera() + } + + private fun allPermissionsGranted() = + REQUIRED_PERMISSIONS.all { + ContextCompat.checkSelfPermission(activity!!.applicationContext, it) == + PackageManager.PERMISSION_GRANTED + } + + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, + grantResults: IntArray + ): Boolean { + if (requestCode == REQUEST_CODE_PERMISSIONS) { + if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) { + initCamera() + } else { + pendingResult?.let { + it.error( + "UNAUTHORIZED", + "The application is not authorized to use the camera device", + null + ) + pendingResult = null + } + } + } + + return true + } + + private fun initCamera() { + // Init barcode Detector + val options = + BarcodeScannerOptions.Builder().setBarcodeFormats(0, *cameraConfig.formats).build() + + barcodeDetector = + MLKitBarcodeDetector( + options, + OnSuccessListener { codes -> + if (!pauseDetection && codes.isNotEmpty()) { + if (cameraConfig.mode == DetectionMode.pauseDetection) { + pauseDetection = true + } else if (cameraConfig.mode == DetectionMode.pauseVideo) { + stop() + } + + listener(codes) + } + }, + OnFailureListener { Log.e(TAG, "Error in MLKit", it) } + ) + + // Select camera + val selectorBuilder = CameraSelector.Builder() + when (cameraConfig.position) { + CameraPosition.front -> + selectorBuilder.requireLensFacing(CameraSelector.LENS_FACING_FRONT) + CameraPosition.back -> + selectorBuilder.requireLensFacing(CameraSelector.LENS_FACING_BACK) + } + cameraSelector = selectorBuilder.build() + + // Create Camera Thread + cameraExecutor = Executors.newSingleThreadExecutor() + + // Setup Surface + cameraSurfaceProvider = + Preview.SurfaceProvider { + val surfaceTexture = flutterTextureEntry.surfaceTexture() + surfaceTexture.setDefaultBufferSize(it.resolution.width, it.resolution.height) + it.provideSurface(Surface(surfaceTexture), cameraExecutor, Consumer {}) + } + + val cameraProviderFuture = ProcessCameraProvider.getInstance(activity!!) + cameraProviderFuture.addListener( + Runnable { + cameraProvider = cameraProviderFuture.get() + isInitialized = true + try { + bindCameraUseCases() + } catch (exc: Exception) { + Log.e(TAG, "Use case binding failed", exc) + } + }, + ContextCompat.getMainExecutor(activity!!) + ) + } + + private fun bindCameraUseCases() { + // Preview + val preview = + Preview.Builder() + .setTargetAspectRatio(AspectRatio.RATIO_16_9) + // .setTargetResolution(cameraConfig.resolution.size()) + .setTargetRotation(Surface.ROTATION_90) + .build() + + val imageAnalyzer = + ImageAnalysis.Builder() + .setTargetAspectRatio(AspectRatio.RATIO_16_9) + // .setTargetResolution(cameraConfig.resolution.size()) + .setTargetRotation(Surface.ROTATION_90) + .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) + // .also { + // Set Framerate via Camera2 Interop + // val interop = Camera2Interop.Extender(analyserBuilder) + // + // interop.setCaptureRequestOption(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, Range(cameraConfig.framerate.intValue(), cameraConfig.framerate.intValue())) + // } + .build() + .also { it.setAnalyzer(cameraExecutor, barcodeDetector) } + + // As required by CameraX, unbinds all use cases before trying to re-bind any of them. + cameraProvider.unbindAll() + + // Attach the viewfinder's surface provider to preview use case + preview.setSurfaceProvider(cameraExecutor, cameraSurfaceProvider) + + // Bind camera to Lifecycle + camera = + cameraProvider.bindToLifecycle( + activity!! as LifecycleOwner, + cameraSelector, + preview, + imageAnalyzer + ) + + // Make sure detections are allowed + pauseDetection = false + } + + companion object { + private const val TAG = "fast_barcode_scanner" + private const val REQUEST_CODE_PERMISSIONS = 10 + private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA) + } +} diff --git a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/FastBarcodeScannerPlugin.kt b/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/FastBarcodeScannerPlugin.kt similarity index 100% rename from fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/FastBarcodeScannerPlugin.kt rename to android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/FastBarcodeScannerPlugin.kt diff --git a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/MLKitBarcodeDetector.kt b/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/MLKitBarcodeDetector.kt similarity index 85% rename from fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/MLKitBarcodeDetector.kt rename to android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/MLKitBarcodeDetector.kt index 7ee2707f..b47e9186 100644 --- a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/MLKitBarcodeDetector.kt +++ b/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/MLKitBarcodeDetector.kt @@ -5,9 +5,9 @@ import androidx.camera.core.ImageAnalysis import androidx.camera.core.ImageProxy import com.google.android.gms.tasks.OnFailureListener import com.google.android.gms.tasks.OnSuccessListener -import com.google.mlkit.vision.barcode.Barcode import com.google.mlkit.vision.barcode.BarcodeScannerOptions import com.google.mlkit.vision.barcode.BarcodeScanning +import com.google.mlkit.vision.barcode.common.Barcode import com.google.mlkit.vision.common.InputImage class MLKitBarcodeDetector( @@ -19,10 +19,11 @@ class MLKitBarcodeDetector( @ExperimentalGetImage override fun analyze(imageProxy: ImageProxy) { - val inputImage = InputImage.fromMediaImage(imageProxy.image!!, imageProxy.imageInfo.rotationDegrees); + val inputImage = + InputImage.fromMediaImage(imageProxy.image!!, imageProxy.imageInfo.rotationDegrees) scanner.process(inputImage) .addOnSuccessListener(successListener) .addOnFailureListener(failureListener) .addOnCompleteListener { imageProxy.close() } } -} \ No newline at end of file +} diff --git a/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/Types.kt b/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/Types.kt new file mode 100644 index 00000000..4fefb356 --- /dev/null +++ b/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/Types.kt @@ -0,0 +1,76 @@ +package com.jhoogstraat.fast_barcode_scanner + +import android.util.Size +import com.google.mlkit.vision.barcode.common.Barcode + +enum class Framerate { + fps30, + fps60, + fps120, + fps240; + + fun intValue(): Int = + when (this) { + fps30 -> 30 + fps60 -> 60 + fps120 -> 120 + fps240 -> 240 + } + + fun duration(): Long = 1 / intValue().toLong() +} + +enum class Resolution { + sd480, + hd720, + hd1080, + hd4k; + + fun width(): Int = + when (this) { + sd480 -> 640 + hd720 -> 1280 + hd1080 -> 1920 + hd4k -> 3840 + } + + fun height(): Int = + when (this) { + sd480 -> 360 + hd720 -> 720 + hd1080 -> 1080 + hd4k -> 2160 + } + + fun size(): Size = Size(width(), height()) +} + +enum class DetectionMode { + pauseDetection, + pauseVideo, + continuous +} + +enum class CameraPosition { + front, + back +} + +val barcodeFormatMap = + hashMapOf( + "aztec" to Barcode.FORMAT_AZTEC, + "code128" to Barcode.FORMAT_CODE_128, + "code39" to Barcode.FORMAT_CODE_39, + "code93" to Barcode.FORMAT_CODE_93, + "codabar" to Barcode.FORMAT_CODABAR, + "dataMatrix" to Barcode.FORMAT_DATA_MATRIX, + "ean13" to Barcode.FORMAT_EAN_13, + "ean8" to Barcode.FORMAT_EAN_8, + "itf" to Barcode.FORMAT_ITF, + "pdf417" to Barcode.FORMAT_PDF417, + "qr" to Barcode.FORMAT_QR_CODE, + "upcA" to Barcode.FORMAT_UPC_A, + "upcE" to Barcode.FORMAT_UPC_E + ) + +val barcodeStringMap = barcodeFormatMap.entries.associateBy({ it.value }) { it.key } diff --git a/fast_barcode_scanner/example/.gitignore b/example/.gitignore similarity index 100% rename from fast_barcode_scanner/example/.gitignore rename to example/.gitignore diff --git a/fast_barcode_scanner/example/.metadata b/example/.metadata similarity index 100% rename from fast_barcode_scanner/example/.metadata rename to example/.metadata diff --git a/fast_barcode_scanner/example/README.md b/example/README.md similarity index 100% rename from fast_barcode_scanner/example/README.md rename to example/README.md diff --git a/fast_barcode_scanner/example/analysis_options.yaml b/example/analysis_options.yaml similarity index 100% rename from fast_barcode_scanner/example/analysis_options.yaml rename to example/analysis_options.yaml diff --git a/fast_barcode_scanner/example/android/.gitignore b/example/android/.gitignore similarity index 100% rename from fast_barcode_scanner/example/android/.gitignore rename to example/android/.gitignore diff --git a/fast_barcode_scanner/example/android/app/build.gradle b/example/android/app/build.gradle similarity index 100% rename from fast_barcode_scanner/example/android/app/build.gradle rename to example/android/app/build.gradle diff --git a/fast_barcode_scanner/example/android/app/src/debug/AndroidManifest.xml b/example/android/app/src/debug/AndroidManifest.xml similarity index 100% rename from fast_barcode_scanner/example/android/app/src/debug/AndroidManifest.xml rename to example/android/app/src/debug/AndroidManifest.xml diff --git a/fast_barcode_scanner/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/AndroidManifest.xml rename to example/android/app/src/main/AndroidManifest.xml diff --git a/fast_barcode_scanner/example/android/app/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner_example/MainActivity.kt b/example/android/app/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner_example/MainActivity.kt similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner_example/MainActivity.kt rename to example/android/app/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner_example/MainActivity.kt diff --git a/fast_barcode_scanner/example/android/app/src/main/res/drawable-v21/launch_background.xml b/example/android/app/src/main/res/drawable-v21/launch_background.xml similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/res/drawable-v21/launch_background.xml rename to example/android/app/src/main/res/drawable-v21/launch_background.xml diff --git a/fast_barcode_scanner/example/android/app/src/main/res/drawable/launch_background.xml b/example/android/app/src/main/res/drawable/launch_background.xml similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/res/drawable/launch_background.xml rename to example/android/app/src/main/res/drawable/launch_background.xml diff --git a/fast_barcode_scanner/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png b/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png rename to example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png diff --git a/fast_barcode_scanner/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png b/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png rename to example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png diff --git a/fast_barcode_scanner/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png rename to example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png diff --git a/fast_barcode_scanner/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png rename to example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png diff --git a/fast_barcode_scanner/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png rename to example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png diff --git a/fast_barcode_scanner/example/android/app/src/main/res/values-night/styles.xml b/example/android/app/src/main/res/values-night/styles.xml similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/res/values-night/styles.xml rename to example/android/app/src/main/res/values-night/styles.xml diff --git a/fast_barcode_scanner/example/android/app/src/main/res/values/styles.xml b/example/android/app/src/main/res/values/styles.xml similarity index 100% rename from fast_barcode_scanner/example/android/app/src/main/res/values/styles.xml rename to example/android/app/src/main/res/values/styles.xml diff --git a/fast_barcode_scanner/example/android/app/src/profile/AndroidManifest.xml b/example/android/app/src/profile/AndroidManifest.xml similarity index 100% rename from fast_barcode_scanner/example/android/app/src/profile/AndroidManifest.xml rename to example/android/app/src/profile/AndroidManifest.xml diff --git a/fast_barcode_scanner/example/android/build.gradle b/example/android/build.gradle similarity index 100% rename from fast_barcode_scanner/example/android/build.gradle rename to example/android/build.gradle diff --git a/fast_barcode_scanner/example/android/gradle.properties b/example/android/gradle.properties similarity index 100% rename from fast_barcode_scanner/example/android/gradle.properties rename to example/android/gradle.properties diff --git a/fast_barcode_scanner/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties similarity index 100% rename from fast_barcode_scanner/example/android/gradle/wrapper/gradle-wrapper.properties rename to example/android/gradle/wrapper/gradle-wrapper.properties diff --git a/fast_barcode_scanner/example/android/settings.gradle b/example/android/settings.gradle similarity index 100% rename from fast_barcode_scanner/example/android/settings.gradle rename to example/android/settings.gradle diff --git a/fast_barcode_scanner/example/ios/.gitignore b/example/ios/.gitignore similarity index 100% rename from fast_barcode_scanner/example/ios/.gitignore rename to example/ios/.gitignore diff --git a/fast_barcode_scanner/example/ios/Flutter/AppFrameworkInfo.plist b/example/ios/Flutter/AppFrameworkInfo.plist similarity index 100% rename from fast_barcode_scanner/example/ios/Flutter/AppFrameworkInfo.plist rename to example/ios/Flutter/AppFrameworkInfo.plist diff --git a/fast_barcode_scanner/example/ios/Flutter/Debug.xcconfig b/example/ios/Flutter/Debug.xcconfig similarity index 100% rename from fast_barcode_scanner/example/ios/Flutter/Debug.xcconfig rename to example/ios/Flutter/Debug.xcconfig diff --git a/fast_barcode_scanner/example/ios/Flutter/Release.xcconfig b/example/ios/Flutter/Release.xcconfig similarity index 100% rename from fast_barcode_scanner/example/ios/Flutter/Release.xcconfig rename to example/ios/Flutter/Release.xcconfig diff --git a/fast_barcode_scanner/example/ios/Podfile b/example/ios/Podfile similarity index 100% rename from fast_barcode_scanner/example/ios/Podfile rename to example/ios/Podfile diff --git a/fast_barcode_scanner/example/ios/Podfile.lock b/example/ios/Podfile.lock similarity index 100% rename from fast_barcode_scanner/example/ios/Podfile.lock rename to example/ios/Podfile.lock diff --git a/fast_barcode_scanner/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj similarity index 100% rename from fast_barcode_scanner/example/ios/Runner.xcodeproj/project.pbxproj rename to example/ios/Runner.xcodeproj/project.pbxproj diff --git a/fast_barcode_scanner/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata similarity index 100% rename from fast_barcode_scanner/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata rename to example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata diff --git a/fast_barcode_scanner/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist similarity index 100% rename from fast_barcode_scanner/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist rename to example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/fast_barcode_scanner/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings similarity index 100% rename from fast_barcode_scanner/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings rename to example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings diff --git a/fast_barcode_scanner/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme similarity index 100% rename from fast_barcode_scanner/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme rename to example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme diff --git a/fast_barcode_scanner/example/ios/Runner.xcworkspace/contents.xcworkspacedata b/example/ios/Runner.xcworkspace/contents.xcworkspacedata similarity index 100% rename from fast_barcode_scanner/example/ios/Runner.xcworkspace/contents.xcworkspacedata rename to example/ios/Runner.xcworkspace/contents.xcworkspacedata diff --git a/fast_barcode_scanner/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist similarity index 100% rename from fast_barcode_scanner/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist rename to example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/fast_barcode_scanner/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings similarity index 100% rename from fast_barcode_scanner/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings rename to example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings diff --git a/fast_barcode_scanner/example/ios/Runner/AppDelegate.swift b/example/ios/Runner/AppDelegate.swift similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/AppDelegate.swift rename to example/ios/Runner/AppDelegate.swift diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png rename to example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json rename to example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png rename to example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png rename to example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png rename to example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png diff --git a/fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md rename to example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md diff --git a/fast_barcode_scanner/example/ios/Runner/Base.lproj/LaunchScreen.storyboard b/example/ios/Runner/Base.lproj/LaunchScreen.storyboard similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Base.lproj/LaunchScreen.storyboard rename to example/ios/Runner/Base.lproj/LaunchScreen.storyboard diff --git a/fast_barcode_scanner/example/ios/Runner/Base.lproj/Main.storyboard b/example/ios/Runner/Base.lproj/Main.storyboard similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Base.lproj/Main.storyboard rename to example/ios/Runner/Base.lproj/Main.storyboard diff --git a/fast_barcode_scanner/example/ios/Runner/Info.plist b/example/ios/Runner/Info.plist similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Info.plist rename to example/ios/Runner/Info.plist diff --git a/fast_barcode_scanner/example/ios/Runner/Runner-Bridging-Header.h b/example/ios/Runner/Runner-Bridging-Header.h similarity index 100% rename from fast_barcode_scanner/example/ios/Runner/Runner-Bridging-Header.h rename to example/ios/Runner/Runner-Bridging-Header.h diff --git a/fast_barcode_scanner/example/lib/detections_counter.dart b/example/lib/detections_counter.dart similarity index 100% rename from fast_barcode_scanner/example/lib/detections_counter.dart rename to example/lib/detections_counter.dart diff --git a/fast_barcode_scanner/example/lib/main.dart b/example/lib/main.dart similarity index 100% rename from fast_barcode_scanner/example/lib/main.dart rename to example/lib/main.dart diff --git a/fast_barcode_scanner/example/lib/scanner_screen.dart b/example/lib/scanner_screen.dart similarity index 100% rename from fast_barcode_scanner/example/lib/scanner_screen.dart rename to example/lib/scanner_screen.dart diff --git a/fast_barcode_scanner/example/pubspec.yaml b/example/pubspec.yaml similarity index 100% rename from fast_barcode_scanner/example/pubspec.yaml rename to example/pubspec.yaml diff --git a/fast_barcode_scanner/fast_barcode_scanner.iml b/fast_barcode_scanner.iml similarity index 100% rename from fast_barcode_scanner/fast_barcode_scanner.iml rename to fast_barcode_scanner.iml diff --git a/fast_barcode_scanner/.gitignore b/fast_barcode_scanner/.gitignore deleted file mode 100644 index e9dc58d3..00000000 --- a/fast_barcode_scanner/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -.DS_Store -.dart_tool/ - -.packages -.pub/ - -build/ diff --git a/fast_barcode_scanner/README.md b/fast_barcode_scanner/README.md deleted file mode 100644 index 76025330..00000000 --- a/fast_barcode_scanner/README.md +++ /dev/null @@ -1,107 +0,0 @@ -# fast_barcode_scanner - -[![pub package](https://img.shields.io/pub/v/fast_barcode_scanner)](https://pub.dev/packages/fast_barcode_scanner) - -A fast barcode scanner using **MLKit** (and **CameraX**) on Android and **AVFoundation** on iOS. This package leaves the UI up to the user, but rather gives an access to a camera preview. - -*Note*: This plugin is still under development, and some APIs might not be available yet. If you have any issues, ideas or recommendendations, don't hesitate to create an issue or pull request on github. I am using this plugin in production myself and will actively develop and maintain it going forward. - -**This plugin required iOS 10.0 and Android sdk version 21 or higher.** - -## Installation -Add the following line to your **pubspec.yaml**: -```yaml -fast_barcode_scanner: ^1.1.0 -``` -### iOS -Add the `NSCameraUsageDescription` key to your `ios/Runner/Info.plist`, like so: -```xml -NSCameraUsageDescription -This app requires access to your phone’s camera solely for scanning barcodes -``` - -### Android -Change the minimum Android sdk version to 21 (or higher) in your `android/app/build.gradle` file. -``` -minSdkVersion 21 -``` - -## Usage -The barcode scanner consists of two main classes `CameraController` and `BarcodeCamera`. -A full example looks like this: -```dart -import 'package:fast_barcode_scanner/fast_barcode_scanner.dart'; - -class MyScannerScreen extends StatelessWidget { - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(title: Text('Barcode Scanner')), - body: BarcodeCamera( - types: const [ - BarcodeType.ean8, - BarcodeType.ean13, - BarcodeType.code128 - ], - resolution: Resolution.hd720, - framerate: Framerate.fps30, - mode: DetectionMode.pauseVideo, - onScan: (code) => print(code), - children: [ - MaterialPreviewOverlay(animateDetection: false), - BlurPreviewOverlay(), - Positioned( - child: ElevatedButton( - onPressed: () => - CameraController.instance.resumeDetector(), - child: Text('Resume'), - ), - ) - ], - ) - ) - } -} -``` -As you can see, there are two overlays in the childrens list. These two are included in the package. `MaterialPreviewOverlay` mimics the official [material barcode scanning example](https://material.io/design/machine-learning/barcode-scanning.html#usage). `BlurPreviewOverlay` blurs the screen when a barcode is detected and unblurs it on resuming. These are normal widget, which are shown above the camera preview. Look at their source code to find out, how to react to events from the barcode scanner. - -### CameraController -The `CameraController`-singleton manages the camera. It handles all the low level stuff like communicating with native code. It is implemented as a singleton to guarantee that there is always one and the same controller managing the camera. You can access the controller via the `CameraController.instance` attribute. These are the accessible methods: - -method |Description -----------------|------------------------------------------------- -`initialize` | Initialized the scanner with the provided config -`pauseDetector` | Actively pauses the scanner -`resumeDetector`| Resumes the scanner from the paused state -`toggleTorch` | toggles the torch on and off -`dispose` | Stops and resets the camera on platform level - -You do not have to call `initialize` yourself, if you use the `BarcodeCamera` widget. - -### CameraState -`CameraController.instance.state` contains the current state of the scanner. -You can use it to build your own overlay. The following information can be accessed: - -Attribute | Description -----------------|------------------------------------------------- -`isInitialized` | Indicated whether the camera is currently initialized -`previewConfig` | A `PreviewConfiguration` that is currently used -`eventNotifier` | A event notifier to react to init or detecting codes -`torchState` | The current state of the torch (on/off) -`hasError` | Indicates whether `error` is null or not -`error` | Access the error produced last - -### BarcodeCamera -The `BarcodeCamera` is a widget showing a preview of the camera feed. It calls the `CameraController` in the background for initialization and configuration of the barcode camera. - -An overview of all possible configurations (either passed to `BarcodeCamera` or `CameraController.initialize`): - -Attribute |Description --------------|--------------------------------------------------------- -`types` | See code types to scan (see `BarcodeType`) -`mode` | Whether to pause the camera on detection -`resolution` | The resolution of the camera feed -`framerate` | The framerate of the camera feed -`position` | Choose between back and front camera (iOS) -`onScan` | The callback when a barcode is scanned -`children` | Widgets to display on top of the preview diff --git a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt b/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt deleted file mode 100644 index 5b6295a8..00000000 --- a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt +++ /dev/null @@ -1,237 +0,0 @@ -package com.jhoogstraat.fast_barcode_scanner - -import android.Manifest -import android.app.Activity -import android.content.pm.PackageManager -import android.util.Log -import android.view.Surface -import androidx.camera.core.* -import androidx.camera.lifecycle.ProcessCameraProvider -import androidx.core.app.ActivityCompat -import androidx.core.content.ContextCompat -import androidx.core.util.Consumer -import androidx.lifecycle.LifecycleOwner -import com.google.android.gms.tasks.OnFailureListener -import com.google.android.gms.tasks.OnSuccessListener -import com.google.mlkit.vision.barcode.Barcode -import com.google.mlkit.vision.barcode.BarcodeScannerOptions - -import io.flutter.plugin.common.MethodChannel.Result -import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener -import io.flutter.view.TextureRegistry -import java.util.ArrayList -import java.util.concurrent.ExecutorService -import java.util.concurrent.Executors - -data class CameraConfig(val formats: IntArray, val mode: DetectionMode, val resolution: Resolution, val framerate: Framerate, var position: CameraPosition) - -class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceTextureEntry, private val listener: (List) -> Unit) : RequestPermissionsResultListener { - /* Android Lifecycle */ - private var activity: Activity? = null - - /* Camera */ - private lateinit var camera: Camera - private lateinit var cameraConfig: CameraConfig - private lateinit var cameraProvider: ProcessCameraProvider - private lateinit var cameraSelector: CameraSelector - private lateinit var cameraExecutor: ExecutorService - private lateinit var cameraSurfaceProvider: Preview.SurfaceProvider - - /* ML Kit */ - private lateinit var barcodeDetector: MLKitBarcodeDetector - - /* State */ - private var isInitialized = false - private var pauseDetection = false - private var pendingResult: Result? = null - - fun attachToActivity(activity: Activity) { - this.activity = activity - } - - fun detachFromActivity() { - stop(null) - this.activity = null - } - - fun start(args: HashMap, result: Result) { - // Make sure we are connected to an activity - if (activity == null) - return result.error("0", "Activity not connected!", null) - - // Stop running camera and start new - stop(null) - - // Reset init state. - isInitialized = false - - // Convert arguments to CameraConfig - cameraConfig = CameraConfig( - (args["types"] as ArrayList).map { barcodeFormatMap[it]!! }.toIntArray(), - DetectionMode.valueOf(args["mode"] as String), - Resolution.valueOf(args["res"] as String), - Framerate.valueOf(args["fps"] as String), - CameraPosition.valueOf(args["pos"] as String) - ) - - if (allPermissionsGranted()) { - initCamera() - result.success(hashMapOf("textureId" to flutterTextureEntry.id(), "surfaceOrientation" to 0, "surfaceHeight" to 1280, "surfaceWidth" to 720)) - } else { - pendingResult = result - ActivityCompat.requestPermissions( - activity!!, - REQUIRED_PERMISSIONS, - REQUEST_CODE_PERMISSIONS - ) - } - } - - fun stop(result: Result? = null) { - if (!isInitialized) return - cameraProvider.unbindAll() - result?.success(null) - } - - fun resume(result: Result) { - if (!isInitialized) return - bindCameraUseCases() - result.success(null) - } - - fun toggleTorch(result: Result) { - if (!isInitialized) return - camera.cameraControl.enableTorch(camera.cameraInfo.torchState.value != TorchState.ON).addListener(Runnable { - result.success(camera.cameraInfo.torchState.value == TorchState.ON) - }, ContextCompat.getMainExecutor(activity)) - } - - fun canChangeCamera(result: Result) { - try { - val cameraProviderFuture = ProcessCameraProvider.getInstance(activity!!) - cameraProviderFuture.addListener(Runnable { - val cameraProviderForChangeCamera = cameraProviderFuture.get() - val hasFrontCamera = cameraProviderForChangeCamera.hasCamera(CameraSelector.DEFAULT_FRONT_CAMERA) - val hasBackCamera = cameraProviderForChangeCamera.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) - result.success(hasFrontCamera && hasBackCamera) - }, ContextCompat.getMainExecutor(activity!!)) - } catch (exc: Exception) { - result.success(false) - } - } - - fun changeCamera(position: String, result: Result) { - cameraConfig.position = when (position) { - "front" -> CameraPosition.front - else -> CameraPosition.back - } - initCamera() - } - - private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all { - ContextCompat.checkSelfPermission(activity!!.applicationContext, it) == PackageManager.PERMISSION_GRANTED - } - - override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray): Boolean { - if (requestCode == REQUEST_CODE_PERMISSIONS) { - if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) { - initCamera() - } else { - pendingResult?.let { - it.error("UNAUTHORIZED", "The application is not authorized to use the camera device", null) - pendingResult = null - } - } - } - - return true - } - - private fun initCamera() { - // Init barcode Detector - val options = BarcodeScannerOptions.Builder() - .setBarcodeFormats(0, *cameraConfig.formats) - .build() - - barcodeDetector = MLKitBarcodeDetector(options, OnSuccessListener { codes -> - if (!pauseDetection && codes.isNotEmpty()) { - if (cameraConfig.mode == DetectionMode.pauseDetection) { - pauseDetection = true - } else if (cameraConfig.mode == DetectionMode.pauseVideo) { - stop() - } - - listener(codes) - } - }, OnFailureListener { - Log.e(TAG, "Error in MLKit", it) - }) - - // Select camera - val selectorBuilder = CameraSelector.Builder() - when (cameraConfig.position) { - CameraPosition.front -> selectorBuilder.requireLensFacing(CameraSelector.LENS_FACING_FRONT) - CameraPosition.back -> selectorBuilder.requireLensFacing(CameraSelector.LENS_FACING_BACK) - } - cameraSelector = selectorBuilder.build() - - // Create Camera Thread - cameraExecutor = Executors.newSingleThreadExecutor() - - // Setup Surface - cameraSurfaceProvider = Preview.SurfaceProvider { - val surfaceTexture = flutterTextureEntry.surfaceTexture() - surfaceTexture.setDefaultBufferSize(it.resolution.width, it.resolution.height) - it.provideSurface(Surface(surfaceTexture), cameraExecutor, Consumer {}) - } - - val cameraProviderFuture = ProcessCameraProvider.getInstance(activity!!) - cameraProviderFuture.addListener(Runnable { - cameraProvider = cameraProviderFuture.get() - isInitialized = true - try { bindCameraUseCases() } - catch (exc: Exception) { Log.e(TAG, "Use case binding failed", exc) } - }, ContextCompat.getMainExecutor(activity!!)) - } - - private fun bindCameraUseCases() { - // Preview - val preview = Preview.Builder() - .setTargetAspectRatio(AspectRatio.RATIO_16_9) - //.setTargetResolution(cameraConfig.resolution.size()) - .setTargetRotation(Surface.ROTATION_90) - .build() - - val imageAnalyzer = ImageAnalysis.Builder() - .setTargetAspectRatio(AspectRatio.RATIO_16_9) - // .setTargetResolution(cameraConfig.resolution.size()) - .setTargetRotation(Surface.ROTATION_90) - .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) -// .also { -// Set Framerate via Camera2 Interop -// val interop = Camera2Interop.Extender(analyserBuilder) -// interop.setCaptureRequestOption(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, Range(cameraConfig.framerate.intValue(), cameraConfig.framerate.intValue())) -// } - .build() - .also { it.setAnalyzer(cameraExecutor, barcodeDetector) } - - // As required by CameraX, unbinds all use cases before trying to re-bind any of them. - cameraProvider.unbindAll() - - // Attach the viewfinder's surface provider to preview use case - preview.setSurfaceProvider(cameraExecutor, cameraSurfaceProvider) - - // Bind camera to Lifecycle - camera = cameraProvider.bindToLifecycle(activity!! as LifecycleOwner, cameraSelector, preview, imageAnalyzer) - - // Make sure detections are allowed - pauseDetection = false - } - - companion object { - private const val TAG = "fast_barcode_scanner" - private const val REQUEST_CODE_PERMISSIONS = 10 - private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA) - } - -} \ No newline at end of file diff --git a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/Types.kt b/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/Types.kt deleted file mode 100644 index 62be3354..00000000 --- a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/Types.kt +++ /dev/null @@ -1,63 +0,0 @@ -package com.jhoogstraat.fast_barcode_scanner - -import android.util.Size -import com.google.mlkit.vision.barcode.Barcode - -enum class Framerate { - fps30, fps60, fps120, fps240; - - fun intValue() : Int = when(this) { - fps30 -> 30 - fps60 -> 60 - fps120 -> 120 - fps240 -> 240 - } - - fun duration() : Long = 1 / intValue().toLong() -} - -enum class Resolution { - sd480, hd720, hd1080, hd4k; - - fun width() : Int = when(this) { - sd480 -> 640 - hd720 -> 1280 - hd1080 -> 1920 - hd4k -> 3840 - } - - fun height() : Int = when(this) { - sd480 -> 360 - hd720 -> 720 - hd1080 -> 1080 - hd4k -> 2160 - } - - fun size() : Size = Size(width(), height()) -} - -enum class DetectionMode { - pauseDetection, pauseVideo, continuous; -} - -enum class CameraPosition { - front, back; -} - -val barcodeFormatMap = hashMapOf( - "aztec" to Barcode.FORMAT_AZTEC, - "code128" to Barcode.FORMAT_CODE_128, - "code39" to Barcode.FORMAT_CODE_39, - "code93" to Barcode.FORMAT_CODE_93, - "codabar" to Barcode.FORMAT_CODABAR, - "dataMatrix" to Barcode.FORMAT_DATA_MATRIX, - "ean13" to Barcode.FORMAT_EAN_13, - "ean8" to Barcode.FORMAT_EAN_8, - "itf" to Barcode.FORMAT_ITF, - "pdf417" to Barcode.FORMAT_PDF417, - "qr" to Barcode.FORMAT_QR_CODE, - "upcA" to Barcode.FORMAT_UPC_A, - "upcE" to Barcode.FORMAT_UPC_E -) - -val barcodeStringMap = barcodeFormatMap.entries.associateBy({ it.value }) { it.key } \ No newline at end of file diff --git a/fast_barcode_scanner_platform_interface/CHANGELOG.md b/fast_barcode_scanner_platform_interface/CHANGELOG.md deleted file mode 100644 index 539a24d0..00000000 --- a/fast_barcode_scanner_platform_interface/CHANGELOG.md +++ /dev/null @@ -1,22 +0,0 @@ -## 1.0.4 - -* Smaller code cleanups -* Updated CameraX and ML Kit to latest versions. - -## 1.0.3 - -* Camera position now taken into account on Android. -* Fix DetectionMode.pauseDetection on Android. -* Updated CameraX to v1.1.0-alpha05 - -## 1.0.2 - -* Even more documentation. - -## 1.0.1 - -* Updated documentation. - -## 1.0.0 - -* Initial release. diff --git a/fast_barcode_scanner_platform_interface/LICENSE b/fast_barcode_scanner_platform_interface/LICENSE deleted file mode 100644 index 48ff54d5..00000000 --- a/fast_barcode_scanner_platform_interface/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2020 Joshua Hoogstraat - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file diff --git a/fast_barcode_scanner_platform_interface/README.md b/fast_barcode_scanner_platform_interface/README.md deleted file mode 100644 index a96a1376..00000000 --- a/fast_barcode_scanner_platform_interface/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# fast_barcode_scanner_platform_interface - -A fast barcode scanner using MLKit (and CameraX) on Android and AVFoundation on iOS. - -This package contains the contract for the fast_barcode_scanner plugin. \ No newline at end of file diff --git a/fast_barcode_scanner_platform_interface/analysis_options.yaml b/fast_barcode_scanner_platform_interface/analysis_options.yaml deleted file mode 100644 index a3be6b82..00000000 --- a/fast_barcode_scanner_platform_interface/analysis_options.yaml +++ /dev/null @@ -1 +0,0 @@ -include: package:flutter_lints/flutter.yaml \ No newline at end of file diff --git a/fast_barcode_scanner_platform_interface/lib/fast_barcode_scanner_platform_interface.dart b/fast_barcode_scanner_platform_interface/lib/fast_barcode_scanner_platform_interface.dart deleted file mode 100644 index 8d2b4c4b..00000000 --- a/fast_barcode_scanner_platform_interface/lib/fast_barcode_scanner_platform_interface.dart +++ /dev/null @@ -1,4 +0,0 @@ -export 'package:fast_barcode_scanner_platform_interface/src/fast_barcode_scanner_platform_interface.dart'; -export 'package:fast_barcode_scanner_platform_interface/src/types/barcode.dart'; -export 'package:fast_barcode_scanner_platform_interface/src/types/barcode_type.dart'; -export 'package:fast_barcode_scanner_platform_interface/src/types/preview_configuration.dart'; diff --git a/fast_barcode_scanner_platform_interface/lib/src/fast_barcode_scanner_platform_interface.dart b/fast_barcode_scanner_platform_interface/lib/src/fast_barcode_scanner_platform_interface.dart deleted file mode 100644 index b10ce0a3..00000000 --- a/fast_barcode_scanner_platform_interface/lib/src/fast_barcode_scanner_platform_interface.dart +++ /dev/null @@ -1,79 +0,0 @@ -import 'package:plugin_platform_interface/plugin_platform_interface.dart'; - -import 'types/barcode.dart'; -import 'types/barcode_type.dart'; -import 'types/preview_configuration.dart'; -import 'method_channel_fast_barcode_scanner.dart'; - -/// The interface that implementations of fast_barcode_scanner must implement. -/// -/// Platform implementations should extend this class rather than implement it as `fast_barcode_scanner` -/// does not consider newly added methods to be breaking changes. Extending this class -/// (using `extends`) ensures that the subclass will get the default implementation, while -/// platform implementations that `implements` this interface will be broken by newly added -/// [FastBarcodeScannerPlatform] methods. -abstract class FastBarcodeScannerPlatform extends PlatformInterface { - FastBarcodeScannerPlatform() : super(token: _token); - - static const Object _token = Object(); - - static FastBarcodeScannerPlatform _instance = - MethodChannelFastBarcodeScanner(); - - /// The default instance of [FastBarcodeScannerPlatform] to use. - /// - /// Defaults to [MethodChannelFastBarcodeScanner]. - static FastBarcodeScannerPlatform get instance => _instance; - - /// Platform specific plugins should set this with their own platform-specific - /// class that extends [FastBarcodeScannerPlatform] when they register themselves. - static set instance(FastBarcodeScannerPlatform instance) { - PlatformInterface.verifyToken(instance, _token); - _instance = instance; - } - - /// Returns a [PreviewConfiguration] containing the parameters with - /// which the camera is set up. - /// - Future init( - List types, - Resolution resolution, - Framerate framerate, - DetectionMode detectionMode, - CameraPosition position) { - throw UnimplementedError('init() has not been implemented'); - } - - /// Pauses the camera on the platform. - Future pause() { - throw UnimplementedError('pause() has not been implemented'); - } - - /// Resumes the camera from the paused state on the platform. - Future resume() { - throw UnimplementedError('resume() has not been implemented'); - } - - /// Stops and clears the camera ressources. - Future dispose() { - throw UnimplementedError('dispose() has not been implemented'); - } - - /// Toggles the torch, if available. - Future toggleTorch() { - throw UnimplementedError('toggleTorch() has not been implemented'); - } - - Future changeCamera(CameraPosition position) { - throw UnimplementedError('changeCamera() has not been implemented'); - } - - Future canChangeCamera() { - throw UnimplementedError('canChangeCamera() has not been implemented'); - } - - /// Set the method to be called when a barcode is detected - void setOnDetectHandler(void Function(Barcode) handler) { - throw UnimplementedError('setOnReadHandler() has not been implemented'); - } -} diff --git a/fast_barcode_scanner_platform_interface/lib/src/method_channel_fast_barcode_scanner.dart b/fast_barcode_scanner_platform_interface/lib/src/method_channel_fast_barcode_scanner.dart deleted file mode 100644 index 7d84da22..00000000 --- a/fast_barcode_scanner_platform_interface/lib/src/method_channel_fast_barcode_scanner.dart +++ /dev/null @@ -1,78 +0,0 @@ -import 'dart:async'; - -import 'package:flutter/foundation.dart'; -import 'package:flutter/services.dart'; - -import 'types/barcode.dart'; -import 'types/barcode_type.dart'; -import 'types/preview_configuration.dart'; -import 'fast_barcode_scanner_platform_interface.dart'; - -class MethodChannelFastBarcodeScanner extends FastBarcodeScannerPlatform { - static const MethodChannel _channel = - MethodChannel('com.jhoogstraat/fast_barcode_scanner'); - - void Function(Barcode)? _onDetectHandler; - - @override - Future init( - List types, - Resolution resolution, - Framerate framerate, - DetectionMode detectionMode, - CameraPosition position) async { - _channel.setMethodCallHandler((call) async { - switch (call.method) { - case 'read': - // This might fail if the code type is not present in the list of available code types. - // Barcode init will throw in this case. - final barcode = Barcode(call.arguments); - _onDetectHandler?.call(barcode); - break; - default: - assert(true, - "FastBarcodeScanner: Unknown method call received: ${call.method}"); - } - }); - - final response = await _channel.invokeMethod('start', { - 'types': types.map((e) => describeEnum(e)).toList(growable: false), - 'mode': describeEnum(detectionMode), - 'res': describeEnum(resolution), - 'fps': describeEnum(framerate), - 'pos': describeEnum(position) - }); - - return PreviewConfiguration(response); - } - - @override - Future pause() => _channel.invokeMethod('pause'); - - @override - Future resume() => _channel.invokeMethod('resume'); - - @override - Future dispose() { - _channel.setMethodCallHandler(null); - _onDetectHandler = null; - return _channel.invokeMethod('stop'); - } - - @override - Future toggleTorch() => - _channel.invokeMethod('toggleTorch').then((isOn) => isOn); - - @override - Future changeCamera(CameraPosition position) => _channel - .invokeMethod('changeCamera', describeEnum(position)) - .then((success) => success); - - @override - Future canChangeCamera() => - _channel.invokeMethod('canChangeCamera').then((success) => success); - - @override - void setOnDetectHandler(void Function(Barcode) handler) => - _onDetectHandler = handler; -} diff --git a/fast_barcode_scanner_platform_interface/lib/src/types/barcode.dart b/fast_barcode_scanner_platform_interface/lib/src/types/barcode.dart deleted file mode 100644 index 7639f92c..00000000 --- a/fast_barcode_scanner_platform_interface/lib/src/types/barcode.dart +++ /dev/null @@ -1,24 +0,0 @@ -import 'package:flutter/foundation.dart'; - -import '../../fast_barcode_scanner_platform_interface.dart'; - -/// Describes a Barcode with type and value. -/// [Barcode] are equatable. -class Barcode { - /// Creates a [Barcode] from a Flutter Message Protocol - Barcode(List data) - : type = BarcodeType.values.firstWhere((e) => describeEnum(e) == data[0]), - value = data[1]; - - /// The type of the barcode. - final BarcodeType type; - - /// The actual value of the barcode. - final String value; - - @override - bool operator ==(Object other) => - other is Barcode && other.type == type && other.value == value; - @override - int get hashCode => super.hashCode ^ type.hashCode ^ value.hashCode; -} diff --git a/fast_barcode_scanner_platform_interface/lib/src/types/barcode_type.dart b/fast_barcode_scanner_platform_interface/lib/src/types/barcode_type.dart deleted file mode 100644 index bbf83d6c..00000000 --- a/fast_barcode_scanner_platform_interface/lib/src/types/barcode_type.dart +++ /dev/null @@ -1,50 +0,0 @@ -/// Contains all currently on iOS and Android supported barcode types. -enum BarcodeType { - /// Android - // all, - - /// Android, iOS - aztec, - - /// Android, iOS - code128, - - /// Android, iOS - code39, - - /// iOS - code39mod43, - - /// Android, iOS - code93, - - /// Android - codabar, - - /// Android, iOS - dataMatrix, - - /// Android, iOS - ean13, - - /// Android, iOS - ean8, - - /// Android, iOS - itf, - - /// Android, iOS - pdf417, - - /// Android, iOS - qr, - - /// Android - upcA, - - /// Android, iOS - upcE, - - /// iOS - interleaved, -} diff --git a/fast_barcode_scanner_platform_interface/lib/src/types/preview_configuration.dart b/fast_barcode_scanner_platform_interface/lib/src/types/preview_configuration.dart deleted file mode 100644 index e18f2ff1..00000000 --- a/fast_barcode_scanner_platform_interface/lib/src/types/preview_configuration.dart +++ /dev/null @@ -1,44 +0,0 @@ -/// Supported resolutions. Not all devices support all resolutions! -enum Resolution { sd480, hd720, hd1080, hd4k } - -/// Supported Framerates. Not all devices support all framerates! -enum Framerate { fps30, fps60, fps120, fps240 } - -enum DetectionMode { - /// Pauses the detection of further barcodes when a barcode is detected. - /// The camera feed continues. - pauseDetection, - - /// Pauses the camera feed on detection. - /// This will inevitably stop the detection of barcodes. - pauseVideo, - - /// Does nothing on detection. May need to throttle detections using continuous. - continuous -} - -/// The position of the camera. -enum CameraPosition { front, back } - -/// The configuration by which the camera feed can be laid out in the UI. -class PreviewConfiguration { - /// The width of the camera feed in points. - final int width; - - /// The height of the camera feed in points. - final int height; - - /// The orientation of the camera feed. - final num sensorOrientation; - - /// A id of a texture which contains the camera feed. - /// - /// Can be consumed by a [Texture] widget. - final int textureId; - - PreviewConfiguration(Map response) - : textureId = response["textureId"], - sensorOrientation = response["surfaceOrientation"], - height = response["surfaceHeight"], - width = response["surfaceWidth"]; -} diff --git a/fast_barcode_scanner_platform_interface/pubspec.yaml b/fast_barcode_scanner_platform_interface/pubspec.yaml deleted file mode 100644 index a96ba385..00000000 --- a/fast_barcode_scanner_platform_interface/pubspec.yaml +++ /dev/null @@ -1,15 +0,0 @@ -name: fast_barcode_scanner_platform_interface -description: A common platform interface for the fast_barcode_scanner plugin. -homepage: https://github.com/jhoogstraat/fast_barcode_scanner/tree/master/fast_barcode_scanner_platform_interface -repository: https://github.com/jhoogstraat/fast_barcode_scanner -version: 1.0.4 - -environment: - sdk: '>=3.0.0 <4.0.0' - -dependencies: - flutter: - sdk: flutter - plugin_platform_interface: ^2.0.1 -dev_dependencies: - flutter_lints: ^1.0.4 diff --git a/fast_barcode_scanner_platform_interface/test/method_channel_fast_barcode_scanner_test.dart b/fast_barcode_scanner_platform_interface/test/method_channel_fast_barcode_scanner_test.dart deleted file mode 100644 index 8b137891..00000000 --- a/fast_barcode_scanner_platform_interface/test/method_channel_fast_barcode_scanner_test.dart +++ /dev/null @@ -1 +0,0 @@ - diff --git a/fast_barcode_scanner/ios/.gitignore b/ios/.gitignore similarity index 100% rename from fast_barcode_scanner/ios/.gitignore rename to ios/.gitignore diff --git a/fast_barcode_scanner/ios/Assets/.gitkeep b/ios/Assets/.gitkeep similarity index 100% rename from fast_barcode_scanner/ios/Assets/.gitkeep rename to ios/Assets/.gitkeep diff --git a/fast_barcode_scanner/ios/Classes/BarcodeReader.swift b/ios/Classes/BarcodeReader.swift similarity index 100% rename from fast_barcode_scanner/ios/Classes/BarcodeReader.swift rename to ios/Classes/BarcodeReader.swift diff --git a/fast_barcode_scanner/ios/Classes/FastBarcodeScannerPlugin.swift b/ios/Classes/FastBarcodeScannerPlugin.swift similarity index 100% rename from fast_barcode_scanner/ios/Classes/FastBarcodeScannerPlugin.swift rename to ios/Classes/FastBarcodeScannerPlugin.swift diff --git a/fast_barcode_scanner/ios/fast_barcode_scanner.podspec b/ios/fast_barcode_scanner.podspec similarity index 100% rename from fast_barcode_scanner/ios/fast_barcode_scanner.podspec rename to ios/fast_barcode_scanner.podspec diff --git a/fast_barcode_scanner/lib/fast_barcode_scanner.dart b/lib/fast_barcode_scanner.dart similarity index 100% rename from fast_barcode_scanner/lib/fast_barcode_scanner.dart rename to lib/fast_barcode_scanner.dart diff --git a/fast_barcode_scanner/lib/src/barcode_camera.dart b/lib/src/barcode_camera.dart similarity index 100% rename from fast_barcode_scanner/lib/src/barcode_camera.dart rename to lib/src/barcode_camera.dart diff --git a/fast_barcode_scanner/lib/src/camera_controller.dart b/lib/src/camera_controller.dart similarity index 100% rename from fast_barcode_scanner/lib/src/camera_controller.dart rename to lib/src/camera_controller.dart diff --git a/fast_barcode_scanner/lib/src/overlays/blur_overlay.dart b/lib/src/overlays/blur_overlay.dart similarity index 100% rename from fast_barcode_scanner/lib/src/overlays/blur_overlay.dart rename to lib/src/overlays/blur_overlay.dart diff --git a/fast_barcode_scanner/lib/src/overlays/material_overlay.dart b/lib/src/overlays/material_overlay.dart similarity index 100% rename from fast_barcode_scanner/lib/src/overlays/material_overlay.dart rename to lib/src/overlays/material_overlay.dart diff --git a/fast_barcode_scanner/lib/src/overlays/material_scanner_painter/material_barcode_frame_painter.dart b/lib/src/overlays/material_scanner_painter/material_barcode_frame_painter.dart similarity index 100% rename from fast_barcode_scanner/lib/src/overlays/material_scanner_painter/material_barcode_frame_painter.dart rename to lib/src/overlays/material_scanner_painter/material_barcode_frame_painter.dart diff --git a/fast_barcode_scanner/lib/src/overlays/material_scanner_painter/material_sensing_painter.dart b/lib/src/overlays/material_scanner_painter/material_sensing_painter.dart similarity index 100% rename from fast_barcode_scanner/lib/src/overlays/material_scanner_painter/material_sensing_painter.dart rename to lib/src/overlays/material_scanner_painter/material_sensing_painter.dart diff --git a/fast_barcode_scanner/pubspec.yaml b/pubspec.yaml similarity index 79% rename from fast_barcode_scanner/pubspec.yaml rename to pubspec.yaml index b782a44e..498bf057 100644 --- a/fast_barcode_scanner/pubspec.yaml +++ b/pubspec.yaml @@ -10,7 +10,9 @@ environment: dependencies: flutter: sdk: flutter - fast_barcode_scanner_platform_interface: ^1.0.4 + fast_barcode_scanner_platform_interface: + git: + url: git@github.com:bschmalb/fast_barcode_scanner_platform_interface.git dev_dependencies: flutter_test: diff --git a/fast_barcode_scanner/test/fast_barcode_scanner_test.dart b/test/fast_barcode_scanner_test.dart similarity index 100% rename from fast_barcode_scanner/test/fast_barcode_scanner_test.dart rename to test/fast_barcode_scanner_test.dart