main.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:image_picker/image_picker.dart';
  5. import 'package:permission_handler/permission_handler.dart';
  6. import 'package:r_scan_example/scan_dialog.dart';
  7. import 'scan_camera_dialog.dart';
  8. import 'package:r_scan/r_scan.dart';
  9. void main() async {
  10. WidgetsFlutterBinding.ensureInitialized();
  11. runApp(MyApp());
  12. }
  13. class MyApp extends StatefulWidget {
  14. @override
  15. _MyAppState createState() => _MyAppState();
  16. }
  17. class _MyAppState extends State<MyApp> {
  18. @override
  19. Widget build(BuildContext context) {
  20. return MaterialApp(
  21. home: MyPage(),
  22. );
  23. }
  24. }
  25. class MyPage extends StatefulWidget {
  26. @override
  27. _MyPageState createState() => _MyPageState();
  28. }
  29. class _MyPageState extends State<MyPage> {
  30. RScanResult result;
  31. @override
  32. Widget build(BuildContext context) {
  33. return Scaffold(
  34. appBar: AppBar(
  35. title: Text('scan example'),
  36. ),
  37. body: Column(
  38. mainAxisAlignment: MainAxisAlignment.center,
  39. crossAxisAlignment: CrossAxisAlignment.center,
  40. children: <Widget>[
  41. Center(
  42. child: Text(result == null
  43. ? '点击下方按钮开始扫码'
  44. : '扫码结果${result.toString().split(',').join('\n')}')),
  45. Center(
  46. child: FlatButton(
  47. onPressed: () async {
  48. final result = await Navigator.of(context).push(
  49. MaterialPageRoute(
  50. builder: (BuildContext context) =>
  51. RScanCameraDialog()));
  52. setState(() {
  53. this.result = result;
  54. });
  55. },
  56. child: Text('RScanCamera开始扫码'),
  57. ),
  58. ),
  59. Center(
  60. child: FlatButton(
  61. onPressed: () async {
  62. final result = await Navigator.of(context).push(
  63. MaterialPageRoute(
  64. builder: (BuildContext context) => RScanDialog()));
  65. setState(() {
  66. this.result = result;
  67. });
  68. },
  69. child: Text('RScanView开始扫码(已弃用)'),
  70. ),
  71. ),
  72. Center(
  73. child: FlatButton(
  74. onPressed: () async {
  75. if (await canReadStorage()) {
  76. var image =
  77. await ImagePicker.pickImage(source: ImageSource.gallery);
  78. if (image != null) {
  79. final result = await RScan.scanImagePath(image.path);
  80. setState(() {
  81. this.result = result;
  82. });
  83. }
  84. }
  85. },
  86. child: Text('选择图片扫描'),
  87. ),
  88. ),
  89. Center(
  90. child: FlatButton(
  91. onPressed: () async {
  92. final result = await RScan.scanImageUrl(
  93. "https://s.cn.bing.net/th?id=OJ.5F0gxqWmxskS0Q&w=75&h=75&pid=MSNJVFeeds");
  94. setState(() {
  95. this.result = result;
  96. });
  97. },
  98. child: Text('网络图片解析'),
  99. ),
  100. ),
  101. Center(
  102. child: FlatButton(
  103. onPressed: () async {
  104. ByteData data = await rootBundle.load('images/qrCode.png');
  105. final result =
  106. await RScan.scanImageMemory(data.buffer.asUint8List());
  107. setState(() {
  108. this.result = result;
  109. });
  110. },
  111. child: Text('内存图片解析'),
  112. ),
  113. ),
  114. ],
  115. ),
  116. );
  117. }
  118. Future<bool> canReadStorage() async {
  119. if (Platform.isIOS) return true;
  120. var status = await PermissionHandler()
  121. .checkPermissionStatus(PermissionGroup.storage);
  122. if (status != PermissionStatus.granted) {
  123. var future = await PermissionHandler()
  124. .requestPermissions([PermissionGroup.storage]);
  125. for (final item in future.entries) {
  126. if (item.value != PermissionStatus.granted) {
  127. return false;
  128. }
  129. }
  130. } else {
  131. return true;
  132. }
  133. return true;
  134. }
  135. }