Skip to content

Commit

Permalink
Added composite scanning for android
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Müller committed Aug 2, 2019
1 parent 9672f9b commit ef683c0
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import at.nineyards.anyline.AnylineDebugListener;
Expand Down Expand Up @@ -45,14 +46,18 @@
import io.anyline.plugin.meter.MeterScanViewPlugin;
import io.anyline.plugin.ocr.OcrScanResult;
import io.anyline.plugin.ocr.OcrScanViewPlugin;
import io.anyline.view.AbstractBaseScanViewPlugin;
import io.anyline.view.CutoutRect;
import io.anyline.view.ScanView;
import io.anyline.view.ScanViewPlugin;
import io.anyline.view.ScanViewPluginComposite;
import io.anyline.view.SerialScanViewComposite;
//import io.anyline.view.ScanViewPlugin;

public class Anyline4Activity extends AnylineBaseActivity {
private static final String TAG = Anyline4Activity.class.getSimpleName();

private ScanView anylineScanView;
private ScanViewPlugin scanViewPlugin;
private AbstractBaseScanViewPlugin scanViewPlugin;
private RadioGroup radioGroup;
private AnylineUIConfig anylineUIConfig;
private String cropAndTransformError;
Expand Down Expand Up @@ -127,7 +132,13 @@ private void initAnyline() {
JSONObject json = new JSONObject(configJson);
// this is used for the OCR Plugin, when languages has to be added
json = AnylinePluginHelper.setLanguages(json, getApplicationContext());
anylineScanView.setScanConfig(json, licenseKey);

if (json.has("serialViewPluginComposite")) {
anylineScanView.initComposite(json, licenseKey); // for composite
scanViewPlugin = anylineScanView.getScanViewPlugin();
} else {
anylineScanView.setScanConfig(json, licenseKey); // for non-composite
}
if (anylineScanView != null) {
scanViewPlugin = anylineScanView.getScanViewPlugin();
}
Expand All @@ -139,9 +150,113 @@ private void initAnyline() {
if (!(scanViewPlugin instanceof MeterScanViewPlugin)) {
setContentView(anylineScanView);
}
if (scanViewPlugin instanceof LicensePlateScanViewPlugin) {

if (scanViewPlugin instanceof SerialScanViewComposite) {
scanViewPlugin.addScanResultListener(new ScanResultListener() {
@Override
public void onResult(ScanResult result) {
// only triggered if all plugins reached a result
JSONObject jsonResult = new JSONObject();
//String sResult = "";


for (ScanResult subResult : (Collection<ScanResult>) result.getResult()) {
if (subResult instanceof LicensePlateScanResult) {
JSONObject jsonLPResult = new JSONObject();
try {
LicensePlateScanResult licensePlateResult = (LicensePlateScanResult) subResult;
jsonLPResult.put("country", licensePlateResult.getCountry());
jsonLPResult.put("licensePlate", licensePlateResult.getResult());
jsonLPResult = AnylinePluginHelper.jsonHelper(Anyline4Activity.this, licensePlateResult,
jsonLPResult);

jsonResult.put(subResult.getPluginId(), jsonLPResult);
} catch (JSONException e) {
e.printStackTrace();
}
} else if (subResult.getResult() instanceof Identification) {
JSONObject jsonIdResult = ((Identification) subResult.getResult()).toJSONObject();
try {
if (jsonIdResult.get("issuingCountryCode").equals("D")
&& jsonIdResult.get("documentType").equals("ID")) {
if (jsonIdResult.get("issuingCountryCode").equals("D")) {
jsonIdResult.put("address", jsonResult.get("address"));
} else {
jsonIdResult.remove("address");
}
}
jsonIdResult = AnylinePluginHelper.jsonHelper(Anyline4Activity.this, subResult,
jsonIdResult);

jsonResult.put(subResult.getPluginId(), jsonIdResult);
} catch (JSONException e) {
e.printStackTrace();
}
} else if (subResult.getResult() instanceof DrivingLicenseIdentification) {
JSONObject jsonIdResult = ((DrivingLicenseIdentification) subResult.getResult())
.toJSONObject();
try {
jsonIdResult = AnylinePluginHelper.jsonHelper(Anyline4Activity.this, subResult,
jsonIdResult);

jsonResult.put(subResult.getPluginId(), jsonIdResult);
} catch (JSONException e) {
e.printStackTrace();
}
} else if (subResult.getResult() instanceof GermanIdFrontIdentification) {
JSONObject jsonIdResult = ((GermanIdFrontIdentification) subResult.getResult()).toJSONObject();
try {
jsonIdResult = AnylinePluginHelper.jsonHelper(Anyline4Activity.this, subResult,
jsonIdResult);

jsonResult.put(subResult.getPluginId(), jsonIdResult);
} catch (JSONException e) {
e.printStackTrace();
}
} else if (subResult instanceof OcrScanResult) {
JSONObject jsonOcrResult = new JSONObject();
try {
jsonOcrResult.put("text", (((OcrScanResult) subResult).getResult()).trim());
jsonOcrResult = AnylinePluginHelper.jsonHelper(Anyline4Activity.this, subResult,
jsonOcrResult);
jsonResult.put(subResult.getPluginId(), jsonOcrResult);
} catch (JSONException e) {
e.printStackTrace();
}
} else if (subResult instanceof BarcodeScanResult) {
JSONObject jsonBcResult = new JSONObject();
try {
jsonBcResult.put("value", subResult.getResult());
jsonBcResult.put("format", ((BarcodeScanResult) subResult).getBarcodeFormat());
jsonBcResult = AnylinePluginHelper.jsonHelper(Anyline4Activity.this, subResult,
jsonBcResult);
jsonResult.put(subResult.getPluginId(), jsonBcResult);
} catch (JSONException e) {
e.printStackTrace();
}
} else if (subResult instanceof MeterScanResult) {
JSONObject jsonMeterResult = new JSONObject();
try {
jsonMeterResult = AnylinePluginHelper.setMeterScanMode(
((MeterScanResult) subResult).getScanMode(), jsonMeterResult);
jsonMeterResult.put("reading", subResult.getResult());
jsonMeterResult = AnylinePluginHelper.jsonHelper(Anyline4Activity.this, subResult,
jsonMeterResult);
jsonResult.put(subResult.getPluginId(), jsonMeterResult);

} catch (Exception e) {
Log.e(TAG, "EXCEPTION", e);
}
}
}

setResult(scanViewPlugin, jsonResult);
}
});
} else if (scanViewPlugin instanceof LicensePlateScanViewPlugin) {
if (json.has("reportingEnabled")) {
scanViewPlugin.setReportingEnabled(json.optBoolean("reportingEnabled", true));
//(IdScanViewPlugin) scanViewPlugin.setReportingEnabled(json.optBoolean("reportingEnabled", true));
(((IdScanViewPlugin) scanViewPlugin).getScanPlugin()).setReportingEnabled(json.optBoolean("reportingEnabled", true));
}
scanViewPlugin.addScanResultListener(new ScanResultListener<LicensePlateScanResult>() {
@Override
Expand All @@ -162,7 +277,7 @@ public void onResult(LicensePlateScanResult licensePlateResult) {

});
} else if (scanViewPlugin instanceof IdScanViewPlugin) {
if (((IdScanPlugin) scanViewPlugin.getScanPlugin()).getIdConfig() instanceof MrzConfig) {
if (((IdScanPlugin) ((IdScanViewPlugin) scanViewPlugin).getScanPlugin()).getIdConfig() instanceof MrzConfig) {

if (json.has("cropAndTransformErrorMessage")) {
setDebugListener();
Expand Down Expand Up @@ -200,7 +315,7 @@ public void onResult(ScanResult<ID> idScanResult) {
}

});
} else if (((IdScanPlugin) scanViewPlugin.getScanPlugin())
} else if (((IdScanPlugin) ((IdScanViewPlugin) scanViewPlugin).getScanPlugin())
.getIdConfig() instanceof DrivingLicenseConfig) {
scanViewPlugin.addScanResultListener(new ScanResultListener<ScanResult<ID>>() {
@Override
Expand All @@ -221,7 +336,7 @@ public void onResult(ScanResult<ID> idScanResult) {
}

});
} else if (((IdScanPlugin) scanViewPlugin.getScanPlugin()).getIdConfig() instanceof GermanIdFrontConfig) {
} else if (((IdScanPlugin) ((IdScanViewPlugin) scanViewPlugin).getScanPlugin()).getIdConfig() instanceof GermanIdFrontConfig) {
scanViewPlugin.addScanResultListener(new ScanResultListener<ScanResult<ID>>() {
@Override
public void onResult(ScanResult<ID> idScanResult) {
Expand All @@ -241,7 +356,8 @@ public void onResult(ScanResult<ID> idScanResult) {

} else if (scanViewPlugin instanceof OcrScanViewPlugin) {
if (json.has("reportingEnabled")) {
scanViewPlugin.setReportingEnabled(json.optBoolean("reportingEnabled", true));
//scanViewPlugin.setReportingEnabled(json.optBoolean("reportingEnabled", true));
(((OcrScanViewPlugin) scanViewPlugin).getScanPlugin()).setReportingEnabled(json.optBoolean("reportingEnabled", true));
}

scanViewPlugin.addScanResultListener(new ScanResultListener<OcrScanResult>() {
Expand Down Expand Up @@ -286,7 +402,9 @@ public void onResult(BarcodeScanResult barcodeScanResult) {
} else if (scanViewPlugin instanceof MeterScanViewPlugin) {

if (json.has("reportingEnabled")) {
scanViewPlugin.setReportingEnabled(json.optBoolean("reportingEnabled", true));
//scanViewPlugin.setReportingEnabled(json.optBoolean("reportingEnabled", true));
(((MeterScanViewPlugin) scanViewPlugin).getScanPlugin()).setReportingEnabled(json.optBoolean("reportingEnabled", true));

}
// create the radio button for the UI
createSegmentRadioButtonUI(json);
Expand Down Expand Up @@ -333,11 +451,14 @@ public void onCameraOpened(CameraController cameraController, int width, int hei
@Override
public void run() {
if (radioGroup != null) {
Rect rect = anylineScanView.getScanViewPlugin().getCutoutImageOnSurface();
//orig: Rect rect = anylineScanView.getScanViewPlugin().getCutoutImageOnSurface(); // =cutoutRect.rectOnVisibleView
Rect rect = ((MeterScanViewPlugin) scanViewPlugin).getCutoutRectOnVisibleView();

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) radioGroup.getLayoutParams();
lp.setMargins(rect.left + anylineUIConfig.getOffsetX(), rect.top + anylineUIConfig.getOffsetY(), 0,
0);
// lp.setMargins(50 + anylineUIConfig.getOffsetX(), anylineUIConfig.getOffsetY(), 0,
// 0);
orig: lp.setMargins(rect.left + anylineUIConfig.getOffsetX(), rect.top + anylineUIConfig.getOffsetY(), 0,
0);
radioGroup.setLayoutParams(lp);

radioGroup.setVisibility(View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@
import at.nineyards.anyline.camera.CameraController;
import at.nineyards.anyline.camera.CameraFeatures;
import at.nineyards.anyline.camera.CameraOpenListener;
import io.anyline.view.ScanViewPlugin;
import io.anyline.plugin.barcode.BarcodeScanViewPlugin;
import io.anyline.plugin.id.IdScanViewPlugin;
import io.anyline.plugin.licenseplate.LicensePlateScanViewPlugin;
import io.anyline.plugin.meter.MeterScanViewPlugin;
import io.anyline.plugin.ocr.OcrScanViewPlugin;
import io.anyline.view.AbstractBaseScanViewPlugin;
import io.anyline.view.SerialScanViewComposite;
//import io.anyline.view.ScanViewPlugin;

public abstract class AnylineBaseActivity extends Activity
implements CameraOpenListener, Thread.UncaughtExceptionHandler {
Expand Down Expand Up @@ -232,8 +239,24 @@ protected void setFocusConfig (JSONObject json, CameraConfig camConfig) throws J
}
}

protected void setResult(ScanViewPlugin scanViewPlugin, JSONObject jsonResult){
if(scanViewPlugin != null && scanViewPlugin.getScanViewPluginConfig().isCancelOnResult()){
protected void setResult(AbstractBaseScanViewPlugin scanViewPlugin, JSONObject jsonResult){
Boolean isCancelOnResult = true;
if (scanViewPlugin instanceof MeterScanViewPlugin) {
isCancelOnResult = ((MeterScanViewPlugin) scanViewPlugin).getScanViewPluginConfig().isCancelOnResult();
} else if (scanViewPlugin instanceof BarcodeScanViewPlugin) {
isCancelOnResult = ((BarcodeScanViewPlugin) scanViewPlugin).getScanViewPluginConfig().isCancelOnResult();
} else if (scanViewPlugin instanceof IdScanViewPlugin) {
isCancelOnResult = ((IdScanViewPlugin) scanViewPlugin).getScanViewPluginConfig().isCancelOnResult();
} else if (scanViewPlugin instanceof LicensePlateScanViewPlugin) {
isCancelOnResult = ((LicensePlateScanViewPlugin) scanViewPlugin).getScanViewPluginConfig().isCancelOnResult();
} else if (scanViewPlugin instanceof OcrScanViewPlugin) {
isCancelOnResult = ((OcrScanViewPlugin) scanViewPlugin).getScanViewPluginConfig().isCancelOnResult();
} else if (scanViewPlugin instanceof SerialScanViewComposite) {
isCancelOnResult = ((SerialScanViewComposite) scanViewPlugin).getScanViewPluginConfig().isCancelOnResult();
}

if(scanViewPlugin != null && isCancelOnResult){
// if(scanViewPlugin != null && scanViewPlugin.getScanViewPluginConfig().isCancelOnResult()){
ResultReporter.onResult(jsonResult, true);
setResult(AnylineSDKPlugin.RESULT_OK);
finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ private void scanAnyline4() {
} else {
returnError("No Plugin in config. Please check your configuration.");
}
} else if (options.has("serialViewPluginComposite")) {
scan(Anyline4Activity.class, null, REQUEST_ANYLINE_4);
} else {
returnError("No ViewPlugin in config. Please check your configuration.");
}
Expand Down

0 comments on commit ef683c0

Please sign in to comment.