1
+ package com.extrieve.quickcapture.docappkotlin
2
+
3
+ import android.content.ContextWrapper
4
+ import android.content.Intent
5
+ import android.content.pm.PackageManager
6
+ import android.graphics.BitmapFactory
7
+ import android.net.Uri
8
+ import android.os.Build
9
+ import androidx.appcompat.app.AppCompatActivity
10
+ import android.os.Bundle
11
+ import android.util.Log
12
+ import android.view.View
13
+ import android.widget.ImageView
14
+ import android.widget.Toast
15
+ import androidx.activity.result.ActivityResult
16
+ import androidx.activity.result.ActivityResultLauncher
17
+ import androidx.activity.result.contract.ActivityResultContracts
18
+ import androidx.core.app.ActivityCompat
19
+ import androidx.core.content.ContextCompat
20
+ import java.io.File
21
+ import java.io.IOException
22
+
23
+ import com.extrieve.quickcapture.sdk.*;
24
+
25
+ class MainActivity : AppCompatActivity () {
26
+ /* DEV_HELP : Declare variables for the classes from SDK*/
27
+ private var cameraHelper: CameraHelper ? = null
28
+ private var imageHelper: ImgHelper ? = null
29
+
30
+ /* DEV_HELP : Declare variables for ActivityResultLauncher to accept result from camera activity
31
+ * As CameraHelper is an activity based class*/
32
+ private var captureActivityResultLauncher: ActivityResultLauncher <Intent >? = null
33
+ private val REQUIREDPERMISSIONS = arrayOf(" android.permission.CAMERA" )
34
+ var fileCollection: ArrayList <String >? = null
35
+
36
+ override fun onCreate (savedInstanceState : Bundle ? ) {
37
+ super .onCreate(savedInstanceState)
38
+ setContentView(R .layout.activity_main)
39
+ checkAndPromptPermissions()
40
+
41
+ /* DEV_HELP : Initialise object of ImgHelper class.Pass the current activity context*/
42
+ imageHelper = ImgHelper (this )
43
+ /* DEV_HELP : Initialise object CameraHelper*/
44
+ cameraHelper = CameraHelper ()
45
+
46
+ /* DEV_HELP : assign registerForActivityResult for getting result from CameraHelper*/
47
+ captureActivityResultLauncher = registerForActivityResult(ActivityResultContracts .StartActivityForResult ()) {
48
+ result: ActivityResult -> handleCaptureActivityResult(result)
49
+ }
50
+
51
+ /* DEV_HELP : Capture Document with SDK Button click handler*/
52
+ findViewById<View >(R .id.getPictureButton).setOnClickListener {
53
+ setConfig()
54
+ openCameraActivity()
55
+ }
56
+ }
57
+
58
+ /* DEV_HELP : Basic permission for App/SDK to work*/
59
+ private fun checkAndPromptPermissions () {
60
+ for (permission in REQUIREDPERMISSIONS ) {
61
+ if (ContextCompat .checkSelfPermission(
62
+ this ,
63
+ permission
64
+ ) != PackageManager .PERMISSION_GRANTED
65
+ ) {
66
+ ActivityCompat .requestPermissions(
67
+ this ,
68
+ REQUIREDPERMISSIONS ,
69
+ REQUEST_CODE_PERMISSIONS
70
+ )
71
+ }
72
+ }
73
+ }
74
+
75
+ override fun onRequestPermissionsResult (
76
+ requestCode : Int ,
77
+ permissions : Array <String >,
78
+ grantResults : IntArray
79
+ ) {
80
+ super .onRequestPermissionsResult(requestCode, permissions, grantResults)
81
+ if (requestCode == REQUEST_CODE_PERMISSIONS ) {
82
+ for (permission in REQUIREDPERMISSIONS ) {
83
+ if (ContextCompat .checkSelfPermission(
84
+ this ,
85
+ permission
86
+ ) != PackageManager .PERMISSION_GRANTED
87
+ ) {
88
+ Toast .makeText(this , " Permissions not granted by the user." , Toast .LENGTH_SHORT )
89
+ .show()
90
+ finish()
91
+ }
92
+ // Got permission
93
+ }
94
+ }
95
+ }
96
+
97
+ /* DEV_HELP : SetUp SDKConfig - Refer tech. Doc. for further info.*/
98
+ private fun setConfig () {
99
+ imageHelper!! .SetPageLayout (4 ) // A1-A7(1-7),PHOTO,CUSTOM,ID(8,9,10)
100
+ imageHelper!! .SetImageQuality (1 ) // 0,1,2 - Photo_Quality, Document_Quality, Compressed_Document
101
+ imageHelper!! .SetDPI (200 ) // int dpi_val = 100, 150, 200, 300, 500, 600;
102
+
103
+ // can set output file path
104
+ CameraSupport .CamConfigClass .OutputPath = buildStoragePath()
105
+ }
106
+
107
+ /* DEV_HELP : BuildStoragePath*/
108
+ private fun buildStoragePath (): String {
109
+ val c = ContextWrapper (this )
110
+ return c.getExternalFilesDir(" .GoNoGoImages" )!! .absolutePath
111
+ }
112
+
113
+ /* DEV_HELP : handleCaptureActivityResult definition*/
114
+ private fun handleCaptureActivityResult (result : ActivityResult ) {
115
+ run {
116
+ val resultCode = result.resultCode
117
+ if (resultCode != RESULT_OK ) {
118
+ return
119
+ }
120
+ val data = result.data
121
+ var status: Boolean? = null
122
+ if (data != null ) {
123
+ status = data.extras!! [" STATUS" ] as Boolean?
124
+ }
125
+ val description = data!! .extras!! [" DESCRIPTION" ] as String?
126
+ if (! status!! ) {
127
+ val imageCaptureLog = " Description : " + description +
128
+ " .Exception: " + CameraSupport .CamConfigClass .LastLogInfo
129
+ Log .d(" INFO" , imageCaptureLog)
130
+ Toast .makeText(this , imageCaptureLog, Toast .LENGTH_LONG ).show()
131
+ finishActivity(MainActivity .Companion .REQUEST_CODE_FILE_RETURN )
132
+ return
133
+ }
134
+ fileCollection = data.extras!! [" fileCollection" ] as ArrayList <String >?
135
+ if (fileCollection == null || fileCollection!! .isEmpty()) return
136
+ try {
137
+ showImages(fileCollection!! )
138
+ } catch (e: IOException ) {
139
+ e.printStackTrace()
140
+ }
141
+ finishActivity(REQUEST_CODE_FILE_RETURN )
142
+ }
143
+ }
144
+
145
+ /* DEV_HELP : showImages*/
146
+ @Throws(IOException ::class )
147
+ private fun showImages (FilesPath : ArrayList <String >) {
148
+ val fileCollectionLength = FilesPath .size
149
+ for (i in 0 until fileCollectionLength) {
150
+ val dir = FilesPath [i]
151
+ val imgFile = File (dir)
152
+ // notifyMediaStoreScanner(imgFile);
153
+ if (imgFile.exists()) {
154
+ val myBitmap = BitmapFactory .decodeFile(imgFile.absolutePath)
155
+ val myImage = findViewById<ImageView >(R .id.displayImageView)
156
+ myImage.setImageBitmap(myBitmap)
157
+ }
158
+ Toast .makeText(this , " SDK captured $fileCollectionLength images." , Toast .LENGTH_SHORT )
159
+ .show()
160
+ }
161
+ }
162
+
163
+ /* DEV_HELP : OpenCameraActivity*/
164
+ private fun openCameraActivity () {
165
+
166
+ /* DEV_HELP : Check basic permissions for camera if needed*/
167
+ // if (!MainActivity.this.allPermissionsGranted()) {
168
+ // Toast.makeText(MainActivity.this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();
169
+ /* DEV_HELP : TODO : handle invalid permission*/
170
+ // return;
171
+ // }
172
+ try {
173
+ /* DEV_HELP :redirecting to camera*/
174
+ val captureIntent = Intent (this , Class .forName(" com.extrieve.quickcapture.sdk.CameraHelper" ))
175
+ val photoURI = Uri .parse(CameraSupport .CamConfigClass .OutputPath )
176
+ grantUriPermission(
177
+ this .packageName, photoURI,
178
+ Intent .FLAG_GRANT_WRITE_URI_PERMISSION or Intent .FLAG_GRANT_READ_URI_PERMISSION
179
+ )
180
+ if (Build .VERSION .SDK_INT <= Build .VERSION_CODES .LOLLIPOP ) {
181
+ captureIntent.addFlags(Intent .FLAG_GRANT_WRITE_URI_PERMISSION )
182
+ }
183
+ captureActivityResultLauncher!! .launch(captureIntent)
184
+ } catch (ex: Exception ) {
185
+ /* DEV_HELP : TODO : handle invalid Exception*/
186
+ Toast .makeText(this , " Failed to open camera -" + ex.message, Toast .LENGTH_LONG ).show()
187
+ }
188
+ }
189
+
190
+ companion object {
191
+ private const val REQUEST_CODE_PERMISSIONS = 1001
192
+ private const val REQUEST_CODE_FILE_RETURN = 1004
193
+ }
194
+ }
0 commit comments