@regulaforensics/cordova-plugin-document-reader-api 6.9.1 → 7.1.0

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,259 +0,0 @@
1
- package cordova.plugin.documentreader;
2
-
3
- import android.content.Context;
4
- import android.graphics.Bitmap;
5
- import android.graphics.BitmapFactory;
6
- import android.graphics.Canvas;
7
- import android.graphics.Matrix;
8
- import android.graphics.drawable.BitmapDrawable;
9
- import android.graphics.drawable.Drawable;
10
- import android.util.Base64;
11
-
12
- import com.regula.documentreader.api.enums.BarcodeType;
13
- import com.regula.documentreader.api.internal.params.FaceMetaData;
14
- import com.regula.documentreader.api.results.Bounds;
15
-
16
- import org.json.JSONArray;
17
- import org.json.JSONException;
18
- import org.json.JSONObject;
19
-
20
- import java.io.ByteArrayOutputStream;
21
- import java.util.ArrayList;
22
- import java.util.HashMap;
23
- import java.util.Iterator;
24
- import java.util.List;
25
- import java.util.Map;
26
-
27
- class Helpers {
28
- interface JSONObjectGeneratorWithContext<T> {
29
- JSONObject generateJSONObject(T param, Context context) throws JSONException;
30
- }
31
-
32
- interface JSONObjectGenerator<T> {
33
- JSONObject generateJSONObject(T param) throws JSONException;
34
- }
35
-
36
- static <T> JSONArray generateList(List<T> list) {
37
- JSONArray result = new JSONArray();
38
- if (list == null) return result;
39
- for (T t : list)
40
- if (t != null)
41
- result.put(t);
42
-
43
- return result;
44
- }
45
-
46
- static <T> JSONArray generateList(List<T> list, JSONObjectGenerator<T> generator) throws JSONException {
47
- JSONArray result = new JSONArray();
48
- if (list == null) return result;
49
- for (T t : list)
50
- if (t != null)
51
- result.put(generator.generateJSONObject(t));
52
-
53
- return result;
54
- }
55
-
56
- static <T> JSONArray generateList(List<T> list, JSONObjectGeneratorWithContext<T> generator, Context context) throws JSONException {
57
- JSONArray result = new JSONArray();
58
- if (list == null) return result;
59
- for (T t : list)
60
- if (t != null)
61
- result.put(generator.generateJSONObject(t, context));
62
-
63
- return result;
64
- }
65
-
66
- static <T> JSONArray generateArray(T[] array) throws JSONException {
67
- JSONArray result = new JSONArray();
68
- if (array == null) return result;
69
- for (int i = 0; i < array.length; i++)
70
- result.put(i, array[i]);
71
-
72
- return result;
73
- }
74
-
75
- static <T> JSONArray generateArray(T[] array, JSONObjectGenerator<T> generator) throws JSONException {
76
- JSONArray result = new JSONArray();
77
- if (array == null) return result;
78
- for (int i = 0; i < array.length; i++)
79
- result.put(i, generator.generateJSONObject(array[i]));
80
-
81
- return result;
82
- }
83
-
84
- static <T, V> JSONObject generateMap(Map<T, V> map) throws JSONException {
85
- JSONObject result = new JSONObject();
86
- if (map == null) return result;
87
- for (Map.Entry<T, V> entry : map.entrySet())
88
- if (entry != null)
89
- result.put(entry.getKey().toString(), entry.getValue());
90
- return result;
91
- }
92
-
93
- static JSONArray generateIntArray(int[] array) throws JSONException {
94
- JSONArray result = new JSONArray();
95
- if (array == null) return result;
96
- for (int i = 0; i < array.length; i++)
97
- result.put(i, array[i]);
98
-
99
- return result;
100
- }
101
-
102
- static int[] intArrayFromJSON(JSONArray input) throws JSONException {
103
- int[] result = new int[input.length()];
104
- for (int i = 0; i < input.length(); i++)
105
- result[i] = input.getInt(i);
106
-
107
- return result;
108
- }
109
-
110
- static JSONArray generateByteArray(byte[] array) throws JSONException {
111
- JSONArray result = new JSONArray();
112
- if (array == null) return result;
113
- for (int i = 0; i < array.length; i++)
114
- result.put(i, array[i]);
115
-
116
- return result;
117
- }
118
-
119
- static JSONArray generateLongArray(long[] array) throws JSONException {
120
- JSONArray result = new JSONArray();
121
- if (array == null) return result;
122
- for (int i = 0; i < array.length; i++)
123
- result.put(i, array[i]);
124
-
125
- return result;
126
- }
127
-
128
- static Bitmap bitmapFromBase64(String base64) {
129
- byte[] decodedString = Base64.decode(base64, Base64.NO_WRAP);
130
- Bitmap result = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
131
- int sizeMultiplier = result.getByteCount() / 5000000;
132
- if (result.getByteCount() > 5000000)
133
- result = Bitmap.createScaledBitmap(result, result.getWidth() / (int) Math.sqrt(sizeMultiplier), result.getHeight() / (int) Math.sqrt(sizeMultiplier), false);
134
- return result;
135
- }
136
-
137
- static BitmapDrawable drawableFromBase64(String base64, Context context) {
138
- byte[] decodedByte = Base64.decode(base64, 0);
139
- Bitmap bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
140
- float density = context.getResources().getDisplayMetrics().density;
141
- int width = (int) (bitmap.getWidth() * density);
142
- int height = (int) (bitmap.getHeight() * density);
143
- return new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, width, height, false));
144
- }
145
-
146
- static Bitmap bitmapFromDrawable(Drawable drawable) {
147
- Bitmap bitmap;
148
-
149
- if (drawable instanceof BitmapDrawable) {
150
- BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
151
- if (bitmapDrawable.getBitmap() != null) {
152
- return bitmapDrawable.getBitmap();
153
- }
154
- }
155
-
156
- if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
157
- bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
158
- } else {
159
- bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
160
- }
161
-
162
- Canvas canvas = new Canvas(bitmap);
163
- drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
164
- drawable.draw(canvas);
165
- return bitmap;
166
- }
167
-
168
- static String bitmapToBase64String(Bitmap bitmap) {
169
- if (bitmap == null) return null;
170
-
171
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
172
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
173
- byte[] byteArray = byteArrayOutputStream.toByteArray();
174
-
175
- return Base64.encodeToString(byteArray, Base64.NO_WRAP);
176
- }
177
-
178
- static Matrix matrixFromFloatArray(float[] floats) {
179
- Matrix matrix = new Matrix();
180
- matrix.setValues(floats);
181
- return matrix;
182
- }
183
-
184
- static float[] floatArrayFromJson(JSONArray jsonArray) throws JSONException {
185
- float[] result = new float[jsonArray.length()];
186
- for (int i = 0; i < jsonArray.length(); i++)
187
- result[i] = (float) jsonArray.getDouble(i);
188
-
189
- return result;
190
- }
191
-
192
- static int[] intArrayFromJson(JSONArray jsonArray) throws JSONException {
193
- int[] result = new int[jsonArray.length()];
194
- for (int i = 0; i < jsonArray.length(); i++)
195
- result[i] = jsonArray.getInt(i);
196
-
197
- return result;
198
- }
199
-
200
- static String[] barcodeTypeArrayFromJson(JSONArray jsonArray) throws JSONException {
201
- String[] result = new String[jsonArray.length()];
202
- for (int i = 0; i < jsonArray.length(); i++)
203
- result[i] = BarcodeType.valueOf(jsonArray.getInt(i));
204
-
205
- return result;
206
- }
207
-
208
- static FaceMetaData[] faceMetaDataArrayFromJson(JSONArray jsonArray) throws JSONException {
209
- FaceMetaData[] result = new FaceMetaData[jsonArray.length()];
210
- for (int i = 0; i < jsonArray.length(); i++)
211
- result[i] = faceMetaDataFromJson(jsonArray.getJSONObject(i));
212
-
213
- return result;
214
- }
215
-
216
- static FaceMetaData faceMetaDataFromJson(JSONObject object) throws JSONException {
217
- FaceMetaData result = new FaceMetaData();
218
- result.ID = object.getInt("ID");
219
- result.rollAngle = object.getInt("rollAngle");
220
- result.bounds = boundsFromJson(object.getJSONObject("bounds"));
221
-
222
- return result;
223
- }
224
-
225
- static Bounds boundsFromJson(JSONObject object) throws JSONException {
226
- Bounds result = new Bounds();
227
- result.height = object.getInt("height");
228
- result.width = object.getInt("width");
229
- result.x = object.getInt("x");
230
- result.y = object.getInt("y");
231
-
232
- return result;
233
- }
234
-
235
- static List<String> stringListFromJson(JSONArray jsonArray) {
236
- List<String> result = new ArrayList<>();
237
- for (int i = 0; i < jsonArray.length(); i++)
238
- result.add(jsonArray.optString(i));
239
- return result;
240
- }
241
-
242
- static String[] stringArrayFromJson(JSONArray jsonArray) {
243
- String[] result = new String[jsonArray.length()];
244
- for (int i = 0; i < jsonArray.length(); i++)
245
- result[i] = jsonArray.optString(i);
246
- return result;
247
- }
248
-
249
- static Map<String, String> stringMapFromJson(JSONObject input) throws JSONException {
250
- Map<String, String> result = new HashMap<>();
251
- Iterator<String> keys = input.keys();
252
- while (keys.hasNext()) {
253
- String key = keys.next();
254
- String value = input.getString(key);
255
- result.put(key, value);
256
- }
257
- return result;
258
- }
259
- }