Skip to content

Commit 389ec07

Browse files
committed
Run example on regula document reader test saas
1 parent dbab013 commit 389ec07

File tree

6 files changed

+138
-74
lines changed

6 files changed

+138
-74
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
build
44
gradle.properties
55
document-image.jpg
6-
portrait.jpg
6+
portrait.jpg
7+
/example/src/main/resources/regula.license

client/src/main/java/com/regula/documentreader/webclient/api/DocumentReaderApi.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.regula.documentreader.webclient.ApiException;
55
import com.regula.documentreader.webclient.model.ProcessRequest;
66
import com.regula.documentreader.webclient.model.ext.RecognitionResponse;
7+
import okio.ByteString;
78

89
public class DocumentReaderApi extends DefaultApi {
910

@@ -59,4 +60,8 @@ public String getLicense() {
5960
public void setLicense(String license) {
6061
this.license = license;
6162
}
63+
64+
public void setLicense(byte[] license) {
65+
this.license = ByteString.of(license).base64();
66+
}
6267
}
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
package com.regula.documentreader.webclient.model.ext;
22

33
import java.util.ArrayList;
4-
import java.util.Collections;
54
import java.util.List;
6-
import javax.annotation.Nonnull;
75
import javax.annotation.Nullable;
86

97
public class Images extends com.regula.documentreader.webclient.model.Images {
108

11-
@Nullable
12-
public ImagesField getField(int fieldType) {
13-
for (ImagesField field : getFieldList()) {
14-
if (field.getFieldType() == fieldType) {
15-
return field;
16-
}
17-
}
18-
return null;
9+
@Nullable
10+
public ImagesField getField(int fieldType) {
11+
for (ImagesField field : getFieldList()) {
12+
if (field.getFieldType() == fieldType) {
13+
return field;
14+
}
1915
}
16+
return null;
17+
}
2018

21-
public List<ImagesField> getFields(int fieldType) {
22-
List<ImagesField> fields = new ArrayList<>();
23-
for (ImagesField field : getFieldList()) {
24-
if (field.getFieldType() == fieldType) {
25-
fields.add(field);
26-
}
27-
}
28-
return fields;
19+
public List<ImagesField> getFields(int fieldType) {
20+
List<ImagesField> fields = new ArrayList<>();
21+
for (ImagesField field : getFieldList()) {
22+
if (field.getFieldType() == fieldType) {
23+
fields.add(field);
24+
}
2925
}
26+
return fields;
27+
}
3028
}

client/src/main/java/com/regula/documentreader/webclient/model/ext/RecognitionResponse.java

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,69 @@
22

33
import com.regula.documentreader.webclient.model.ImagesResult;
44
import com.regula.documentreader.webclient.model.ProcessResponse;
5-
import com.regula.documentreader.webclient.model.RawImageResult;
65
import com.regula.documentreader.webclient.model.Result;
76
import com.regula.documentreader.webclient.model.ResultItem;
87
import com.regula.documentreader.webclient.model.Status;
98
import com.regula.documentreader.webclient.model.StatusResult;
109
import com.regula.documentreader.webclient.model.TextResult;
11-
1210
import java.util.ArrayList;
13-
import java.util.Collections;
1411
import java.util.List;
1512
import javax.annotation.Nullable;
1613

1714
public class RecognitionResponse {
18-
private final ProcessResponse originalResponse;
19-
20-
public RecognitionResponse(ProcessResponse originalResponse) {
21-
this.originalResponse = originalResponse;
22-
}
15+
private final ProcessResponse originalResponse;
2316

24-
public ProcessResponse getOriginalResponse() {
25-
return originalResponse;
26-
}
17+
public RecognitionResponse(ProcessResponse originalResponse) {
18+
this.originalResponse = originalResponse;
19+
}
2720

21+
public ProcessResponse getOriginalResponse() {
22+
return originalResponse;
23+
}
2824

29-
@Nullable
30-
public Status status() {
31-
StatusResult result = resultByType(Result.STATUS);
32-
if (result != null) {
33-
return result.getStatus();
34-
}
35-
return null;
25+
@Nullable
26+
public Status status() {
27+
StatusResult result = resultByType(Result.STATUS);
28+
if (result != null) {
29+
return result.getStatus();
3630
}
31+
return null;
32+
}
3733

38-
@Nullable
39-
public Text text() {
40-
TextResult result = resultByType(Result.TEXT);
41-
if (result != null) {
42-
return result.getText();
43-
}
44-
return null;
34+
@Nullable
35+
public Text text() {
36+
TextResult result = resultByType(Result.TEXT);
37+
if (result != null) {
38+
return result.getText();
4539
}
40+
return null;
41+
}
4642

47-
@Nullable
48-
public Images images() {
49-
ImagesResult result = resultByType(Result.IMAGES);
50-
if (result != null) {
51-
return result.getImages();
52-
}
53-
return null;
43+
@Nullable
44+
public Images images() {
45+
ImagesResult result = resultByType(Result.IMAGES);
46+
if (result != null) {
47+
return result.getImages();
5448
}
49+
return null;
50+
}
5551

56-
public <R> R resultByType(int type) {
57-
for (ResultItem item : originalResponse.getContainerList().getList()) {
58-
if (item.getResultType() == type) {
59-
return (R) item;
60-
}
61-
}
62-
return null;
52+
public <R> R resultByType(int type) {
53+
for (ResultItem item : originalResponse.getContainerList().getList()) {
54+
if (item.getResultType() == type) {
55+
return (R) item;
56+
}
6357
}
58+
return null;
59+
}
6460

65-
public <R> List<R> resultsByType(int type) {
66-
List<R> results = new ArrayList<>();
67-
for (ResultItem item : originalResponse.getContainerList().getList()) {
68-
if (item.getResultType() == type) {
69-
results.add((R) item);
70-
}
71-
}
72-
return results;
61+
public <R> List<R> resultsByType(int type) {
62+
List<R> results = new ArrayList<>();
63+
for (ResultItem item : originalResponse.getContainerList().getList()) {
64+
if (item.getResultType() == type) {
65+
results.add((R) item);
66+
}
7367
}
68+
return results;
69+
}
7470
}

example/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Regula Document Reader java client compatible with jvm and android
2+
3+
:bulb: Before you start: if you just want to play with an online demo, visit our [playground](https://api.regulaforensics.com).
4+
5+
Requirements:
6+
- installed java 11+
7+
8+
Verify Java version:
9+
```bash
10+
java --version
11+
> openjdk 14.0.1 2020-04-14
12+
```
13+
14+
Cloning example:
15+
```bash
16+
git clone https://github.com/regulaforensics/DocumentReader-web-java-client.git
17+
cd DocumentReader-web-java-client
18+
```
19+
20+
### Running with local Regula Document Reader web API installation
21+
22+
:warning: NOTE: for some systems `gradlew.bat` command should be used, instead of `./gradlew`.
23+
24+
Follow [the instructions](https://docs.regulaforensics.com/web/quick-start-guide) to run Regula Document Reader web API.
25+
Assuming you have successfully launched instance, use next line command to run example:
26+
```bash
27+
./gradlew :example:run
28+
29+
# If Regula Document Reader web API is running not on localhost, specify host via env variable:
30+
API_BASE_PATH="http://192.168.0.101:8080" ./gradlew :example:run
31+
```
32+
33+
### Running using Regula Document Reader web API test SaaS
34+
35+
:warning: NOTE: for some systems `gradlew.bat` command should be used, instead of `./gradlew`.
36+
37+
Get your [free trial here](https://mobile.regulaforensics.com/). You should obtain `regula.license` file.
38+
Copy it to [example resources](../example/src/main/resources) folder. You are ready for running!
39+
40+
Execute example:
41+
```bash
42+
API_BASE_PATH="https://test-api.regulaforensics.com" ./gradlew :example:run
43+
```
44+
45+
### Output
46+
This sample generates next text output:
47+
```text
48+
---------------------------------------------------------------------------
49+
Document Overall Status: not valid
50+
Document Number Visual: U0996738
51+
Document Number MRZ: U0996738
52+
Validity Of Document Number Visual: 1
53+
Validity Of Document Number MRZ: 1
54+
MRZ-Visual values comparison: 1
55+
---------------------------------------------------------------------------
56+
```
57+
Also, it creates [portrait](portrait.jpg) and [document image](document-image.jpg) pictures inside current folder.
58+
Edit [example](../example/src/main/java/com/regula/documentreader/webclient/example/Main.java) on your own , and re-run to see your results.

example/src/main/java/com/regula/documentreader/webclient/example/Main.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import com.regula.documentreader.webclient.model.ext.ProcessRequestImage;
1010
import com.regula.documentreader.webclient.model.ext.RecognitionResponse;
1111

12+
import javax.annotation.Nullable;
1213
import java.io.IOException;
1314

14-
import java.net.URISyntaxException;
1515
import java.nio.file.Files;
1616
import java.nio.file.Path;
1717
import java.util.List;
@@ -25,13 +25,14 @@ public class Main {
2525
public static final String TEST_LICENSE = "TEST_LICENSE";
2626

2727

28-
public static void main(String[] args) throws IOException, ApiException, URISyntaxException {
28+
public static void main(String[] args) throws IOException, ApiException {
2929

3030
var apiBaseUrl = System.getenv(API_BASE_PATH);
3131
if (apiBaseUrl == null) {
3232
apiBaseUrl = "http://localhost:8080";
3333
}
34-
var license = System.getenv(TEST_LICENSE); // optional, used here only for smoke test purpouses
34+
var licenseFromEnv = System.getenv(TEST_LICENSE); // optional, used here only for smoke test purposes
35+
var licenseFromFile = readFile("regula.license");
3536

3637

3738
byte[] imageBytes = readFile("australia_passport.jpg");
@@ -44,7 +45,8 @@ public static void main(String[] args) throws IOException, ApiException, URISynt
4445
RecognitionRequest request = new RecognitionRequest(requestParams, List.of(image));
4546

4647
var api = new DocumentReaderApi(apiBaseUrl);
47-
api.setLicense(license);
48+
if (licenseFromEnv != null) api.setLicense(licenseFromEnv);
49+
if (licenseFromFile != null) api.setLicense(licenseFromFile);
4850

4951
RecognitionResponse response = api.process(request);
5052

@@ -72,19 +74,23 @@ public static void main(String[] args) throws IOException, ApiException, URISynt
7274
var documentImage = response.images().getField(DOCUMENT_FRONT).getValue();
7375
var portraitField = response.images().getField(PORTRAIT);
7476
var portraitFromVisual = portraitField.getValue(Source.VISUAL);
75-
saveFile("portrait.jpg", portraitFromVisual);
7677
saveFile("document-image.jpg", documentImage);
78+
saveFile("portrait.jpg", portraitFromVisual);
7779

7880
// how to get low lvl individual results
7981
LexicalAnalysisResult lexResult = response.resultByType(Result.LEXICAL_ANALYSIS);
8082
}
8183

84+
@Nullable
8285
private static byte[] readFile(String filePath) throws IOException {
83-
var classLoader = Main.class.getClassLoader();
84-
return classLoader.getResourceAsStream(filePath).readAllBytes();
86+
var inputStream = Main.class.getClassLoader().getResourceAsStream(filePath);
87+
if (inputStream != null) {
88+
return inputStream.readAllBytes();
89+
}
90+
return null;
8591
}
8692

87-
private static void saveFile(String filePath, byte[] data) throws IOException, URISyntaxException {
93+
private static void saveFile(String filePath, byte[] data) throws IOException {
8894
Files.write(Path.of(filePath), data);
8995
}
9096
}

0 commit comments

Comments
 (0)