mobile_scanner_controller.dart 15.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
import 'dart:async';
import 'dart:io';
// ignore: unnecessary_import
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:mobile_scanner/mobile_scanner.dart';

/// The [MobileScannerController] holds all the logic of this plugin,
/// where as the [MobileScanner] class is the frontend of this plugin.
class MobileScannerController {
  MobileScannerController({
    this.facing = CameraFacing.back,
    this.detectionSpeed = DetectionSpeed.normal,
    this.detectionTimeoutMs = 250,
    this.torchEnabled = false,
    this.formats,
    this.returnImage = false,
    @Deprecated(
      'Instead, use the result of calling `start()` to determine if permissions were granted.',
    )
    this.onPermissionSet,
    this.autoStart = true,
    this.cameraResolution,
    this.useNewCameraSelector = false,
  });

  /// Select which camera should be used.
  ///
  /// Default: CameraFacing.back
  final CameraFacing facing;

  /// Enable or disable the torch (Flash) on start
  ///
  /// Default: disabled
  final bool torchEnabled;

  /// Set to true if you want to return the image buffer with the Barcode event
  ///
  /// Only supported on iOS and Android
  final bool returnImage;

  /// If provided, the scanner will only detect those specific formats
  final List<BarcodeFormat>? formats;

  /// Sets the speed of detections.
  ///
  /// WARNING: DetectionSpeed.unrestricted can cause memory issues on some devices
  final DetectionSpeed detectionSpeed;

  /// Sets the timeout, in milliseconds, of the scanner.
  ///
  /// This timeout is ignored if the [detectionSpeed]
  /// is not set to [DetectionSpeed.normal].
  ///
  /// By default this is set to `250` milliseconds,
  /// which prevents memory issues on older devices.
  final int detectionTimeoutMs;

  /// Automatically start the mobileScanner on initialization.
  final bool autoStart;

  /// The desired resolution for the camera.
  ///
  /// When this value is provided, the camera will try to match this resolution,
  /// or fallback to the closest available resolution.
  /// When this is null, Android defaults to a resolution of 640x480.
  ///
  /// Bear in mind that changing the resolution has an effect on the aspect ratio.
  ///
  /// When the camera orientation changes,
  /// the resolution will be flipped to match the new dimensions of the display.
  ///
  /// Currently only supported on Android.
  final Size? cameraResolution;

  /// Use the new resolution selector. Warning: not fully tested, may produce
  /// unwanted/zoomed images.
  ///
  /// Only supported on Android
  final bool useNewCameraSelector;

  /// Sets the barcode stream
  final StreamController<BarcodeCapture> _barcodesController =
      StreamController.broadcast();

  Stream<BarcodeCapture> get barcodes => _barcodesController.stream;

  static const MethodChannel _methodChannel =
      MethodChannel('dev.steenbakker.mobile_scanner/scanner/method');
  static const EventChannel _eventChannel =
      EventChannel('dev.steenbakker.mobile_scanner/scanner/event');

  @Deprecated(
    'Instead, use the result of calling `start()` to determine if permissions were granted.',
  )
  Function(bool permissionGranted)? onPermissionSet;

  /// Listen to events from the platform specific code
  StreamSubscription? events;

  /// A notifier that provides several arguments about the MobileScanner
  final ValueNotifier<MobileScannerArguments?> startArguments =
      ValueNotifier(null);

  /// A notifier that provides the state of the Torch (Flash)
  final ValueNotifier<TorchState> torchState = ValueNotifier(TorchState.off);

  /// A notifier that provides the state of which camera is being used
  late final ValueNotifier<CameraFacing> cameraFacingState =
      ValueNotifier(facing);

  /// A notifier that provides zoomScale.
  final ValueNotifier<double> zoomScaleState = ValueNotifier(0.0);

  bool isStarting = false;

  /// A notifier that provides availability of the Torch (Flash)
  final ValueNotifier<bool?> hasTorchState = ValueNotifier(false);

  /// Returns whether the device has a torch.
  ///
  /// Throws an error if the controller is not initialized.
  bool get hasTorch {
    final hasTorch = hasTorchState.value;
    if (hasTorch == null) {
      throw const MobileScannerException(
        errorCode: MobileScannerErrorCode.controllerUninitialized,
      );
    }

    return hasTorch;
  }

  /// Set the starting arguments for the camera
  Map<String, dynamic> _argumentsToMap({CameraFacing? cameraFacingOverride}) {
    final Map<String, dynamic> arguments = {};

    cameraFacingState.value = cameraFacingOverride ?? facing;
    arguments['facing'] = cameraFacingState.value.rawValue;
    arguments['torch'] = torchEnabled;
    arguments['speed'] = detectionSpeed.rawValue;
    arguments['timeout'] = detectionTimeoutMs;
    arguments['returnImage'] = returnImage;
    arguments['useNewCameraSelector'] = useNewCameraSelector;

    /*    if (scanWindow != null) {
      arguments['scanWindow'] = [
        scanWindow!.left,
        scanWindow!.top,
        scanWindow!.right,
        scanWindow!.bottom,
      ];
    } */

    if (formats != null) {
      if (kIsWeb || Platform.isIOS || Platform.isMacOS || Platform.isAndroid) {
        arguments['formats'] = formats!.map((e) => e.rawValue).toList();
      }
    }

    if (cameraResolution != null) {
      arguments['cameraResolution'] = <int>[
        cameraResolution!.width.toInt(),
        cameraResolution!.height.toInt(),
      ];
    }

    return arguments;
  }

  /// Start scanning for barcodes.
  /// Upon calling this method, the necessary camera permission will be requested.
  ///
  /// Returns an instance of [MobileScannerArguments]
  /// when the scanner was successfully started.
  /// Returns null if the scanner is currently starting.
  ///
  /// Throws a [MobileScannerException] if starting the scanner failed.
  Future<MobileScannerArguments?> start({
    CameraFacing? cameraFacingOverride,
  }) async {
    if (isStarting) {
      debugPrint("Called start() while starting.");
      return null;
    }

    events ??= _eventChannel
        .receiveBroadcastStream()
        .listen((data) => _handleEvent(data as Map));

    isStarting = true;

    // Check authorization status
    if (!kIsWeb) {
      final MobileScannerState state;

      try {
        state = MobileScannerState.fromRawValue(
          await _methodChannel.invokeMethod('state') as int? ?? 0,
        );
      } on PlatformException catch (error) {
        isStarting = false;

        throw MobileScannerException(
          errorCode: MobileScannerErrorCode.genericError,
          errorDetails: MobileScannerErrorDetails(
            code: error.code,
            details: error.details as Object?,
            message: error.message,
          ),
        );
      }

      switch (state) {
        // Android does not have an undetermined permission state.
        // So if the permission state is denied, just request it now.
        case MobileScannerState.undetermined:
        case MobileScannerState.denied:
          try {
            final bool granted =
                await _methodChannel.invokeMethod('request') as bool? ?? false;

            if (!granted) {
              isStarting = false;
              throw const MobileScannerException(
                errorCode: MobileScannerErrorCode.permissionDenied,
              );
            }
          } on PlatformException catch (error) {
            isStarting = false;
            throw MobileScannerException(
              errorCode: MobileScannerErrorCode.genericError,
              errorDetails: MobileScannerErrorDetails(
                code: error.code,
                details: error.details as Object?,
                message: error.message,
              ),
            );
          }

          break;
        case MobileScannerState.authorized:
          break;
      }
    }

    // Start the camera with arguments
    Map<String, dynamic>? startResult = {};
    try {
      startResult = await _methodChannel.invokeMapMethod<String, dynamic>(
        'start',
        _argumentsToMap(cameraFacingOverride: cameraFacingOverride),
      );
    } on PlatformException catch (error) {
      MobileScannerErrorCode errorCode = MobileScannerErrorCode.genericError;

      final String? errorMessage = error.message;

      if (kIsWeb) {
        if (errorMessage == null) {
          errorCode = MobileScannerErrorCode.genericError;
        } else if (errorMessage.contains('NotFoundError') ||
            errorMessage.contains('NotSupportedError')) {
          errorCode = MobileScannerErrorCode.unsupported;
        } else if (errorMessage.contains('NotAllowedError')) {
          errorCode = MobileScannerErrorCode.permissionDenied;
        } else {
          errorCode = MobileScannerErrorCode.genericError;
        }
      }

      isStarting = false;

      throw MobileScannerException(
        errorCode: errorCode,
        errorDetails: MobileScannerErrorDetails(
          code: error.code,
          details: error.details as Object?,
          message: error.message,
        ),
      );
    }

    if (startResult == null) {
      isStarting = false;
      throw const MobileScannerException(
        errorCode: MobileScannerErrorCode.genericError,
      );
    }

    final hasTorch = startResult['torchable'] as bool? ?? false;
    hasTorchState.value = hasTorch;

    final Size size;

    if (kIsWeb) {
      size = Size(
        startResult['videoWidth'] as double? ?? 0,
        startResult['videoHeight'] as double? ?? 0,
      );
    } else {
      final Map<Object?, Object?>? sizeInfo =
          startResult['size'] as Map<Object?, Object?>?;

      size = Size(
        sizeInfo?['width'] as double? ?? 0,
        sizeInfo?['height'] as double? ?? 0,
      );
    }

    isStarting = false;
    return startArguments.value = MobileScannerArguments(
      nrOfCameras: startResult['nrOfCameras'] as int?,
      size: size,
      hasTorch: hasTorch,
      textureId: kIsWeb ? null : startResult['textureId'] as int?,
      webId: kIsWeb ? startResult['ViewID'] as String? : null,
    );
  }

  /// Stops the camera, but does not dispose this controller.
  Future<void> stop() async {
    await _methodChannel.invokeMethod('stop');

    // After the camera stopped, set the torch state to off,
    // as the torch state callback is never called when the camera is stopped.
    torchState.value = TorchState.off;
  }

  /// Switches the torch on or off.
  ///
  /// Does nothing if the device has no torch.
  ///
  /// Throws if the controller was not initialized.
  Future<void> toggleTorch() async {
    final hasTorch = hasTorchState.value;

    if (hasTorch == null) {
      throw const MobileScannerException(
        errorCode: MobileScannerErrorCode.controllerUninitialized,
      );
    }

    if (!hasTorch) {
      return;
    }

    final TorchState newState =
        torchState.value == TorchState.off ? TorchState.on : TorchState.off;

    await _methodChannel.invokeMethod('torch', newState.rawValue);
  }

  /// Changes the state of the camera (front or back).
  ///
  /// Does nothing if the device has no front camera.
  Future<void> switchCamera() async {
    await _methodChannel.invokeMethod('stop');
    final CameraFacing facingToUse =
        cameraFacingState.value == CameraFacing.back
            ? CameraFacing.front
            : CameraFacing.back;
    await start(cameraFacingOverride: facingToUse);
  }

  /// Handles a local image file.
  /// Returns true if a barcode or QR code is found.
  /// Returns false if nothing is found.
  ///
  /// [path] The path of the image on the devices
  Future<bool> analyzeImage(String path) async {
    events ??= _eventChannel
        .receiveBroadcastStream()
        .listen((data) => _handleEvent(data as Map));

    return _methodChannel
        .invokeMethod<bool>('analyzeImage', path)
        .then<bool>((bool? value) => value ?? false);
  }

  /// Set the zoomScale of the camera.
  ///
  /// [zoomScale] must be within 0.0 and 1.0, where 1.0 is the max zoom, and 0.0
  /// is zoomed out.
  Future<void> setZoomScale(double zoomScale) async {
    if (zoomScale < 0 || zoomScale > 1) {
      throw const MobileScannerException(
        errorCode: MobileScannerErrorCode.genericError,
        errorDetails: MobileScannerErrorDetails(
          message: 'The zoomScale must be between 0 and 1.',
        ),
      );
    }
    await _methodChannel.invokeMethod('setScale', zoomScale);
  }

  /// Reset the zoomScale of the camera to use standard scale 1x.
  Future<void> resetZoomScale() async {
    await _methodChannel.invokeMethod('resetScale');
  }

  /// Disposes the MobileScannerController and closes all listeners.
  ///
  /// If you call this, you cannot use this controller object anymore.
  void dispose() {
    stop();
    events?.cancel();
    _barcodesController.close();
  }

  /// Handles a returning event from the platform side
  void _handleEvent(Map event) {
    final name = event['name'];
    final data = event['data'];

    switch (name) {
      case 'torchState':
        final state = TorchState.values[data as int? ?? 0];
        torchState.value = state;
        break;
      case 'zoomScaleState':
        zoomScaleState.value = data as double? ?? 0.0;
        break;
      case 'barcode':
        if (data == null) return;
        final parsed = (data as List)
            .map((value) => Barcode.fromNative(value as Map))
            .toList();
        _barcodesController.add(
          BarcodeCapture(
            raw: data,
            barcodes: parsed,
            image: event['image'] as Uint8List?,
            width: event['width'] as double?,
            height: event['height'] as double?,
          ),
        );
        break;
      case 'barcodeMac':
        _barcodesController.add(
          BarcodeCapture(
            raw: data,
            barcodes: [
              Barcode(
                rawValue: (data as Map)['payload'] as String?,
                format: BarcodeFormat.fromRawValue(
                  data['symbology'] as int? ?? -1,
                ),
              ),
            ],
          ),
        );
        break;
      case 'barcodeWeb':
        final barcode = data as Map?;
        final corners = barcode?['corners'] as List<Object?>? ?? <Object?>[];

        _barcodesController.add(
          BarcodeCapture(
            raw: data,
            barcodes: [
              if (barcode != null)
                Barcode(
                  rawValue: barcode['rawValue'] as String?,
                  rawBytes: barcode['rawBytes'] as Uint8List?,
                  format: BarcodeFormat.fromRawValue(
                    barcode['format'] as int? ?? -1,
                  ),
                  corners: List.unmodifiable(
                    corners.cast<Map<Object?, Object?>>().map(
                      (Map<Object?, Object?> e) {
                        return Offset(e['x']! as double, e['y']! as double);
                      },
                    ),
                  ),
                ),
            ],
          ),
        );
        break;
      case 'error':
        throw MobileScannerException(
          errorCode: MobileScannerErrorCode.genericError,
          errorDetails: MobileScannerErrorDetails(message: data as String?),
        );
      default:
        throw UnimplementedError(name as String?);
    }
  }

  /// updates the native ScanWindow
  Future<void> updateScanWindow(Rect? window) async {
    List? data;
    if (window != null) {
      data = [window.left, window.top, window.right, window.bottom];
    }

    await _methodChannel.invokeMethod('updateScanWindow', {'rect': data});
  }
}