Offline gym log for Android. No account, no ads, no internet.
Dart androidbodybuildingdartexercisesf-droidfitnessfluttergymgym-apphealthprivacyself-hostedworkoutworkout-appworkout-tracker
Read QR codes from photos for gym passes
InlitX8ba955a135ea
diff --git a/lib/models/gym_pass.dart b/lib/models/gym_pass.dartnew file mode 100644index 0000000..de56983--- /dev/null+++ b/lib/models/gym_pass.dart@@ -0,0 +1,51 @@+enum PassFormat { qr, code128, ean13, ean8, upca, code39, itf, dataMatrix, pdf417, aztec }++PassFormat passFormatFrom(Object? raw) =>+ PassFormat.values.firstWhere((f) => f.name == raw, orElse: () => PassFormat.qr);++class GymPass {+ const GymPass({+ required this.id,+ required this.name,+ required this.code,+ this.format = PassFormat.qr,+ this.hue = 0,+ this.member = '',+ });++ final String id;+ final String name;+ final String code;+ final PassFormat format;+ final int hue;+ final String member;++ bool get square => const {PassFormat.qr, PassFormat.dataMatrix, PassFormat.aztec}.contains(format);++ GymPass copyWith({String? name, String? code, PassFormat? format, int? hue, String? member}) => GymPass(+ id: id,+ name: name ?? this.name,+ code: code ?? this.code,+ format: format ?? this.format,+ hue: hue ?? this.hue,+ member: member ?? this.member,+ );++ Map<String, dynamic> toJson() => {+ 'id': id,+ 'n': name,+ 'c': code,+ if (format != PassFormat.qr) 'f': format.name,+ if (hue != 0) 'h': hue,+ if (member.isNotEmpty) 'm': member,+ };++ factory GymPass.fromJson(Map<String, dynamic> j) => GymPass(+ id: (j['id'] ?? '') as String,+ name: (j['n'] ?? '') as String,+ code: (j['c'] ?? '') as String,+ format: passFormatFrom(j['f']),+ hue: (j['h'] as num?)?.toInt() ?? 0,+ member: (j['m'] ?? '') as String,+ );+}diff --git a/lib/services/pass_scan.dart b/lib/services/pass_scan.dartnew file mode 100644index 0000000..93cdcbc--- /dev/null+++ b/lib/services/pass_scan.dart@@ -0,0 +1,64 @@+import 'dart:io';+import 'dart:isolate';+import 'dart:typed_data';+import 'dart:ui' as ui;++import 'package:zxing2/qrcode.dart';++Future<String?> readQrFromFile(String path) async {+ ui.ImmutableBuffer? buffer;+ ui.ImageDescriptor? descriptor;+ try {+ final bytes = await File(path).readAsBytes();+ buffer = await ui.ImmutableBuffer.fromUint8List(bytes);+ descriptor = await ui.ImageDescriptor.encoded(buffer);+ final long = descriptor.width > descriptor.height ? descriptor.width : descriptor.height;+ final sides = <int>{for (final s in const [1200, 1800, 2600, 800]) s < long ? s : long};+ for (final side in sides) {+ final scale = side / long;+ final codec = await descriptor.instantiateCodec(+ targetWidth: (descriptor.width * scale).round(),+ targetHeight: (descriptor.height * scale).round(),+ );+ final frame = await codec.getNextFrame();+ final image = frame.image;+ final data = await image.toByteData(format: ui.ImageByteFormat.rawRgba);+ final w = image.width, h = image.height;+ image.dispose();+ codec.dispose();+ if (data == null) continue;+ final found = await Isolate.run(() => decodeQr(w, h, data.buffer.asUint8List()));+ if (found != null) return found;+ }+ } catch (_) {+ } finally {+ descriptor?.dispose();+ buffer?.dispose();+ }+ return null;+}++String? decodeQr(int width, int height, Uint8List rgba) {+ final pixels = Int32List(width * height);+ for (var i = 0, p = 0; i < pixels.length; i++, p += 4) {+ pixels[i] = 0xff000000 | (rgba[p] << 16) | (rgba[p + 1] << 8) | rgba[p + 2];+ }+ final whole = RGBLuminanceSource(width, height, pixels);+ final views = <LuminanceSource>[whole];+ for (final f in const [0.6, 0.4]) {+ final cw = (width * f).round(), ch = (height * f).round();+ views.add(whole.crop((width - cw) ~/ 2, (height - ch) ~/ 2, cw, ch));+ }+ final hints = DecodeHints()..put(DecodeHintType.tryHarder);+ for (final view in views) {+ for (final lum in [view, InvertedLuminanceSource(view)]) {+ for (final bitmap in [BinaryBitmap(HybridBinarizer(lum)), BinaryBitmap(GlobalHistogramBinarizer(lum))]) {+ try {+ final text = QRCodeReader().decode(bitmap, hints: hints).text;+ if (text.isNotEmpty) return text;+ } catch (_) {}+ }+ }+ }+ return null;+}