-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglePagePreviewActivity.kt
361 lines (316 loc) · 12.6 KB
/
SinglePagePreviewActivity.kt
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.example.scanbot.preview
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.scanbot.ExampleApplication
import com.example.scanbot.di.ExampleSingletonImpl
import com.example.scanbot.sharing.SaveListener
import com.example.scanbot.sharing.SharingDocumentStorage
import com.example.scanbot.usecases.GenerateJpgForSharingUseCase
import com.example.scanbot.usecases.GeneratePdfForSharingUseCase
import com.example.scanbot.usecases.GeneratePngForSharingUseCase
import com.example.scanbot.usecases.GenerateTiffForSharingUseCase
import com.example.scanbot.utils.ExampleUtils
import com.example.scanbot.utils.ExampleUtils.showEncryptedDocumentToast
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.persistence.Page
import io.scanbot.sdk.persistence.PageFileStorage
import io.scanbot.sdk.process.ImageFilterType
import io.scanbot.sdk.ui.registerForActivityResultOk
import io.scanbot.sdk.ui.view.edit.CroppingActivity
import io.scanbot.sdk.ui.view.edit.configuration.CroppingConfiguration
import io.scanbot.sdk.usecases.documents.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import kotlin.coroutines.CoroutineContext
class SinglePagePreviewActivity : AppCompatActivity(), FiltersListener, SaveListener,
CoroutineScope {
private lateinit var imageView: ImageView
private lateinit var exampleSingleton: ExampleSingletonImpl
private lateinit var exportPdf: GeneratePdfForSharingUseCase
private lateinit var exportJpeg: GenerateJpgForSharingUseCase
private lateinit var exportPng: GeneratePngForSharingUseCase
private lateinit var exportTiff: GenerateTiffForSharingUseCase
private lateinit var page: Page
private lateinit var filtersSheetFragment: FiltersBottomSheetMenuFragment
private lateinit var saveSheetFragment: SaveBottomSheetMenuFragment
private lateinit var scanbotSDK: ScanbotSDK
private lateinit var progress: ProgressBar
private var job: Job = Job()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Default + job
private val croppingResult: ActivityResultLauncher<CroppingConfiguration> =
registerForActivityResultOk(CroppingActivity.ResultContract()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
if (result.result != null) {
page = result.result!!
updateImageView()
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_page_preview)
initActionBar()
initMenu()
exampleSingleton = ExampleSingletonImpl(this)
val sharingDocumentStorage = SharingDocumentStorage(this)
exportPdf = GeneratePdfForSharingUseCase(
sharingDocumentStorage,
exampleSingleton.pagePDFRenderer()
)
exportTiff = GenerateTiffForSharingUseCase(
sharingDocumentStorage,
exampleSingleton.pageFileStorageInstance(),
exampleSingleton.pageTIFFWriter()
)
exportJpeg = GenerateJpgForSharingUseCase(
sharingDocumentStorage,
exampleSingleton.pageFileStorageInstance()
)
exportPng = GeneratePngForSharingUseCase(
sharingDocumentStorage,
exampleSingleton.pageFileStorageInstance()
)
scanbotSDK = ScanbotSDK(application)
imageView = findViewById(R.id.image)
val pagesId =
intent.getStringExtra("page")
?: throw IllegalStateException("No page id")
page = Page(pageId = pagesId)
val actionFilter = findViewById<TextView>(R.id.action_filter)
actionFilter.setOnClickListener {
val fragment = supportFragmentManager.findFragmentByTag(FILTERS_MENU_TAG)
if (fragment == null) {
filtersSheetFragment.show(supportFragmentManager, FILTERS_MENU_TAG)
}
}
val actionCrop = findViewById<TextView>(R.id.action_crop)
actionCrop.setOnClickListener {
runCroppingUseCase()
}
val actionBlur = findViewById<TextView>(R.id.action_blur)
actionBlur.setOnClickListener {
runBlurDetection()
}
val export = findViewById<TextView>(R.id.action_export)
export.setOnClickListener {
val fragment = supportFragmentManager.findFragmentByTag(SAVE_MENU_TAG)
if (fragment == null) {
saveSheetFragment.show(supportFragmentManager, SAVE_MENU_TAG)
}
}
}
private fun runBlurDetection() {
if (!scanbotSDK.licenseInfo.isValid) {
showLicenseToast()
} else {
progress.visibility = View.VISIBLE
launch {
val image = withContext(Dispatchers.Default) {
exampleSingleton.pageFileStorageInstance()
.getImage(page.pageId, PageFileStorage.PageFileType.DOCUMENT)
}
withContext(Dispatchers.Main) {
image?.let {
// 0 - no blur, 1 - blurry. Please note that image blurriness is a subjective value and depends on the use case and document content. So 0.5 is vary a lot for different documents.
val blurValue =
exampleSingleton.pageBlurDetector().estimateInBitmap(image, 0)
val text = if (blurValue < 0.5) {
"Document is ok"
} else {
"Document is not ok"
}
Toast.makeText(this@SinglePagePreviewActivity, text, Toast.LENGTH_LONG)
.show()
updateImageView()
progress.visibility = View.GONE
}
}
}
}
}
private fun runCroppingUseCase() {
if (!scanbotSDK.licenseInfo.isValid) {
showLicenseToast()
return
}
val configuration = CroppingConfiguration(page)
// apply styling parameters by configuration.setColor() methods
croppingResult.launch(configuration)
}
private fun updateImageView() {
val image = exampleSingleton.pageFileStorageInstance()
.getImage(page.pageId, PageFileStorage.PageFileType.DOCUMENT)
progress = findViewById(R.id.progressBar)
imageView.setImageBitmap(image)
}
override fun onDestroy() {
super.onDestroy()
job.cancel()
}
private fun initMenu() {
val fragment = supportFragmentManager.findFragmentByTag(FILTERS_MENU_TAG)
if (fragment != null) {
supportFragmentManager
.beginTransaction()
.remove(fragment)
.commitNow()
}
filtersSheetFragment = FiltersBottomSheetMenuFragment()
val fragment2 = supportFragmentManager.findFragmentByTag(SAVE_MENU_TAG)
if (fragment2 != null) {
supportFragmentManager
.beginTransaction()
.remove(fragment2)
.commitNow()
}
saveSheetFragment = SaveBottomSheetMenuFragment(true)
}
private fun initActionBar() {
supportActionBar?.title = getString(R.string.scan_results)
}
override fun onFilterApplied(filterType: ImageFilterType) {
applyFilter(filterType)
}
override fun savePdf() {
saveDocumentPdf()
}
override fun saveTiff() {
saveDocumentTiff()
}
override fun saveJpeg() {
saveDocumentImage(true)
}
override fun savePng() {
saveDocumentImage(false)
}
private fun applyFilter(imageFilterType: ImageFilterType) {
if (!scanbotSDK.licenseInfo.isValid) {
showLicenseToast()
} else {
progress.visibility = View.VISIBLE
launch {
page = exampleSingleton.pageProcessorInstance()
.applyFilter(page, imageFilterType)
withContext(Dispatchers.Main) {
updateImageView()
progress.visibility = View.GONE
}
}
}
}
override fun onResume() {
super.onResume()
if (!scanbotSDK.licenseInfo.isValid) {
showLicenseToast()
return
}
updateImageView()
}
private fun showLicenseToast() {
Toast.makeText(this@SinglePagePreviewActivity, "License is not valid!", Toast.LENGTH_LONG)
.show()
}
private fun saveDocumentImage(withJpeg: Boolean) {
if (!scanbotSDK.licenseInfo.isValid) {
showLicenseToast()
} else {
progress.visibility = View.VISIBLE
lifecycleScope.launch {
val pdfFile: File? =
withContext(Dispatchers.Default) {
if (withJpeg) {
exportJpeg.generate(listOf(page.pageId)).firstOrNull()
} else {
exportPng.generate(listOf(page.pageId)).firstOrNull()
}
}
withContext(Dispatchers.Main) {
progress.visibility = View.GONE
//open first document
if (pdfFile != null) {
if (!ExampleApplication.USE_ENCRYPTION) {
ExampleUtils.openDocument(this@SinglePagePreviewActivity, pdfFile)
} else {
showEncryptedDocumentToast(
this@SinglePagePreviewActivity,
pdfFile,
exampleSingleton.fileIOProcessor()
)
}
}
}
}
}
}
private fun saveDocumentPdf() {
if (!scanbotSDK.licenseInfo.isValid) {
showLicenseToast()
} else {
progress.visibility = View.VISIBLE
lifecycleScope.launch {
val pdfFile: File? = withContext(Dispatchers.Default) {
exportPdf.generate(listOf(page.pageId)).firstOrNull()
}
withContext(Dispatchers.Main) {
progress.visibility = View.GONE
//open first document
if (pdfFile != null) {
if (!ExampleApplication.USE_ENCRYPTION) {
ExampleUtils.openDocument(this@SinglePagePreviewActivity, pdfFile)
} else {
showEncryptedDocumentToast(
this@SinglePagePreviewActivity,
pdfFile,
exampleSingleton.fileIOProcessor()
)
}
}
}
}
}
}
private fun saveDocumentTiff() {
if (!scanbotSDK.licenseInfo.isValid) {
showLicenseToast()
} else {
progress.visibility = View.VISIBLE
lifecycleScope.launch {
val pdfFile: File? = withContext(Dispatchers.Default) {
exportTiff.generate(listOf(page.pageId)).firstOrNull()
}
withContext(Dispatchers.Main) {
progress.visibility = View.GONE
//open first document
if (pdfFile != null) {
if (!ExampleApplication.USE_ENCRYPTION) {
ExampleUtils.openDocument(this@SinglePagePreviewActivity, pdfFile)
} else {
showEncryptedDocumentToast(
this@SinglePagePreviewActivity,
pdfFile,
exampleSingleton.fileIOProcessor()
)
}
}
}
}
}
}
companion object {
private const val FILTERS_MENU_TAG = "FILTERS_MENU_TAG"
private const val SAVE_MENU_TAG = "SAVE_MENU_TAG"
}
}