analyzeImage static method
Future<List<UCode> >
analyzeImage({
- String? path,
- Uint8List? bytes,
- UCodeScanOptions options = const UCodeScanOptions(multiple: true),
- UScanEngine engine = UScanEngine.auto,
Decodes an image file or byte buffer without opening a camera.
Prefers a free platform decoder (Apple Vision, the browser BarcodeDetector) and otherwise decodes the image with Flutter's own codecs and runs the bundled Dart engine, so this works on every platform with no native help.
Implementation
static Future<List<UCode>> analyzeImage({
String? path,
Uint8List? bytes,
UCodeScanOptions options = const UCodeScanOptions(multiple: true),
UScanEngine engine = UScanEngine.auto,
}) async {
Uint8List? data = bytes;
if (data == null && path != null && !kIsWeb) {
final File file = File(path);
if (file.existsSync()) data = await file.readAsBytes();
}
if (data == null && path == null) return const <UCode>[];
if (engine != UScanEngine.dart) {
try {
final List<Object?>? raw = await UCameraChannel.invokeList("analyzeImage", <String, Object?>{"path": path, "bytes": data});
if (raw != null) {
final List<UCode> codes = raw.whereType<Map<Object?, Object?>>().map(UCode.fromMap).toList(growable: false);
if (codes.isNotEmpty || engine == UScanEngine.platform) return codes;
}
} catch (_) {
if (engine == UScanEngine.platform) return const <UCode>[];
}
}
if (data == null) return const <UCode>[];
final UGrayImage? gray = await decodeToGray(data);
if (gray == null) return const <UCode>[];
return UCodeScanWorker.decode(gray.data, gray.width, gray.height, gray.stride, options);
}