-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.dart
175 lines (160 loc) · 5.38 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_twain_scanner/flutter_twain_scanner.dart';
import 'package:flutter_twain_scanner/dynamsoft_service.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _flutterTwainScannerPlugin = FlutterTwainScanner();
List<String> scannerNames = [];
String? _selectedScanner;
String host = 'http://192.168.8.72:18622'; // Visit http://127.0.0.1:18625/ and replace 127.0.0.1 with your LAN IP address.
final DynamsoftService dynamsoftService = DynamsoftService();
List<dynamic> devices = [];
List<Uint8List> imagePaths = [];
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
if (!mounted) return;
}
Future<void> _scanDocument(int index) async {
final Map<String, dynamic> parameters = {
'license':
'LICENSE-KEY',
'device': devices[index]['device'],
};
parameters['config'] = {
'IfShowUI': false,
'PixelType': 2,
// 'XferCount': 1,
// 'PageSize': 1,
'Resolution': 200,
'IfFeederEnabled': false,
'IfDuplexEnabled': false,
};
try {
final String jobId =
await dynamsoftService.scanDocument(host, parameters);
if (jobId != '') {
List<Uint8List> paths =
await dynamsoftService.getImageStreams(host, jobId);
await dynamsoftService.deleteJob(host, jobId);
if (paths.isNotEmpty) {
setState(() {
imagePaths.insertAll(0, paths);
});
}
}
} catch (error) {
print('An error occurred: $error');
}
}
@override
Widget build(BuildContext context) {
Row row = Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
MaterialButton(
textColor: Colors.white,
color: Colors.blue,
onPressed: () async {
// List<String>? scanners =
// await _flutterTwainScannerPlugin.getDataSources();
try {
final scanners = await dynamsoftService.getDevices(host,
ScannerType.TWAINSCANNER | ScannerType.TWAINX64SCANNER);
for (var i = 0; i < scanners.length; i++) {
devices.add(scanners[i]);
scannerNames.add(scanners[i]['name']);
}
} catch (error) {
print('An error occurred: $error');
}
// if (scanners != null) {
// setState(() {
// // scannerNames = scanners;
// _selectedScanner = scanners[0];
// });
// }
if (devices.isNotEmpty) {
setState(() {
// scannerNames = scanners;
_selectedScanner = devices[0]['name'];
});
}
},
child: const Text('List Scanners')),
MaterialButton(
textColor: Colors.white,
color: Colors.blue,
onPressed: () async {
if (_selectedScanner != null) {
int index = scannerNames.indexOf(_selectedScanner!);
// imagePaths =
// await _flutterTwainScannerPlugin.scanDocument(index);
// setState(() {});
await _scanDocument(index);
}
},
child: const Text('Scan Document')),
]);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter TWAIN Scanner'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
height: 100,
child: row,
),
DropdownButton(
hint:
const Text('Select a scanner'), // Not necessary for Option 1
value: _selectedScanner,
onChanged: (newValue) {
setState(() {
_selectedScanner = newValue;
});
},
items: scannerNames.map((location) {
return DropdownMenuItem(
value: location,
child: Text(location),
);
}).toList(),
),
Expanded(
child: imagePaths.isEmpty
? Image.asset('images/default.png')
: ListView.builder(
itemCount: imagePaths.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(10.0),
child: Image.memory(
imagePaths[index],
fit: BoxFit.contain,
), // Replace with Image.file() for local images
);
},
))
],
),
),
);
}
}