io.anyline.cordova.MrzActivity.java Source code

Java tutorial

Introduction

Here is the source code for io.anyline.cordova.MrzActivity.java

Source

/*
 * Anyline Cordova Plugin
 * MrzActivity.java
 *
 * Copyright (c) 2015 9yards GmbH
 *
 * Created by martin at 2015-07-21
 */
package io.anyline.cordova;

import java.io.IOException;
import java.io.File;
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.view.WindowManager;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;

import at.nineyards.anyline.models.AnylineImage;
import at.nineyards.anyline.modules.mrz.MrzScanView;
import at.nineyards.anyline.modules.mrz.MrzResultListener;
import at.nineyards.anyline.modules.mrz.Identification;
import at.nineyards.anyline.camera.CameraOpenListener;
import at.nineyards.anyline.camera.AnylineViewConfig;
import at.nineyards.anyline.models.AnylineImage;
import at.nineyards.anyline.util.TempFileUtil;

import io.anyline.cordova.Resources;
import io.anyline.cordova.AnylineBaseActivity;

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

    private MrzScanView mrzScanView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mrzScanView = new MrzScanView(this, null);
        try {
            JSONObject json = new JSONObject(configJson);
            mrzScanView.setConfig(new AnylineViewConfig(this, json));
        } catch (Exception e) {
            //JSONException or IllegalArgumentException is possible, return it to javascript
            finishWithError(Resources.getString(this, "error_invalid_json_data") + "\n" + e.getLocalizedMessage());
            return;
        }
        setContentView(mrzScanView);

        initAnyline();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mrzScanView.startScanning();
    }

    @Override
    protected void onPause() {
        super.onPause();

        mrzScanView.cancelScanning();
        mrzScanView.releaseCameraInBackground();
    }

    private void initAnyline() {
        mrzScanView.setCameraOpenListener(this);

        mrzScanView.initAnyline(licenseKey, new MrzResultListener() {

            @Override
            public void onResult(Identification mrzResult, AnylineImage anylineImage) {

                JSONObject jsonResult = mrzResult.toJSONObject();

                try {
                    File imageFile = TempFileUtil.createTempFileCheckCache(MrzActivity.this,
                            UUID.randomUUID().toString(), ".jpg");
                    anylineImage.save(imageFile, 90);
                    jsonResult.put("imagePath", imageFile.getAbsolutePath());

                } catch (IOException e) {
                    Log.e(TAG, "Image file could not be saved.", e);

                } catch (JSONException jsonException) {
                    //should not be possible
                    Log.e(TAG, "Error while putting image path to json.", jsonException);
                }

                if (mrzScanView.getConfig().isCancelOnResult()) {
                    ResultReporter.onResult(jsonResult, true);
                    setResult(AnylinePlugin.RESULT_OK);
                    finish();
                } else {
                    ResultReporter.onResult(jsonResult, false);
                }
            }
        });
        mrzScanView.getAnylineController().setWorkerThreadUncaughtExceptionHandler(this);
    }

}