r_scan.dart 3.5 KB

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