Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package com.regula.documentreader.webclient.example;


import static com.regula.documentreader.webclient.model.TextFieldType.DOCUMENT_NUMBER;

import com.regula.documentreader.webclient.ApiException;
import com.regula.documentreader.webclient.api.DocumentReaderApi;
import com.regula.documentreader.webclient.model.CheckResult;
import com.regula.documentreader.webclient.model.LexicalAnalysisResult;
import com.regula.documentreader.webclient.model.Light;
import com.regula.documentreader.webclient.model.Result;
import com.regula.documentreader.webclient.model.Scenario;
import com.regula.documentreader.webclient.model.SecurityFeatureType;
import com.regula.documentreader.webclient.model.Source;
import com.regula.documentreader.webclient.model.TextField;
import com.regula.documentreader.webclient.model.ext.ProcessRequestImage;
import com.regula.documentreader.webclient.model.ext.RecognitionParams;
import com.regula.documentreader.webclient.model.ext.RecognitionRequest;
import com.regula.documentreader.webclient.model.ext.RecognitionResponse;

import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import static com.regula.documentreader.webclient.model.GraphicFieldType.DOCUMENT_FRONT;
import static com.regula.documentreader.webclient.model.GraphicFieldType.PORTRAIT;
import static com.regula.documentreader.webclient.model.TextFieldType.DOCUMENT_NUMBER;
import javax.annotation.Nullable;

public class Main {
public static final String API_BASE_PATH = "API_BASE_PATH";
public static final String TEST_LICENSE = "TEST_LICENSE";

public static void main(String[] args) throws IOException, ApiException {

var apiBaseUrl = System.getenv(API_BASE_PATH);
Expand All @@ -37,87 +37,114 @@ public static void main(String[] args) throws IOException, ApiException {
var licenseFromEnv = System.getenv(TEST_LICENSE); // optional, used here only for smoke test purposes
var licenseFromFile = readFile("regula.license");


byte[] whitePage0 = readFile("WHITE.jpg");
byte[] irPage0 = readFile("IR.jpg");
byte[] uvPage0 = readFile("UV.jpg");

var whitePageRequestImage = new ProcessRequestImage(whitePage0, Light.WHITE, 0);
var irPageRequestImage = new ProcessRequestImage(irPage0, Light.IR, 0);
var uvPageRequestImage = new ProcessRequestImage(uvPage0, Light.UV, 0);

var requestParams = new RecognitionParams()
.withScenario(Scenario.FULL_AUTH)
.withResultTypeOutput(
// actual results
Result.STATUS, Result.AUTHENTICITY, Result.TEXT, Result.IMAGES,
Result.DOCUMENT_TYPE, Result.DOCUMENT_TYPE_CANDIDATES, Result.IMAGE_QUALITY,
Result.DOCUMENT_POSITION,
// legacy results
Result.MRZ_TEXT, Result.VISUAL_TEXT, Result.BARCODE_TEXT, Result.RFID_TEXT,
Result.VISUAL_GRAPHICS, Result.BARCODE_GRAPHICS, Result.RFID_GRAPHICS,
Result.LEXICAL_ANALYSIS
);
.withScenario(Scenario.FULL_PROCESS)
// .withResultTypeOutput(
// // actual results
// Result.STATUS, Result.TEXT, Result.IMAGES,
// Result.DOCUMENT_TYPE, Result.DOCUMENT_TYPE_CANDIDATES, Result.IMAGE_QUALITY,
// Result.DOCUMENT_POSITION,
// // legacy results
// Result.MRZ_TEXT, Result.VISUAL_TEXT, Result.BARCODE_TEXT, Result.RFID_TEXT,
// Result.VISUAL_GRAPHICS, Result.BARCODE_GRAPHICS, Result.RFID_GRAPHICS,
// Result.LEXICAL_ANALYSIS
// )
.withAlreadyCropped(true);

RecognitionRequest request = new RecognitionRequest(
requestParams, List.of(whitePageRequestImage, irPageRequestImage, uvPageRequestImage)
requestParams, List.of(whitePageRequestImage)
);

var api = new DocumentReaderApi(apiBaseUrl);

if (licenseFromEnv != null) api.setLicense(licenseFromEnv);
if (licenseFromFile != null) api.setLicense(licenseFromFile);

var info = api.ping();
System.out.println("-----------------------------------------------------------------");
System.out.format("Web API version %s%n", info.getVersion());

RecognitionResponse response = api.process(request);

var requestJson = request.json();
var responseJson = response.json();

// to send raw request(ex encrypted one) with overriding processing params here use next api
// RecognitionResponse response = api.process(request, requestParams);

var status = response.status();
var docOverallStatus = status.getOverallStatus() == CheckResult.OK ? "valid" : "not valid";
var docOpticalTextStatus = status.getDetailsOptical().getText();

var docNumberField = response.text().getField(DOCUMENT_NUMBER);
var docNumberFieldByName = response.text().getField("Document Number");

var docNumberVisual = docNumberField.getValue(Source.VISUAL);
var docNumberMrz = docNumberField.getValue(Source.MRZ);
var docNumberVisualValidity = docNumberField.sourceValidity(Source.VISUAL);
var docNumberMrzValidity = docNumberField.sourceValidity(Source.MRZ);
var docNumberMrzVisualMatching = docNumberField.crossSourceComparison(Source.MRZ, Source.VISUAL);

var docAuthenticity = response.authenticity();

var docIRB900 = docAuthenticity.irB900Checks();
var docIRB900Blank = docIRB900.checksByElement(SecurityFeatureType.BLANK);

var docImagePattern = docAuthenticity.imagePatternChecks();
var docImagePatternBlank = docImagePattern.checksByElement(SecurityFeatureType.BLANK);

System.out.println("-----------------------------------------------------------------");
System.out.format(" Document Overall Status: %s%n", docOverallStatus);
System.out.format(" Document Number Visual: %s%n", docNumberVisual);
System.out.format(" Document Number MRZ: %s%n", docNumberMrz);
System.out.format("Validity Of Document Number Visual: %s%n", docNumberVisualValidity);
System.out.format(" Validity Of Document Number MRZ: %s%n", docNumberMrzValidity);
System.out.format(" MRZ-Visual values comparison: %s%n", docNumberMrzVisualMatching);
System.out.format("-----------------------------------------------------------------");

var documentImage = response.images().getField(DOCUMENT_FRONT).getValue();
var portraitField = response.images().getField(PORTRAIT);
var portraitFromVisual = portraitField.getValue(Source.VISUAL);
saveFile("document-image.jpg", documentImage);
saveFile("portrait.jpg", portraitFromVisual);

// how to get low lvl individual results
LexicalAnalysisResult lexResult = response.resultByType(Result.LEXICAL_ANALYSIS);
System.exit(0);
String finalApiBaseUrl = apiBaseUrl;

new Thread(() -> {
var api = new DocumentReaderApi(finalApiBaseUrl);

// Uncomment one of the lines below if you want to transfer the license with each request
// if (licenseFromEnv != null) api.setLicense(licenseFromEnv);
// if (licenseFromFile != null) api.setLicense(licenseFromFile);

var info = api.ping();
System.out.println();
System.out.format(
"-----------------------------------------------------------------" + "\n"
+ "Web API version: " + info.getVersion() + "\n"
+ "-----------------------------------------------------------------");
RecognitionResponse response = api.process(request);
var requestJson = request.json();
var responseJson = response.json();

// to send raw request(ex encrypted one) with overriding processing params here use next api
// RecognitionResponse response = api.process(request, requestParams);

var status = response.status();
var docOverallStatus = status.getOverallStatus() == CheckResult.OK ? "valid" : "not valid";
var docOpticalTextStatus = status.getDetailsOptical().getText();

var docType = response.documentType() == null ? "Unknown" : response.documentType().getDocumentName();

var docNumberField = response.text().getField(DOCUMENT_NUMBER);
var docNumberFieldByName = response.text().getField("Document Number");

var docNumberVisual = docNumberField.getValue(Source.VISUAL);
var docNumberMrz = docNumberField.getValue(Source.MRZ);
var docNumberVisualValidity = docNumberField.sourceValidity(Source.VISUAL);
var docNumberMrzValidity = docNumberField.sourceValidity(Source.MRZ);
var docNumberMrzVisualMatching = docNumberField.crossSourceComparison(Source.MRZ, Source.VISUAL);

// var docAuthenticity = response.authenticity();

// var docIRB900 = docAuthenticity.irB900Checks();
// var docIRB900Blank = docIRB900.checksByElement(SecurityFeatureType.BLANK);

// var docImagePattern = docAuthenticity.imagePatternChecks();
// var docImagePatternBlank = docImagePattern.checksByElement(SecurityFeatureType.BLANK);

System.out.format("-----------------------------------------------------------------" + "\n"
+ "Document Type: " + docType + "\n"
+ "Document Overall Status: " + docOverallStatus + "\n"
+ "Document Number Visual: " + docNumberVisual + "\n"
+ "Document Number MRZ: " + docNumberMrz + "\n"
+ "Validity Of Document Number Visual: " + docNumberVisualValidity + "\n"
+ "Validity Of Document Number MRZ: " + docNumberMrzValidity + "\n"
+ "MRZ-Visual values comparison: " + docNumberMrzVisualMatching + "\n"
+ "-----------------------------------------------------------------");

if (response.text() != null) {
for (TextField field : response.text().getFieldList()) {
System.out.format(
"Field: " + " "
+ field.getFieldName() + " "
+ "Value: " + " "
+ field.getValueList().get(0).getValue() + " "
+ "Source: " + field.getValueList().get(0).getSource()
+ "\n");
}
System.out.format("---------------------------------------------------------------");
} else {
System.out.format(" NO TEXT DATA");
System.out.format("---------------------------------------------------------------");
}

// var documentImage = response.images().getField(DOCUMENT_FRONT).getValue();
// var portraitField = response.images().getField(PORTRAIT);
// var portraitFromVisual = portraitField.getValue(Source.VISUAL);
// saveFile("document-image.jpg", documentImage);
// saveFile("portrait.jpg", portraitFromVisual);

// how to get low lvl individual results
LexicalAnalysisResult lexResult = response.resultByType(Result.LEXICAL_ANALYSIS);

}).start();


// System.exit(0);
}

@Nullable
Expand Down
Loading