package org.myrobotlab.service;

import java.io.File;

import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.ServiceType;
import org.myrobotlab.image.SerializableImage;
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.Logging;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.service.TesseractOcr.Environment.POSIX;
import org.slf4j.Logger;

import com.sun.jna.Library;
import com.sun.jna.Native;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

/**
 * 
 * TesseractOCR - This service will use the open source project tesseract.
 * Tesseract will take an Image and extract any recognizable text from that
 * image as a string.
 *
 */
public class TesseractOcr extends Service {

  private static final long serialVersionUID = 1L;

  public final static Logger log = LoggerFactory.getLogger(TesseractOcr.class.getCanonicalName());

  public static void main(String[] args) {
    LoggingFactory.init(Level.WARN);

    try {

      TesseractOcr tesseract = new TesseractOcr("tesseract");
      tesseract.startService();

      Runtime.createAndStart("gui", "SwingGui");
      /*
       * SwingGui gui = new SwingGui("gui"); gui.startService();
       */
    } catch (Exception e) {
      Logging.logError(e);
    }
  }

  /*
   * Static list of third party dependencies for this service. The list will be
   * consumed by Ivy to download and manage the appropriate resources
   */

  public TesseractOcr(String n) {
    super(n);
    File file = new File(".");
    String filed = file.getAbsolutePath();
    POSIX e = new Environment.POSIX();
    e.setenv("TESSDATA_PREFIX", filed.substring(0, filed.length() - 1), 1);
    // for (Entry<String, String> g : System.getenv().entrySet()) {
    // System.out.println(g.getKey() + "=" + g.getValue());
    //
    // }
  }

  public String OCR(SerializableImage image) {
    try {
      String hh = Tesseract.getInstance().doOCR(image.getImage());
      // System.out.println(hh);
      log.info("Read: " + hh);
      return hh;
    } catch (TesseractException e) {
      e.printStackTrace();
    }
    return null;

  }

  @Override
  public void releaseService() {

    super.releaseService();
  }

  @Override
  public void stopService() {
    super.stopService();
  }

  /**
   * This static method returns all the details of the class without it having
   * to be constructed. It has description, categories, dependencies, and peer
   * definitions.
   * 
   * @return ServiceType - returns all the data
   * 
   */
  static public ServiceType getMetaData() {

    ServiceType meta = new ServiceType(TesseractOcr.class.getCanonicalName());
    meta.addDescription("Optical character recognition - the ability to read");
    meta.addCategory("intelligence");
    meta.addDependency("net.sourceforge.tess4j", "1.1");
    meta.addDependency("com.sun.jna", "3.2.2");
    return meta;
  }

  static class Environment {
    public interface LinuxLibC extends Library {
      public int setenv(String name, String value, int overwrite);

      public int unsetenv(String name);
    }

    static public class POSIX {
      static Object libc;

      static {
        if (System.getProperty("os.name").equals("Linux")) {
          libc = Native.loadLibrary("c", LinuxLibC.class);
        } else {
          libc = Native.loadLibrary("msvcrt", WinLibC.class);
        }
      }

      public int setenv(String name, String value, int overwrite) {
        if (libc instanceof LinuxLibC) {
          return ((LinuxLibC) libc).setenv(name, value, overwrite);
        } else {
          return ((WinLibC) libc)._putenv(name + "=" + value);
        }
      }

      public int unsetenv(String name) {
        if (libc instanceof LinuxLibC) {
          return ((LinuxLibC) libc).unsetenv(name);
        } else {
          return ((WinLibC) libc)._putenv(name + "=");
        }
      }
    }

    public interface WinLibC extends Library {
      public int _putenv(String name);
    }

    static POSIX libc = new POSIX();

  }

}

@@ -1234,24 +1234,26 @@
 %22gui%22, %22
-SwingGui
+GUIService
 %22);%0D%0A   
@@ -1268,24 +1268,26 @@
       * 
-SwingGui
+GUIService
  gui = n
@@ -1293,16 +1293,18 @@
 new 
-SwingGui
+GUIService
 (%22gu
@@ -1411,24 +1411,25 @@
 %0A  %7D%0D%0A%0D%0A  /*
+*
 %0D%0A   * Stati
@@ -1570,16 +1570,37 @@
 ources%0D%0A
+   * %0D%0A   * @return%0D%0A
    */%0D%0A%0D
@@ -3028,11 +3028,13 @@
 %22, %22
-1.1
+3.4.0
 %22);%0D

rebuild ?