-
Notifications
You must be signed in to change notification settings - Fork 1
/
PageFilterActivity.cs
200 lines (166 loc) · 7.16 KB
/
PageFilterActivity.cs
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
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using AndroidX.AppCompat.App;
using IO.Scanbot.Sdk.Persistence;
using IO.Scanbot.Sdk.Process;
using IO.Scanbot.Sdk.UI.View.Edit;
using IO.Scanbot.Sdk.UI.View.Edit.Configuration;
using ReadyToUseUI.Droid.Fragments;
using ReadyToUseUI.Droid.Listeners;
using ReadyToUseUI.Droid.Utils;
using DocumentSDK.NET.Model;
namespace ReadyToUseUI.Droid.Activities
{
[Activity]
public class PageFilterActivity : AppCompatActivity, IFiltersListener
{
const string FILTERS_MENU_TAG = "FILTERS_MENU_TAG";
const string CHOOSE_FILTERS_DIALOG_TAG = "CHOOSE_FILTERS_DIALOG_TAG";
const int CROP_DEFAULT_UI_REQUEST_CODE = 9999;
TextView crop, delete, checkQuality, filter;
public static Intent CreateIntent(Context context, string pageId)
{
var intent = new Intent(context, typeof(PageFilterActivity));
intent.PutExtra(nameof(selectedPageId), pageId);
return intent;
}
private string selectedPageId;
private ImageFilterType selectedFilter;
private FilterBottomSheetMenuFragment filterFragment;
private ProgressBar progress;
private IO.Scanbot.Sdk.ScanbotSDK scanbotSDK;
private IO.Scanbot.Sdk.Persistence.PageFileStorage pageStorage;
private IO.Scanbot.Sdk.Docprocessing.PageProcessor pageProcessor;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
scanbotSDK = new IO.Scanbot.Sdk.ScanbotSDK(this);
pageStorage = scanbotSDK.CreatePageFileStorage();
pageProcessor = scanbotSDK.CreatePageProcessor();
SetContentView(Resource.Layout.activity_filters);
SetupToolbar();
progress = FindViewById<ProgressBar>(Resource.Id.progress);
selectedPageId = Intent.GetStringExtra(nameof(selectedPageId));
crop = FindViewById<TextView>(Resource.Id.action_crop_and_rotate);
crop.Text = Texts.crop_amp_rotate;
filter = FindViewById<TextView>(Resource.Id.action_filter);
filter.Text = Texts.filter;
filter.Click += delegate
{
filterFragment.Show(SupportFragmentManager, CHOOSE_FILTERS_DIALOG_TAG);
};
delete = FindViewById<TextView>(Resource.Id.action_delete);
delete.Text = Texts.delete;
delete.Click += delegate
{
pageStorage.Remove(selectedPageId);
Finish();
};
checkQuality = FindViewById<TextView>(Resource.Id.action_check_quality);
checkQuality.Text = Texts.check_document_quality;
checkQuality.Click += delegate
{
var bitmap = pageStorage.GetPreviewImage(selectedPageId, PageFileStorage.PageFileType.Document, null);
var qualityAnalyzer = scanbotSDK.CreateDocumentQualityAnalyzer();
var documentQualityResult = qualityAnalyzer.AnalyzeInBitmap(bitmap, 0);
Alert.ShowAlert(this, "Document Quality", documentQualityResult.Name());
};
FindViewById(Resource.Id.action_crop_and_rotate).Click += delegate
{
var configuration = new CroppingConfiguration(new Page().Copy(pageId: selectedPageId));
configuration.SetPolygonColor(Color.Red);
configuration.SetPolygonColorMagnetic(Color.Blue);
var intent = CroppingActivity.NewIntent(this, configuration);
StartActivityForResult(intent, CROP_DEFAULT_UI_REQUEST_CODE);
};
var fragmentFilterMenu = SupportFragmentManager.FindFragmentByTag(FILTERS_MENU_TAG);
if (fragmentFilterMenu != null)
{
SupportFragmentManager.BeginTransaction().Remove(fragmentFilterMenu).CommitNow();
}
filterFragment = new FilterBottomSheetMenuFragment();
if (!scanbotSDK.LicenseInfo.IsValid)
{
Alert.ShowLicenseDialog(this);
}
else
{
GeneratePreviewImage();
}
}
private void SetupToolbar()
{
var toolbar = FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.Title = Texts.page_title;
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
}
private void GeneratePreviewImage()
{
progress.Visibility = ViewStates.Visible;
Task.Run(delegate
{
var uri = pageStorage.GetFilteredPreviewImageURI(selectedPageId, ImageFilterType.None);
if (!File.Exists(uri.Path))
{
pageProcessor.GenerateFilteredPreview(new Page().Copy(pageId: selectedPageId), ImageFilterType.None);
}
UpdateImage(uri);
});
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode != Result.Ok)
{
return;
}
if (requestCode == CROP_DEFAULT_UI_REQUEST_CODE)
{
selectedFilter = selectedFilter ?? ImageFilterType.None;
var uri = pageStorage.GetFilteredPreviewImageURI(selectedPageId, selectedFilter);
if (!File.Exists(uri.Path))
{
scanbotSDK.CreatePageProcessor().GenerateFilteredPreview(new Page().Copy(pageId: selectedPageId), selectedFilter);
}
UpdateImage(uri);
}
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId == Android.Resource.Id.Home)
{
base.OnBackPressed();
return true;
}
return base.OnOptionsItemSelected(item);
}
public void ApplyFilter(ImageFilterType type)
{
progress.Visibility = ViewStates.Visible;
selectedFilter = type;
Task.Run(delegate
{
var pageToFilter = new Page().Copy(pageId: selectedPageId);
pageProcessor.ApplyFilter(pageToFilter, selectedFilter);
pageProcessor.GenerateFilteredPreview(pageToFilter, selectedFilter);
var uri = pageStorage.GetFilteredPreviewImageURI(selectedPageId, selectedFilter);
UpdateImage(uri);
});
}
private void UpdateImage(Android.Net.Uri uri)
{
RunOnUiThread(delegate
{
var image = FindViewById<ImageView>(Resource.Id.image);
image.SetImageBitmap(null);
image.SetImageBitmap(scanbotSDK.FileIOProcessor().ReadImage(uri, new BitmapFactory.Options()));
progress.Visibility = ViewStates.Gone;
});
}
}
}