forked from kalinjul/EasyDocumentScan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentScanner.android.kt
More file actions
83 lines (73 loc) · 3.43 KB
/
Copy pathDocumentScanner.android.kt
File metadata and controls
83 lines (73 loc) · 3.43 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
package io.github.kalinjul.easydocumentscan
import android.app.Activity.RESULT_CANCELED
import android.app.Activity.RESULT_OK
import android.content.IntentSender
import android.util.Log
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions
import com.google.mlkit.vision.documentscanner.GmsDocumentScanning
import com.google.mlkit.vision.documentscanner.GmsDocumentScanningResult
@Composable
actual fun rememberDocumentScanner(
onResult: (Result<List<KmpImage>>) -> Unit,
options: DocumentScannerOptions
): DocumentScanner {
val context = LocalContext.current
val options = remember {
val scannerMode = when (options.android.scannerMode) {
DocumentScannerModeAndroid.FULL -> GmsDocumentScannerOptions.SCANNER_MODE_FULL
DocumentScannerModeAndroid.BASE_WITH_FILTER -> GmsDocumentScannerOptions.SCANNER_MODE_BASE_WITH_FILTER
DocumentScannerModeAndroid.BASE -> GmsDocumentScannerOptions.SCANNER_MODE_BASE
}
GmsDocumentScannerOptions.Builder()
.setResultFormats(GmsDocumentScannerOptions.RESULT_FORMAT_JPEG) // PDF format not supported on ios, so disable in android, too
.setGalleryImportAllowed(options.android.allowGalleryImport)
.setScannerMode(scannerMode)
.setPageLimit(options.android.pageLimit)
.build()
}
val gmsScanner = remember(options) {
GmsDocumentScanning.getClient(options)
}
val scannerLauncher = rememberLauncherForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val gmsResult = GmsDocumentScanningResult.fromActivityResultIntent(result.data)
gmsResult?.pages?.let { pages ->
val images = pages.map {
AndroidImage(platformFile = it.imageUri, type = "jpeg", contentResolver = context.contentResolver)
}
onResult(Result.success(images))
}
// gmsResult?.pdf?.let { pdf ->
// val pdfUri = pdf.uri
// println("PDF: $pdfUri")
// onScanned(pdfUri.toString())
// }
} else if(result.resultCode == RESULT_CANCELED) {
onResult(Result.failure(DocumentScannerException.Canceled(message = null)))
} else {
onResult(Result.failure(DocumentScannerException.Unknown("Unknown activity result code: ${result.resultCode}")))
}
}
val documentScanner = remember(gmsScanner, scannerLauncher, context) {
object: DocumentScanner {
override fun scan() {
context.getActivityOrNull()?.let {
gmsScanner.getStartScanIntent(it)
.addOnSuccessListener { intentSender: IntentSender ->
scannerLauncher.launch(IntentSenderRequest.Builder(intentSender).build())
}
.addOnFailureListener { e: Exception ->
e.message?.let { Log.e("error", it) }
}
}
}
}
}
return documentScanner
}