Creating your own OCR Data Capture plugin

You can create your own OCR Data Capture plugin.

Conditions:

  • The new OCR Data Capture class must implement the "OCRTemplateParser" interface.
  • The new OCR Data Capture class must be declared under the package "com.openkm.ocr.template.parser".
  • The new OCR Data Capture class must be annotated with "@PluginImplementation".

OCRTemplateParser interface:

package com.openkm.ocr.template;

import net.xeoh.plugins.base.Plugin;

import com.openkm.dao.bean.OCRTemplateField;

/**
 * OCRTemplateParser
 * 
 */
public interface OCRTemplateParser extends Plugin {
	Object parse(OCRTemplateField otf, String text) throws OCRTemplateException, OCRParserEmptyValueException;
	String getName();
	boolean isPatternRequired();
}

More information at  Register a new plugin.

Methods description

MethodTypeDescription

parse(OCRTemplateField otf, String text)

Object

Returns the value of the data captured into an Object.

Allowed Object classes:

  • String
  • Calendar ( will be converted to ISO8601 String )
  • Other ( will be converted to string with String.valueOf(object).

getName()

String

Sets the name that will be shown in the administrator user interface selector list.

isPatternRequired()

boolean

When returns true indicates that the pattern field is required.

Will show an error - missing pattern - on the administrator user interface if pattern is required and the user is not set.

Example

StringParser class:

package com.openkm.ocr.template.parser;

import com.openkm.dao.bean.OCRTemplateField;
import com.openkm.ocr.template.OCRParserEmptyValueException;
import com.openkm.ocr.template.OCRTemplateException;
import com.openkm.ocr.template.OCRTemplateParser;
import net.xeoh.plugins.base.annotations.PluginImplementation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * StringParser
 *
 */
@PluginImplementation
public class StringParser implements OCRTemplateParser {

	@Override
	public Object parse(OCRTemplateField otf, String text) throws OCRTemplateException, OCRParserEmptyValueException {
		if (text == null || text.equals("")) {
			throw new OCRParserEmptyValueException("Empty value");
		}

		if (otf.getPattern() == null || otf.getPattern().equals("")) {
			return text != null ? text.trim() : null;
		} else {
			Pattern pattern = Pattern.compile("(" + otf.getPattern() + ")", Pattern.UNICODE_CASE);
			Matcher matcher = pattern.matcher(text);

			if (matcher.find() && matcher.groupCount() == 1) {
				return matcher.group();
			} else {
				throw new OCRTemplateException("Bad format, parse exception");
			}
		}
	}

	@Override
	public String getName() {
		return "String";
	}

	@Override
	public boolean isPatternRequired() {
		return false;
	}
}