12
12
import androidx .annotation .Nullable ;
13
13
import androidx .appcompat .app .AlertDialog ;
14
14
import androidx .constraintlayout .widget .Group ;
15
+ import androidx .core .util .Pair ;
15
16
16
17
import com .appliedrec .uielements .RxVerIDActivity ;
17
18
import com .appliedrec .verid .core .Bearing ;
@@ -101,9 +102,16 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
101
102
102
103
private void showCardFromResult (BlinkIdCombinedRecognizer .Result result ) {
103
104
byte [] frontImage = result .getEncodedFrontFullDocumentImage ();
105
+ byte [] faceImage = result .getEncodedFaceImage ();
104
106
documentData = new DocumentData (result );
105
107
addDisposable (Single .create (single -> {
106
108
try {
109
+ if (frontImage .length == 0 ) {
110
+ throw new Exception (getString (R .string .failed_to_collect_card_image ));
111
+ }
112
+ if (faceImage .length == 0 ) {
113
+ throw new Exception (getString (R .string .failed_to_detect_face_in_card ));
114
+ }
107
115
File imageFile = new File (getFilesDir (), "cardFront.jpg" );
108
116
FileOutputStream outputStream = new FileOutputStream (imageFile );
109
117
ByteArrayInputStream inputStream = new ByteArrayInputStream (frontImage );
@@ -114,18 +122,29 @@ private void showCardFromResult(BlinkIdCombinedRecognizer.Result result) {
114
122
}
115
123
outputStream .close ();
116
124
inputStream .close ();
117
- single .onSuccess (Uri .fromFile (imageFile ));
125
+ Uri [] uris = new Uri [2 ];
126
+ uris [0 ] = Uri .fromFile (imageFile );
127
+ imageFile = File .createTempFile ("verid_card_face_" , ".jpg" );
128
+ outputStream = new FileOutputStream (imageFile );
129
+ inputStream = new ByteArrayInputStream (faceImage );
130
+ while ((read = inputStream .read (buffer , 0 , buffer .length )) > 0 ) {
131
+ outputStream .write (buffer , 0 , read );
132
+ }
133
+ outputStream .close ();
134
+ inputStream .close ();
135
+ uris [1 ] = Uri .fromFile (imageFile );
136
+ single .onSuccess (uris );
118
137
} catch (Exception e ) {
119
- single .onError (e );
138
+ single .onError (new Exception ( getString ( R . string . failed_to_save_image_of_card ), e ) );
120
139
}
121
- }).cast (Uri .class )
122
- .flatMapObservable (imageUri -> getRxVerID ().detectRecognizableFacesInImage (imageUri , 1 ).map (face -> new DetectedFace (face , Bearing .STRAIGHT , imageUri )))
140
+ }).cast (Uri [] .class )
141
+ .flatMapObservable (imageUri -> getRxVerID ().detectRecognizableFacesInImage (imageUri [ 1 ] , 1 ).switchIfEmpty ( getRxVerID (). detectRecognizableFacesInImage ( imageUri [ 0 ], 1 )). map (face -> new Pair <>( new DetectedFace (face , Bearing .STRAIGHT , imageUri [ 1 ]), imageUri [ 0 ] )))
123
142
.singleOrError ()
124
143
.subscribeOn (Schedulers .io ())
125
144
.observeOn (AndroidSchedulers .mainThread ())
126
145
.subscribe (
127
- this :: showCard ,
128
- this :: showError
146
+ pair -> showCard ( pair . first , pair . second ) ,
147
+ error -> showError ( new Exception ( getString ( R . string . failed_to_detect_face_in_card ), error ))
129
148
));
130
149
}
131
150
@@ -167,6 +186,8 @@ private void scanIDCard() {
167
186
BlinkIdCombinedRecognizer recognizer = new BlinkIdCombinedRecognizer ();
168
187
recognizer .setReturnFullDocumentImage (true );
169
188
recognizer .setEncodeFullDocumentImage (true );
189
+ recognizer .setReturnFaceImage (true );
190
+ recognizer .setEncodeFaceImage (true );
170
191
recognizerBundle = new RecognizerBundle (recognizer );
171
192
BlinkIdUISettings uiSettings = new BlinkIdUISettings (recognizerBundle );
172
193
uiSettings .enableHighResSuccessFrameCapture (true );
@@ -176,18 +197,23 @@ private void scanIDCard() {
176
197
private void showError (Throwable error ) {
177
198
progressBar .setVisibility (View .GONE );
178
199
mainUIGroup .setVisibility (View .VISIBLE );
200
+ String message = error .getLocalizedMessage ();
201
+ if (error .getCause () != null && error .getCause ().getLocalizedMessage () != null && !error .getCause ().getLocalizedMessage ().isEmpty ()) {
202
+ message += ": " +error .getCause ().getLocalizedMessage ();
203
+ }
179
204
new AlertDialog .Builder (this )
180
205
.setTitle (R .string .error )
181
- .setMessage (error . getLocalizedMessage () )
206
+ .setMessage (message )
182
207
.setPositiveButton (android .R .string .ok , null )
183
208
.create ()
184
209
.show ();
185
210
}
186
211
187
- private void showCard (DetectedFace face ) {
212
+ private void showCard (DetectedFace face , Uri cardImageUri ) {
188
213
progressBar .setVisibility (View .GONE );
189
214
mainUIGroup .setVisibility (View .VISIBLE );
190
215
Intent intent = new Intent (this , IDCardActivity .class );
216
+ intent .putExtra (IDCardActivity .EXTRA_CARD_IMAGE_URI , cardImageUri );
191
217
intent .putExtra (IDCardActivity .EXTRA_DETECTED_FACE , face );
192
218
if (documentData != null ) {
193
219
intent .putExtra (IDCardActivity .EXTRA_DOCUMENT_DATA , documentData );
0 commit comments