Skip to content

Commit b1cd64d

Browse files
committed
feat(Avalonia): add OCR drag&drop
1 parent e42ac46 commit b1cd64d

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

SnapX.Avalonia/Views/OCR.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</Design.DataContext>
1818

1919
<Grid
20+
DragDrop.AllowDrop="True"
2021
Margin="20"
2122
RowDefinitions="Auto,Auto,Auto,Auto"
2223
RowSpacing="15"

SnapX.Avalonia/Views/OCR.axaml.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public OCR(HistoryItem? item, OCRViewModel viewModel, SixLabors.ImageSharp.Image
5353
{
5454
Title = "OCR Tool";
5555
}
56+
57+
AddHandler(DragDrop.DropEvent, Drop);
5658
// LanguageSelector = this.FindControl<ComboBox>("LanguageSelector");
5759
// LanguageSelector!.ItemsSource = viewModel.LanguageDisplayNames;
5860
// LanguageSelector.Items = _languages;
@@ -61,7 +63,32 @@ public OCR(HistoryItem? item, OCRViewModel viewModel, SixLabors.ImageSharp.Image
6163
// LoadImage();
6264
// RunOCR(_languages[0]);
6365
}
66+
async void Drop(object? sender, DragEventArgs e)
67+
{
68+
if (e.DataTransfer.TryGetFiles()?.FirstOrDefault() is not { } storageFile) return;
69+
70+
try
71+
{
72+
await using Stream stream = File.OpenRead(storageFile.Path.LocalPath);
73+
_img = await SixLabors.ImageSharp.Image.LoadAsync<Rgba32>(stream);
6474

75+
if (LanguageSelector?.SelectedIndex is not (>= 0 and int index)) return;
76+
77+
Title = $"OCR Result for dropped file {_img.Metadata.DecodedImageFormat?.Name}";
78+
var code = _ocrViewModel.GetLanguageCode(index);
79+
await RunOCRAsync(code);
80+
}
81+
catch (Exception ex)
82+
{
83+
DebugHelper.WriteException(ex);
84+
await new ContentDialog
85+
{
86+
Title = "Drop Error",
87+
Content = ex.Message,
88+
CloseButtonText = "OK"
89+
}.ShowAsync(this);
90+
}
91+
}
6592
public OCR()
6693
: this(null, new OCRViewModel()) { }
6794
public OCR(SixLabors.ImageSharp.Image img)

0 commit comments

Comments
 (0)