|
1 | 1 | package io.scanbot.example |
2 | 2 |
|
3 | 3 | import android.content.Intent |
4 | | -import android.content.pm.PackageManager |
5 | | -import android.net.Uri |
6 | 4 | import android.os.Bundle |
7 | | -import android.util.Log |
8 | | -import android.view.View |
9 | | -import androidx.activity.result.PickVisualMediaRequest |
10 | | -import androidx.activity.result.contract.ActivityResultContracts |
11 | 5 | import androidx.appcompat.app.AppCompatActivity |
12 | | -import androidx.core.content.FileProvider |
13 | | -import androidx.core.net.toFile |
14 | | -import androidx.lifecycle.lifecycleScope |
15 | | -import io.scanbot.common.mapSuccess |
16 | | -import io.scanbot.common.onSuccess |
17 | | -import io.scanbot.example.common.Const |
18 | 6 | import io.scanbot.example.common.applyEdgeToEdge |
19 | | -import io.scanbot.example.common.showToast |
20 | 7 | import io.scanbot.example.databinding.ActivityMainBinding |
21 | | -import io.scanbot.sdk.ScanbotSDK |
22 | | -import io.scanbot.sdk.image.ImageRef |
23 | | -import io.scanbot.sdk.imageprocessing.ParametricFilter |
24 | | -import io.scanbot.sdk.ocr.OcrEngineManager |
25 | | -import io.scanbot.sdk.pdfgeneration.PageSize |
26 | | -import io.scanbot.sdk.pdfgeneration.PdfConfiguration |
27 | | -import io.scanbot.sdk.pdfgeneration.PdfGenerator |
28 | | -import io.scanbot.sdk.util.PolygonHelper |
29 | | -import kotlinx.coroutines.Dispatchers |
30 | | -import kotlinx.coroutines.launch |
31 | | -import kotlinx.coroutines.runBlocking |
32 | | -import kotlinx.coroutines.withContext |
33 | | -import java.io.File |
34 | | - |
35 | | -/** |
36 | | -Ths example uses new sdk APIs presented in Scanbot SDK v.8.x.x |
37 | | -Please, check the official documentation for more details: |
38 | | -Result API https://docs.scanbot.io/android/document-scanner-sdk/detailed-setup-guide/result-api/ |
39 | | -ImageRef API https://docs.scanbot.io/android/document-scanner-sdk/detailed-setup-guide/image-ref-api/ |
40 | | - */ |
41 | 8 |
|
42 | 9 | class MainActivity : AppCompatActivity() { |
43 | | - private val runOcr = false |
44 | | - private val scanbotSdk by lazy { ScanbotSDK(this) } |
45 | | - private val pdfGenerator by lazy { scanbotSdk.createPdfGenerator(if (runOcr) OcrEngineManager.OcrConfig() else null) } |
46 | | - |
47 | | - private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } |
48 | | - |
49 | | - private val selectGalleryImageResultLauncher = |
50 | | - // limit to 5 images for example purposes |
51 | | - registerForActivityResult(ActivityResultContracts.PickMultipleVisualMedia(5)) { uris -> |
52 | | - if (uris.isEmpty()) { |
53 | | - this@MainActivity.showToast("No images were selected!") |
54 | | - Log.w(Const.LOG_TAG, "No images were selected!") |
55 | | - return@registerForActivityResult |
56 | | - } |
57 | | - |
58 | | - if (!scanbotSdk.licenseInfo.isValid) { |
59 | | - this@MainActivity.showToast("Scanbot SDK license (1-minute trial) has expired!") |
60 | | - Log.w(Const.LOG_TAG, "Scanbot SDK license (1-minute trial) has expired!") |
61 | | - return@registerForActivityResult |
62 | | - } |
63 | | - |
64 | | - lifecycleScope.launch { processDocument(uris, isGrayscaleChecked) } |
65 | | - } |
66 | | - |
67 | | - private val isGrayscaleChecked: Boolean |
68 | | - get() = binding.grayscaleCheckBox.isChecked |
69 | 10 |
|
70 | 11 | override fun onCreate(savedInstanceState: Bundle?) { |
71 | 12 | super.onCreate(savedInstanceState) |
| 13 | + val binding = ActivityMainBinding.inflate(layoutInflater) |
72 | 14 | setContentView(binding.root) |
73 | 15 | supportActionBar!!.hide() |
74 | | - applyEdgeToEdge(findViewById(R.id.root_view)) |
75 | | - |
76 | | - binding.scanButton.setOnClickListener { |
77 | | - selectGalleryImageResultLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)) |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - private suspend fun processDocument(uris: List<Uri>, applyGrayscale: Boolean) { |
82 | | - val filters = |
83 | | - if (applyGrayscale) listOf(ParametricFilter.grayscaleFilter()) else emptyList() |
84 | | - withContext(Dispatchers.Main) { binding.progressBar.visibility = View.VISIBLE } |
85 | | - |
86 | | - withContext(Dispatchers.Default) { |
87 | | - scanbotSdk.createDocumentScanner().mapSuccess { documentScanner -> |
88 | | - //can be handled with .getOrNull() if needed |
89 | | - val document = scanbotSdk.documentApi.createDocument() |
90 | | - .getOrReturn() //can be handled with .getOrNull() if needed |
91 | | - uris.asSequence().forEach { uri -> |
92 | | - val imageRef = contentResolver.openInputStream(uri)?.use { inputStream -> |
93 | | - ImageRef.fromInputStream(inputStream) |
94 | | - } |
95 | | - if (imageRef == null) { |
96 | | - Log.w(Const.LOG_TAG, "Cannot open input stream from URI: $uri") |
97 | | - return@forEach |
98 | | - } |
99 | | - val newPolygon = documentScanner.run(imageRef).getOrNull()?.pointsNormalized |
100 | | - ?: PolygonHelper.getFullPolygon() |
101 | | - val page = document.addPage(imageRef) |
102 | | - .getOrReturn() //can be handled with .getOrNull() if needed |
103 | | - page.apply(newPolygon = newPolygon, newFilters = filters) |
104 | | - } |
105 | | - pdfGenerator.generate( |
106 | | - document, |
107 | | - PdfConfiguration.default().copy(pageSize = PageSize.A4) |
108 | | - ) |
109 | | - document.pdfUri.toFile() |
110 | | - }.onSuccess { renderedPdfFile -> |
111 | | - runBlocking(Dispatchers.Main) { |
112 | | - binding.progressBar.visibility = View.GONE |
113 | | - openPdfDocument(renderedPdfFile) |
114 | | - } |
115 | | - } |
116 | | - } |
117 | | - } |
| 16 | + applyEdgeToEdge(binding.root) |
118 | 17 |
|
119 | | - private fun openPdfDocument(file: File) { |
120 | | - val uri = FileProvider.getUriForFile(this, "${this.packageName}.provider", file) |
121 | | - |
122 | | - val openIntent = Intent(Intent.ACTION_VIEW).apply { |
123 | | - setDataAndType(uri, "application/pdf") |
124 | | - flags = Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 18 | + binding.pdfButton.setOnClickListener { |
| 19 | + val intent = Intent(this, PdfActivity::class.java) |
| 20 | + startActivity(intent) |
125 | 21 | } |
126 | 22 |
|
127 | | - if (intent.resolveActivity(packageManager) != null) { |
128 | | - val chooser = Intent.createChooser(openIntent, file.name) |
129 | | - val resInfoList = this.packageManager.queryIntentActivities( |
130 | | - chooser, |
131 | | - PackageManager.MATCH_DEFAULT_ONLY |
132 | | - ) |
133 | | - |
134 | | - for (resolveInfo in resInfoList) { |
135 | | - val packageName = resolveInfo.activityInfo.packageName |
136 | | - grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION) |
137 | | - } |
138 | | - startActivity(chooser) |
139 | | - } else { |
140 | | - // Handle the case where no app can open PDF files |
141 | | - showToast("No app found to open PDF files") |
142 | | - Log.w(Const.LOG_TAG, "No app found to open PDF files") |
| 23 | + binding.pdfWithOcrButton.setOnClickListener { |
| 24 | + val intent = Intent(this, PdfWithOcrActivity::class.java) |
| 25 | + startActivity(intent) |
143 | 26 | } |
144 | 27 | } |
145 | 28 | } |
0 commit comments