@regulaforensics/cordova-plugin-document-reader-api 8.1.122-nightly → 8.1.131-nightly

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,35 +1,19 @@
1
- //
2
- // Utils.kt
3
- // DocumentReader
4
- //
5
- // Created by Pavel Masiuk on 21.09.2023.
6
- // Copyright © 2023 Regula. All rights reserved.
7
- //
1
+ @file:SuppressLint("UseKtx")
8
2
 
9
- package cordova.plugin.documentreader
3
+ package com.regula.plugin.documentreader
10
4
 
11
- import android.content.Context
5
+ import android.annotation.SuppressLint
12
6
  import android.graphics.Bitmap
13
7
  import android.graphics.BitmapFactory
14
8
  import android.graphics.Canvas
15
- import android.graphics.Matrix
16
- import android.graphics.Paint
17
- import android.graphics.Typeface
18
9
  import android.graphics.drawable.BitmapDrawable
19
10
  import android.graphics.drawable.Drawable
20
11
  import android.util.Base64
21
- import com.regula.documentreader.api.enums.CustomizationFont
22
- import com.regula.documentreader.api.params.ParamsCustomization
23
12
  import org.json.JSONArray
24
13
  import org.json.JSONObject
25
14
  import java.io.ByteArrayOutputStream
26
15
  import kotlin.math.sqrt
27
16
 
28
- fun Any?.toSendable(): Any? = this?.let {
29
- if (this is JSONObject || this is JSONArray) this.toString()
30
- else this
31
- }
32
-
33
17
  fun List<*>.toJson(): JSONArray {
34
18
  val result = JSONArray()
35
19
  for (i in indices)
@@ -55,83 +39,73 @@ fun Map<*, *>.toJson(): JSONObject {
55
39
  return result
56
40
  }
57
41
 
58
- fun <T> generateList(list: List<T>?) = list?.let {
59
- val result = JSONArray()
60
- for (t in list) if (t != null) result.put(t)
61
- result
42
+ fun Any?.toSendable(): Any? = this?.let {
43
+ if (it is JSONObject || it is JSONArray) it.toString()
44
+ else it
62
45
  }
63
46
 
64
- fun <T> generateList(list: List<T>, toJson: (T?) -> JSONObject?): JSONArray {
47
+ fun <T> List<T>?.toJson(toJson: (T?) -> Any?) = this?.let {
65
48
  val result = JSONArray()
66
- for (t in list) if (t != null) result.put(toJson(t))
67
- return result
49
+ for (item in it) result.put(toJson(item))
50
+ result
68
51
  }
69
52
 
70
- fun <T> listFromJSON(input: JSONArray?, fromJson: (JSONObject?) -> T) = input?.let {
71
- val result: MutableList<T> = ArrayList()
72
- for (i in 0 until input.length()) {
73
- val item = input.getJSONObject(i)
74
- result.add(fromJson(item))
75
- }
53
+ fun <T> List<T>?.toJsonNullable(toJson: (T?) -> Any?) = this?.let {
54
+ val result = JSONArray()
55
+ for (item in it) result.put(toJson(item))
76
56
  result
77
57
  }
78
58
 
79
- @Suppress("UNCHECKED_CAST")
80
- fun <T> listFromJSON(input: JSONArray): List<T> {
59
+ fun <T> JSONArray?.toList(fromJson: (JSONObject) -> T) = this?.let {
81
60
  val result: MutableList<T> = ArrayList()
82
- for (i in 0 until input.length()) result.add(input.opt(i) as T)
83
- return result
61
+ for (i in 0 until it.length()) result.add(fromJson(it.getJSONObject(i)))
62
+ result
84
63
  }
85
64
 
86
- fun <T> arrayFromJSON(input: JSONArray?, fromJson: (JSONObject?) -> T, result: Array<T>) = input?.let {
87
- for (i in 0 until input.length())
88
- result[i] = fromJson(input.getJSONObject(i))
65
+ fun <T> JSONArray.toList() = this.let {
66
+ val result = mutableListOf<T>()
67
+ @Suppress("UNCHECKED_CAST")
68
+ for (i in 0 until length()) result.add(get(i) as T)
89
69
  result
90
70
  }
91
71
 
92
- fun <T> generateList(list: List<T>, toJson: (T?, Context?) -> JSONObject?, context: Context?): JSONArray {
93
- val result = JSONArray()
94
- for (t in list) if (t != null) result.put(toJson(t, context))
95
- return result
72
+ inline fun <reified T> JSONArray?.toArray() = this?.let {
73
+ val result = arrayOfNulls<T>(length())
74
+ for (i in 0 until length()) result[i] = get(i) as T
75
+ result
96
76
  }
97
77
 
98
- fun <T> generateArray(array: Array<T>?) = array?.let {
99
- val result = JSONArray()
100
- for (i in array.indices) result.put(i, array[i])
78
+ inline fun <reified T> JSONArray?.toArray(fromJson: (JSONObject?) -> T) = this?.let {
79
+ val result = arrayOfNulls<T>(length())
80
+ for (i in 0 until length()) result[i] = fromJson(getJSONObject(i))
101
81
  result
102
82
  }
103
83
 
104
- fun <T> generateArray(array: Array<T>?, toJson: (T?) -> JSONObject?) = array?.let {
84
+ fun <T> Array<T>?.toJson() = this?.let {
105
85
  val result = JSONArray()
106
- for (i in array.indices) result.put(i, toJson(array[i]))
86
+ for (i in it.indices) result.put(i, it[i])
107
87
  result
108
88
  }
109
89
 
110
- fun generateLongArray(array: LongArray?) = array?.let {
90
+ fun <T> Array<T>?.toJson(toJson: (T?) -> JSONObject?) = this?.let {
111
91
  val result = JSONArray()
112
- for (i in array.indices) result.put(i, array[i])
92
+ for (i in indices) result.put(i, toJson(this[i]))
113
93
  result
114
94
  }
115
95
 
116
- fun stringListFromJson(jsonArray: JSONArray): List<String> {
117
- val result: MutableList<String> = ArrayList()
118
- for (i in 0 until jsonArray.length()) result.add(jsonArray.optString(i))
119
- return result
120
- }
121
-
122
- fun stringArrayFromJson(jsonArray: JSONArray): Array<String?> {
123
- val result = arrayOfNulls<String>(jsonArray.length())
124
- for (i in 0 until jsonArray.length()) result[i] = jsonArray.optString(i)
125
- return result
96
+ fun Any?.toIntArray() = (this as JSONArray?)?.let {
97
+ val result = IntArray(it.length())
98
+ for (i in 0 until it.length()) result[i] = it.getInt(i)
99
+ result
126
100
  }
127
101
 
128
- fun paintCapToInt(cap: Paint.Cap) = when (cap) {
129
- Paint.Cap.BUTT -> 0
130
- Paint.Cap.ROUND -> 1
131
- Paint.Cap.SQUARE -> 2
102
+ fun IntArray?.toJson() = this?.let {
103
+ val result = JSONArray()
104
+ for (i in it.indices) result.put(i, it[i])
105
+ result
132
106
  }
133
107
 
134
- fun JSONObject.forEach(action: (String, Any?) -> Unit) {
108
+ fun JSONObject.forEach(action: (String, Any) -> Unit) {
135
109
  val keys: Iterator<String> = keys()
136
110
  while (keys.hasNext()) {
137
111
  val key = keys.next()
@@ -139,130 +113,69 @@ fun JSONObject.forEach(action: (String, Any?) -> Unit) {
139
113
  }
140
114
  }
141
115
 
142
- fun Map<String, Any?>.toJsonObject(): JSONObject {
143
- val result = JSONObject()
144
- forEach { (key, value) -> result.put(key, value) }
145
- return result
116
+ fun JSONObject.getJSONObjectOrNull(name: String): JSONObject? {
117
+ if (has(name) && get(name).toString() != "null") return getJSONObject(name)
118
+ return null
146
119
  }
147
120
 
148
- fun stringMapFromJson(input: JSONObject): Map<String, String> {
149
- val result: MutableMap<String, String> = HashMap()
150
- input.forEach { key, value -> result[key] = value as String }
151
- return result
121
+ fun JSONObject.getIntOrNull(name: String): Int? {
122
+ if (has(name) && get(name).toString() != "null") return getInt(name)
123
+ return null
152
124
  }
153
125
 
154
- fun generateStringMap(input: Map<String, String?>?) = input?.let {
155
- val result = JSONObject()
156
- for ((key, value) in input) result.put(key, value)
157
- result
126
+ fun JSONObject.getDoubleOrNull(name: String): Double? {
127
+ if (has(name) && get(name).toString() != "null") return getDouble(name)
128
+ return null
158
129
  }
159
130
 
160
- fun Any?.toInt() = when (this) {
161
- is Double -> toInt()
162
- is Long -> toInt()
163
- else -> this as Int
131
+ fun JSONObject.getBooleanOrNull(name: String): Boolean? {
132
+ if (has(name) && get(name).toString() != "null") return getBoolean(name)
133
+ return null
164
134
  }
165
135
 
166
- fun Any?.toLong() = when (this) {
167
- is Double -> toLong()
168
- is Int -> toLong()
169
- else -> this as Long
170
- }
171
-
172
- fun Any?.toDouble() = when (this) {
173
- is Int -> toDouble()
174
- is Long -> toDouble()
175
- else -> this as Double
176
- }
177
-
178
- fun Any?.toColor() = "#" + toLong().toString(16)
179
-
180
- fun Any?.toFloat() = when (this) {
181
- is Int -> toFloat()
182
- is Double -> toFloat()
183
- else -> this as Float
184
- }
185
-
186
- fun Any?.toMatrix() = this?.let {
187
- val matrix = Matrix()
188
- val result = FloatArray((this as JSONArray).length())
189
- for (i in 0 until length()) result[i] = getDouble(i).toFloat()
190
- matrix.setValues(result)
191
- matrix
192
- }
193
-
194
- fun Any?.toIntArray() = (this as JSONArray?)?.let {
195
- val result = IntArray(it.length())
196
- for (i in 0 until it.length()) result[i] = it.getInt(i)
197
- result
198
- }
199
-
200
- fun IntArray?.generate() = this?.let {
201
- val result = JSONArray()
202
- for (i in indices) result.put(i, this[i])
203
- result
204
- }
205
-
206
- fun String?.toLong() = this?.let {
207
- if (this[0] == '#') this.substring(1).toLong(16)
208
- else this.toLong(16)
209
- }
210
-
211
- fun Matrix?.generate() = this?.let {
212
- val floats = FloatArray(9)
213
- getValues(floats)
214
- val result = JSONArray()
215
- for (f in floats) result.put(java.lang.Float.valueOf(f))
216
- result
217
- }
218
-
219
- fun CustomizationFont.generate(fonts: Map<CustomizationFont, Typeface>, sizes: Map<CustomizationFont, Int>) = generateTypeface(fonts[this], sizes[this])
220
-
221
- fun CustomizationFont.setFont(editor: ParamsCustomization.CustomizationEditor, value: Any?) {
222
- val font = typefaceFromJSON(value as JSONObject)
223
- editor.setFont(this, font.first)
224
- font.second?.let { editor.setFontSize(this, it) }
136
+ fun JSONObject.getStringOrNull(name: String): String? {
137
+ if (has(name) && get(name).toString() != "null") return getString(name)
138
+ return null
225
139
  }
226
140
 
227
141
  internal object Convert {
228
- fun byteArrayFromBase64(base64: String?): ByteArray? {
229
- var str = base64 ?: return null
142
+ fun String?.toByteArray(): ByteArray? {
143
+ var str = this ?: return null
230
144
  if (str.startsWith("data")) str = str.substring(str.indexOf(",") + 1)
231
145
  return Base64.decode(str, Base64.NO_WRAP)
232
146
  }
233
147
 
234
- fun generateByteArray(array: ByteArray?) = array?.let { Base64.encodeToString(it, Base64.NO_WRAP) }
148
+ fun ByteArray?.toBase64() = this?.let { Base64.encodeToString(it, Base64.NO_WRAP) }
149
+
150
+ fun Bitmap?.toBase64() = this?.let {
151
+ val byteArrayOutputStream = ByteArrayOutputStream()
152
+ it.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
153
+ byteArrayOutputStream.toByteArray().toBase64()
154
+ }
235
155
 
236
- fun bitmapFromBase64(base64: String?) = base64?.let {
237
- val decodedString = byteArrayFromBase64(base64)
238
- var result = BitmapFactory.decodeByteArray(decodedString, 0, decodedString!!.size)
156
+ fun String?.toBitmap() = this?.let {
157
+ val decodedString = toByteArray()!!
158
+ var result = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
239
159
  val sizeMultiplier = result.byteCount / 5000000
240
160
  if (result.byteCount > 5000000) result = Bitmap.createScaledBitmap(result, result.width / sqrt(sizeMultiplier.toDouble()).toInt(), result.height / sqrt(sizeMultiplier.toDouble()).toInt(), false)
241
161
  result
242
162
  }
243
163
 
244
- fun bitmapToBase64(bitmap: Bitmap?) = bitmap?.let {
245
- val byteArrayOutputStream = ByteArrayOutputStream()
246
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
247
- val byteArray = byteArrayOutputStream.toByteArray()
248
- generateByteArray(byteArray)
249
- }
250
-
251
- fun Any?.toDrawable(context: Context) = (this as String?)?.let {
252
- val decodedByte = byteArrayFromBase64(it)
253
- val bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte!!.size)
164
+ fun Any?.toDrawable() = (this as String?)?.let {
165
+ val decodedByte = it.toByteArray()!!
166
+ val bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.size)
254
167
  val density = context.resources.displayMetrics.density
255
168
  val width = (bitmap.width * density).toInt()
256
169
  val height = (bitmap.height * density).toInt()
257
170
  BitmapDrawable(context.resources, Bitmap.createScaledBitmap(bitmap, width, height, false))
258
171
  }
259
172
 
260
- fun Drawable?.toString() = this?.let {
261
- if (this is BitmapDrawable) if (bitmap != null) return bitmapToBase64(bitmap)
173
+ fun Drawable?.toBase64() = this?.let {
174
+ if (this is BitmapDrawable) if (bitmap != null) return bitmap.toBase64()
262
175
  val bitmap: Bitmap = if (intrinsicWidth <= 0 || intrinsicHeight <= 0) Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888) else Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888)
263
176
  val canvas = Canvas(bitmap)
264
177
  setBounds(0, 0, canvas.width, canvas.height)
265
178
  draw(canvas)
266
- bitmapToBase64(bitmap)
179
+ bitmap.toBase64()
267
180
  }
268
- }
181
+ }
@@ -14,7 +14,7 @@ repositories {
14
14
 
15
15
  dependencies {
16
16
  //noinspection GradleDependency
17
- implementation ('com.regula.documentreader:api:8.1.11441'){
17
+ implementation ('com.regula.documentreader:api:8.1.11467'){
18
18
  transitive = true
19
19
  }
20
20
  }
@@ -1,11 +1,3 @@
1
- //
2
- // RGLWConfig.h
3
- // DocumentReader
4
- //
5
- // Created by Pavel Masiuk on 21.09.2023.
6
- // Copyright © 2023 Regula. All rights reserved.
7
- //
8
-
9
1
  #ifndef RGLWConfig_h
10
2
  #define RGLWConfig_h
11
3