-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
268 lines (228 loc) · 9.23 KB
/
MainWindow.xaml.cs
File metadata and controls
268 lines (228 loc) · 9.23 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using DynamicWebTWAIN.RestClient;
using System.Windows;
using System.IO;
using DynamicWebTWAIN.Service;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using System.Reflection.Metadata;
namespace WpfWebviewApp;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private DWTClient? _dwtClient;
private ServiceManager? _serviceManager;
private IReadOnlyList<Scanner>? _scanners;
private string productKey = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
private ObservableCollection<BitmapImage> _scannedImages = new ObservableCollection<BitmapImage>();
private string _documentId;
public MainWindow()
{
InitializeComponent();
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
_serviceManager = new ServiceManager();
_serviceManager.CreateService();
// Use DWTClient directly without JSInterop
_dwtClient = new DWTClient(_serviceManager.Service.BaseAddress, productKey);
_scanners = await _dwtClient.ScannerControlClient.ScannerManager.GetScanners(EnumDeviceTypeMask.DT_TWAINSCANNER | EnumDeviceTypeMask.DT_WIASCANNER);
cbxSources.Items.Clear();
foreach (var scanner in _scanners)
{
if (scanner.Type == EnumDeviceTypeMask.DT_WIASCANNER)
{
cbxSources.Items.Add("WIA-" + scanner.Name);
}
else
{
cbxSources.Items.Add(scanner.Name);
}
}
if (cbxSources.Items.Count > 0)
cbxSources.SelectedIndex = 0;
thumbnailList.ItemsSource = _scannedImages;
// Create a new document
CreateDocumentOptions docOptions = new CreateDocumentOptions();
docOptions.Name = "ScannedDocument_" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
var document = await _dwtClient.DocumentManagerClient.CreateDocument(docOptions);
if (document == null)
{
MessageBox.Show("Failed to create document.");
return;
}
_documentId = document.Uid;
}
catch (Exception ex)
{
MessageBox.Show($"Initialization error: {ex.Message}\n{ex.StackTrace}");
}
}
private void Window_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
{
_dwtClient?.Dispose();
_serviceManager?.Dispose();
}
private async void btnScanToView_Click(object sender, RoutedEventArgs e)
{
try
{
if (_dwtClient == null || _scanners == null)
{
MessageBox.Show("System not initialized.");
return;
}
if (cbxSources.SelectedIndex < 0)
{
MessageBox.Show("Please select a scanner first.");
return;
}
// Create scan job options
CreateScanJobOptions options = new CreateScanJobOptions();
options.AutoRun = false; // Use event-driven scanning mode
options.Device = _scanners[cbxSources.SelectedIndex].Device;
options.Config = new ScannerConfiguration();
options.Config.IfShowUI = false;
options.Config.IfFeederEnabled = false;
options.Config.IfDuplexEnabled = false;
options.Config.IfDisableSourceAfterAcquire = true;
options.Config.PixelType = EnumDWT_PixelType.TWPT_RGB; // Color scanning
// Create scan job
var jobClient = await _dwtClient.ScannerControlClient.ScannerJobs.CreateJob(options);
// Use TaskCompletionSource to wait for scanning completion
var tcs = new TaskCompletionSource<bool>();
var scannedImages = new List<byte[]>();
var processingCount = 0;
var lockObj = new object();
// Subscribe to PageScanned event
jobClient.PageScanned += async (sender, e) =>
{
lock (lockObj) { processingCount++; }
try
{
var imageData = await jobClient.GetImageByUrl(e.Url);
scannedImages.Add(imageData);
await _dwtClient.DocumentManagerClient.AddImageToDocument(_documentId, e.Url);
}
catch (Exception ex)
{
Application.Current.Dispatcher.Invoke(() =>
{
MessageBox.Show($"Error getting scanned image: {ex.Message}", "Scan error");
});
}
finally
{
lock (lockObj) { processingCount--; }
}
};
// Subscribe to TransferEnded event
jobClient.TransferEnded += async (sender, e) =>
{
// Wait for all PageScanned handlers to complete
for (int i = 0; i < 100; i++) // Max wait 10 seconds
{
lock (lockObj)
{
if (processingCount == 0) break;
}
await Task.Delay(100);
}
tcs.SetResult(true);
};
// Start scan job
await jobClient.StartJob();
// Wait for scan completion (with timeout)
var completedTask = await Task.WhenAny(tcs.Task, Task.Delay(TimeSpan.FromSeconds(120)));
if (completedTask != tcs.Task)
{
MessageBox.Show("Scan timeout.", "Scan error");
}
else
{
// Add all scanned images to display list
if (scannedImages.Count > 0)
{
// Append new scanned images without clearing existing ones
int firstNewImageIndex = _scannedImages.Count;
foreach (var imageData in scannedImages)
{
var bitmapImage = BytesToBitmapImage(imageData);
_scannedImages.Add(bitmapImage);
}
// Select the first newly scanned image
if (_scannedImages.Count > 0)
{
mainImage.Source = _scannedImages[firstNewImageIndex];
thumbnailList.SelectedIndex = firstNewImageIndex;
}
//MessageBox.Show($"Successfully scanned {scannedImages.Count} image(s).\nTotal images: {_scannedImages.Count}");
}
else
{
MessageBox.Show("No images were scanned.", "Scan Info");
}
}
// Cleanup
await jobClient.DeleteJob();
}
catch (Exception ex)
{
MessageBox.Show($"Scan error: {ex.Message}\n{ex.StackTrace}");
}
}
private async void btnSaveAsPdf_Click(object sender, RoutedEventArgs e)
{
try
{
if (_dwtClient == null)
{
MessageBox.Show("System not initialized.");
return;
}
if (_scannedImages.Count == 0)
{
MessageBox.Show("No images to save. Please scan images first.");
return;
}
// Call SaveDocumentAsPDF to get PDF blob (equivalent to: let blob = await response.blob())
byte[] pdfBlob = await _dwtClient.DocumentManagerClient.SaveDocumentAsPDF(_documentId);
if (pdfBlob == null || pdfBlob.Length == 0)
{
MessageBox.Show("Failed to get PDF blob data.");
return;
}
// Save blob as local PDF file
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), $"ScannedDocument_{DateTime.Now:yyyyMMdd_HHmmss}.pdf");
File.WriteAllBytes(filePath, pdfBlob);
MessageBox.Show($"PDF saved successfully!\nLocation: {filePath}\nTotal pages: {_scannedImages.Count}");
}
catch (Exception ex)
{
MessageBox.Show($"Save PDF error: {ex.Message}\n{ex.StackTrace}");
}
}
private void ThumbnailList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (thumbnailList.SelectedIndex >= 0 && thumbnailList.SelectedIndex < _scannedImages.Count)
{
mainImage.Source = _scannedImages[thumbnailList.SelectedIndex];
}
}
private BitmapImage BytesToBitmapImage(byte[] imageBytes)
{
var bitmapImage = new BitmapImage();
using (var stream = new MemoryStream(imageBytes))
{
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
bitmapImage.Freeze();
}
return bitmapImage;
}
}