r_scan.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2019 The rhyme_lph Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. import 'dart:async';
  5. import 'dart:typed_data';
  6. import 'package:flutter/services.dart';
  7. export 'package:r_scan/src/r_scan_view.dart';
  8. export 'package:r_scan/src/r_scan_camera.dart';
  9. /// qr scan
  10. class RScan {
  11. static const MethodChannel _channel = const MethodChannel('com.rhyme_lph/r_scan');
  12. /// scan qr image in path
  13. ///
  14. /// [path] your qr image path
  15. ///
  16. /// It will return your scan result.if not found qr,will return empty.
  17. static Future<RScanResult?> scanImagePath(String path) async =>
  18. RScanResult.formMap(await _channel.invokeMethod('scanImagePath', {
  19. "path": path,
  20. }));
  21. /// scan qr image in url
  22. ///
  23. /// [url] your qr image url
  24. ///
  25. /// It will return your scan result.if not found qr,will return empty.
  26. static Future<RScanResult?> scanImageUrl(String url) async =>
  27. RScanResult.formMap(await _channel.invokeMethod('scanImageUrl', {
  28. "url": url,
  29. }));
  30. /// scan qr image in memory
  31. ///
  32. /// [uint8list] your qr image memory
  33. ///
  34. /// It will return your scan result.if not found qr,will return empty.
  35. static Future<RScanResult?> scanImageMemory(Uint8List uint8list) async =>
  36. RScanResult.formMap(await _channel.invokeMethod('scanImageMemory', {
  37. "uint8list": uint8list,
  38. }));
  39. }
  40. /// barcode type
  41. enum RScanBarType {
  42. azetc,
  43. codabar, // ios not found
  44. code_39,
  45. code_93,
  46. code_128,
  47. data_matrix,
  48. ean_8,
  49. ean_13, // ios include upc_a
  50. itf,
  51. maxicode, // ios not found
  52. pdf_417,
  53. qr_code,
  54. rss_14, // ios not found
  55. rss_expanded, // ios not found
  56. upc_a, // ios not found
  57. upc_e,
  58. upc_ean_extension, // ios not found
  59. }
  60. /// barcode point
  61. class RScanPoint {
  62. /// barcode point x
  63. final double x;
  64. /// barcode point y
  65. final double y;
  66. RScanPoint(this.x, this.y);
  67. @override
  68. String toString() {
  69. return 'RScanPoint{x: $x, y: $y}';
  70. }
  71. @override
  72. bool operator ==(Object other) =>
  73. identical(this, other) || other is RScanPoint && runtimeType == other.runtimeType && x == other.x && y == other.y;
  74. @override
  75. int get hashCode => x.hashCode ^ y.hashCode;
  76. }
  77. /// scan result
  78. class RScanResult {
  79. /// barcode type
  80. final RScanBarType? type;
  81. ///barcode message
  82. final String? message;
  83. ///barcode points
  84. final List<RScanPoint>? points;
  85. const RScanResult({
  86. this.type,
  87. this.message,
  88. this.points,
  89. });
  90. static RScanResult? formMap(Map? map) {
  91. return map == null
  92. ? null
  93. : RScanResult(
  94. type: map['type'] != null ? RScanBarType.values[map['type'] as int] : null,
  95. message: map['message'] as String,
  96. points: map['points'] != null
  97. ? (map['points'] as List)
  98. .map(
  99. (data) => RScanPoint(
  100. data['X'],
  101. data['Y'],
  102. ),
  103. )
  104. .toList()
  105. : null,
  106. );
  107. }
  108. @override
  109. String toString() {
  110. return 'RScanResult{type: $type, message: $message, points: $points}';
  111. }
  112. @override
  113. bool operator ==(Object other) =>
  114. identical(this, other) ||
  115. other is RScanResult &&
  116. runtimeType == other.runtimeType &&
  117. type == other.type &&
  118. message == other.message &&
  119. points == other.points;
  120. @override
  121. int get hashCode => type.hashCode ^ message.hashCode ^ points.hashCode;
  122. }