Navaron Bracke
Committed by GitHub

Merge pull request #820 from navaronbracke/remove_barcode_utility

feat: Remove unused barcode utility helpers
## NEXT
Breaking changes:
* The internal `fromNative()` methods now accept a `Map<Object?, Object?>` instead of `Map<dynamic, dynamic>`.
Improvements:
* The `type` of an `Address` is now non-null.
* The `type` of an `Email` is now non-null.
... ... @@ -10,7 +13,6 @@ Improvements:
* The `width` and `height` of `BarcodeCapture` are now non-null.
* The `BarcodeCapture` class now exposes a `size`.
* The list of `corners` of a `Barcode` is now non-null.
* The internal `fromNative()` methods now accept a `Map<Object?, Object?>` instead of `Map<dynamic, dynamic>`.
Bugs fixed:
* Fixed the default values for the `format` and `type` arguments of the Barcode constructor.
... ...
... ... @@ -5,7 +5,6 @@ import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:mobile_scanner/mobile_scanner_web.dart';
import 'package:mobile_scanner/src/barcode_utility.dart';
import 'package:mobile_scanner/src/enums/barcode_format.dart';
import 'package:mobile_scanner/src/enums/camera_facing.dart';
... ... @@ -110,7 +109,7 @@ class MobileScannerWebPlugin {
if (arguments.containsKey('formats')) {
formats = (arguments['formats'] as List)
.cast<int>()
.map((e) => toFormat(e))
.map(BarcodeFormat.fromRawValue)
.toList();
}
... ...
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
Size toSize(Map data) {
final width = data['width'] as double;
final height = data['height'] as double;
return Size(width, height);
}
List<Offset>? toCorners(List<Map<Object?, Object?>>? data) {
if (data == null) {
return null;
}
return List.unmodifiable(
data.map((Map<Object?, Object?> e) {
return Offset(e['x']! as double, e['y']! as double);
}),
);
}
BarcodeFormat toFormat(int value) {
switch (value) {
case 0:
return BarcodeFormat.all;
case 1:
return BarcodeFormat.code128;
case 2:
return BarcodeFormat.code39;
case 4:
return BarcodeFormat.code93;
case 8:
return BarcodeFormat.codebar;
case 16:
return BarcodeFormat.dataMatrix;
case 32:
return BarcodeFormat.ean13;
case 64:
return BarcodeFormat.ean8;
case 128:
return BarcodeFormat.itf;
case 256:
return BarcodeFormat.qrCode;
case 512:
return BarcodeFormat.upcA;
case 1024:
return BarcodeFormat.upcE;
case 2048:
return BarcodeFormat.pdf417;
case 4096:
return BarcodeFormat.aztec;
default:
return BarcodeFormat.unknown;
}
}
CalendarEvent? toCalendarEvent(Map? data) {
if (data != null) {
return CalendarEvent.fromNative(data);
} else {
return null;
}
}
DateTime? toDateTime(Map<String, dynamic>? data) {
if (data != null) {
final year = data['year'] as int;
final month = data['month'] as int;
final day = data['day'] as int;
final hour = data['hours'] as int;
final minute = data['minutes'] as int;
final second = data['seconds'] as int;
return data['isUtc'] as bool
? DateTime.utc(year, month, day, hour, minute, second)
: DateTime(year, month, day, hour, minute, second);
} else {
return null;
}
}
ContactInfo? toContactInfo(Map? data) {
if (data != null) {
return ContactInfo.fromNative(data);
} else {
return null;
}
}
PersonName? toName(Map? data) {
if (data != null) {
return PersonName.fromNative(data);
} else {
return null;
}
}
DriverLicense? toDriverLicense(Map? data) {
if (data != null) {
return DriverLicense.fromNative(data);
} else {
return null;
}
}
Email? toEmail(Map? data) {
if (data != null) {
return Email.fromNative(data);
} else {
return null;
}
}
GeoPoint? toGeoPoint(Map? data) {
if (data != null) {
return GeoPoint.fromNative(data);
} else {
return null;
}
}
Phone? toPhone(Map? data) {
if (data != null) {
return Phone.fromNative(data);
} else {
return null;
}
}
SMS? toSMS(Map? data) {
if (data != null) {
return SMS.fromNative(data);
} else {
return null;
}
}
UrlBookmark? toUrl(Map? data) {
if (data != null) {
return UrlBookmark.fromNative(data);
} else {
return null;
}
}
WiFi? toWiFi(Map? data) {
if (data != null) {
return WiFi.fromNative(data);
} else {
return null;
}
}
Size applyBoxFit(BoxFit fit, Size input, Size output) {
if (input.height <= 0.0 ||
input.width <= 0.0 ||
output.height <= 0.0 ||
output.width <= 0.0) {
return Size.zero;
}
Size destination;
final inputAspectRatio = input.width / input.height;
final outputAspectRatio = output.width / output.height;
switch (fit) {
case BoxFit.fill:
destination = output;
break;
case BoxFit.contain:
if (outputAspectRatio > inputAspectRatio) {
destination = Size(
input.width * output.height / input.height,
output.height,
);
} else {
destination = Size(
output.width,
input.height * output.width / input.width,
);
}
break;
case BoxFit.cover:
if (outputAspectRatio > inputAspectRatio) {
destination = Size(
output.width,
input.height * (output.width / input.width),
);
} else {
destination = Size(
input.width * (output.height / input.height),
output.height,
);
}
break;
case BoxFit.fitWidth:
destination = Size(
output.width,
input.height * (output.width / input.width),
);
break;
case BoxFit.fitHeight:
destination = Size(
input.width * (output.height / input.height),
output.height,
);
break;
case BoxFit.none:
destination = Size(
math.min(input.width, output.width),
math.min(input.height, output.height),
);
break;
case BoxFit.scaleDown:
destination = input;
if (destination.height > output.height) {
destination = Size(output.height * inputAspectRatio, output.height);
}
if (destination.width > output.width) {
destination = Size(output.width, output.width / inputAspectRatio);
}
break;
}
return destination;
}
... ... @@ -6,7 +6,6 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner/src/barcode_utility.dart';
/// The [MobileScannerController] holds all the logic of this plugin,
/// where as the [MobileScanner] class is the frontend of this plugin.
... ... @@ -287,14 +286,26 @@ class MobileScannerController {
torchState.value = TorchState.on;
}
isStarting = false;
return startArguments.value = MobileScannerArguments(
size: kIsWeb
? Size(
final Size size;
if (kIsWeb) {
size = Size(
startResult['videoWidth'] as double? ?? 0,
startResult['videoHeight'] as double? ?? 0,
)
: toSize(startResult['size'] as Map? ?? {}),
);
} 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(
size: size,
hasTorch: hasTorch,
textureId: kIsWeb ? null : startResult['textureId'] as int?,
webId: kIsWeb ? startResult['ViewID'] as String? : null,
... ... @@ -424,7 +435,9 @@ class MobileScannerController {
barcodes: [
Barcode(
rawValue: (data as Map)['payload'] as String?,
format: toFormat(data['symbology'] as int),
format: BarcodeFormat.fromRawValue(
data['symbology'] as int? ?? -1,
),
),
],
),
... ... @@ -432,6 +445,8 @@ class MobileScannerController {
break;
case 'barcodeWeb':
final barcode = data as Map?;
final corners = barcode?['corners'] as List<Object?>? ?? <Object?>[];
_barcodesController.add(
BarcodeCapture(
raw: data,
... ... @@ -440,12 +455,16 @@ class MobileScannerController {
Barcode(
rawValue: barcode['rawValue'] as String?,
rawBytes: barcode['rawBytes'] as Uint8List?,
format: toFormat(barcode['format'] as int),
corners: toCorners(
(barcode['corners'] as List<Object?>? ?? [])
.cast<Map<Object?, Object?>>(),
) ??
const <Offset>[],
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);
},
),
),
),
],
),
... ...