Wednesday, October 8, 2014

Java Wrapper for Tesseract OCR Library

Tesseract is a very popular OCR library written in C++. It can be simply used to identify characters in a given image that contains text. In addition to that it can be used to get positions of each word/ character. Tesseract provides a command line tool and a C++ api to give services to users. However there is not a implementation for Java users that can directly use Tesseract for their applications.

As a part of my GSoC project in Apache PDFBox  I implemented a Java wrapper for Tesseract C++ api that can be used by Java users to directly use Tesseract in their applications. Code repository can be found from here.

To use Java API simply import Tesseract-JNI-Wrapper-1.0.0.jar to your project. If you are using maven, add this to your pom

<dependency>
  <groupId>org.apache.pdfbox.ocr</groupId>
  <artifactId>Tesseract-JNI-Wrapper</artifactId>
  <name>Tesseract Jni Wrapper</name>
  <version>1.0.0</version>
</dependency>


Here is a sample code that can use Java API invoke Tesseract.

public String getOCRText(BufferedImage image){ //You need to send BufferedImage (RGB) of scanned image
  TessBaseAPI api = new TessBaseAPI();
  boolean init = api.init("src/main/resources/data", "eng"); // position of Training data files
  api.setBufferedImage(image);
  String text = api.getUTF8Text();
  System.out.println(text);
  api.end();
  return text;
}


Getting positions of each OCRed word

public void printOCRTextPositions(BufferedImage image){
  TessBaseAPI api = new TessBaseAPI();
  boolean init = api.init("src/main/resources/data", "eng");
  api.setBufferedImage(image);
  api.getResultIterator();
  if (api.isResultIteratorAvailable()) {
    do {
      System.out.println(api.getWord().trim());
      String result = api.getBoundingBox();
      System.out.println(result);
    } while (api.resultIteratorNext());
  }
  api.end();
}


P.S.
This wrapper currently is working in MacOS and Linux environments. It wasn't tested in Windows environments. If anyone is willing to develop or improve functionalities of this wrapper please let me know.

Tuesday, October 7, 2014

Continuous Integration for GitHub - Travis CI

Travis CI is a very impressive and cool CI tool that can directly fetch and automatically build your GitHub projects. Following few steps you can easily integrate your GitHub projects with Travis CI

1. Got to https://travis-ci.org/ and log in using your GitHub account

2. click + button and add your project to Travis CI




3. Add .travis.yml file to the root folder of the project and push it to GitHub
This is the file that contains configuration details to Travis CI about your project details like language and build instructions
If your project is a java maven project, you can simply add

language: java

install: mvn install -Dmaven.compiler.target=1.6 -Dmaven.compiler.source=1.6 -DskipTests=true

script: mvn test -Dmaven.compiler.target=1.6 -Dmaven.compiler.source=1.6

For more configuration details refer to the documentation of Travis CI

4. Do some change to your project and push it to GitHub. Commit will be reflected in Travis Console same time and it will start to build project automatically and send build details to your mail.


Thursday, January 30, 2014

LDAP connector for WSO2 ESB

As we were working on our intern project : "Infra portal", we had to connect to WSO2 user store LDAP (Lightweight Directory Access Protocol) directory in several ways like authenticating users, getting user list in a particular group, adding new entries (users in our case), editing entries and deleting them. Currently these operations can be done using javax.naming.* packages in java. However because most of our developments are done in jaggery, we had to write a java client for each property and integrate it with jaggery server. Then we could invoke its methods inside jaggery. But when time goes on, it was really painful to write methods to each operation that is required in the application in a single java class. It became too large and most of them were repetitions of previous methods with small changes, which is not suitable. 

So we decided to search for a better solution and finally we decided it is better to write a LDAP connector for WSO2 ESB. Main reasons for writing a LDAP connector were

  1. Connecting through an ESB connector makes most of the integrations with external LDAP directories become easy
  2. Loosely coupled interface between LDAP and client
  3. Others who are willing to use LDAP in their products can re-use this easily
  4. Language independent (Data exchange is done using REST or SOAP)


In our connector there are basic four functions implemented and one special function

Basic functions

  • Add new entry
  • Delete an entry
  • Update an entry
  • Search for an entry


Special Functions

  • Authenticate user


Before using any operation it is required to provide admin authenticate details to ESB. For that there is an Init operation.

<ldap.init xmlns="http://ws.apache.org/ns/synapse">
      <providerUrl>ldap://192.168.1.164:389/</providerUrl>
      <securityPrincipal>cn=admin,dc=wso2,dc=com</securityPrincipal>
      <securityCredentials>comadmin</securityCredentials>
   </ldap.init>
   
This signs in as the admin of LDAP directory which can perform any operation on LDAP Directory.
It is better to put this in a local entry and refer it in other operations with configKey

Add new entry

<ldap.addEntry configKey="LdapConfig">
    <objectClass>inetOrgPerson</objectClass>
    <dn>uid=dimuthuu2,ou=staff,dc=wso2,dc=com</dn>
    <attributes>cn=Dimuthu2Upeksha,mail=dimuthuu2wso2.com,userPassword=123,sn=Dimuthu2</attributes>
</ldap.addEntry

To add a new entry there are 3 parameters. 
1. Object class - This is a mandatory parameter. This defines the objectClass of the new entry
2. dn - Distinguished name of the new entry
3. attributes - Other attributes you need to add in to the entry


Delete an entry

<ldap.deleteEntry configKey="LdapConfig">
    <dn>uid=dimuthuu2,ou=staff,dc=wso2,dc=com</dn>
</ldap.deleteEntry>

Update an entry

<ldap.addEntry configKey="LdapConfig">
    <dn>uid=dimuthuu2,ou=staff,dc=wso2,dc=com</dn>
    <attributes>cn=Dimuthu2Upeksha,mail=dimuthuu2wso2.com,userPassword=123,sn=Dimuthu2</attributes>
</ldap.addEntry

1. dn - Distinguished name of the entry that is needed to update attributes
2. attributes- Key value pairs of attributes that are needed to be changed

Search for an entry

This searches a particular entry of a set of entries for given keywords.

<ldap.searchEntry configKey="LdapConfig">
            <objectClass>inetOrgPerson</objectClass>
            <filters>uid=dimuthuu</filters>
            <dn>ou=staff,dc=wso2,dc=com</dn>
            <attributes>uid,mail</attributes>
</ldap.searchEntry>

1. objectClass - type of entry that is needed to be searched
2. filters - keywords to search. Above case: search entries with uid with "dimuthuu"
3. dn - Distinguished name of the scope which searching should be applied.
4. attributes -  Attributes of the entry that should be included in the search result.


Authenticate

LDAP authentication is one of the major requirement in most LDAP based applications. To simplify this authentication mechanism, there is a special operation. For your given username and password it tells whether authentication succeeded or not.

<ldap.authenticate configKey="LdapConfig">
            <dn>uid=dimuthuu,ou=staff,dc=wso2,dc=com</dn>
            <password>1234</password>
</ldap.authenticate>

1. dn : Distinguished name of user
2. password: password of the user

-----------------------------------------
Special thanks should go to WSO2 ESB team including Dushan ayya and Isuru ayya for giving us a great help when we were in trouble.

If you think that this should be improved or I'm missing something here, please do comment below. Thanks

Thursday, December 26, 2013

Adding Google Maps to your Java Standalone applications

Google maps is a very impressive tool to present details of particular location in any part of world. It is very common to see google maps integration in web applications using java scripts. However it can be included in to Java SE applications also. It can be simply done using google static maps api and java swing components. Here is my code.

public class Main {
     JFrame frame = new JFrame();
     JPanel panel;
     BufferedImage image;
     public void show(String gps) {
            panel = new JPanel();
            try {
                   image = ImageIO.read(new URL("http://maps.google.com/staticmapcenter="+gps+"&zoom=14&size=600x300&maptype=roadmap&markers="+gps+"&sensor=false&key=ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&format=jpg"));

                   JLabel label = new JLabel(new ImageIcon(image));
                   panel.add(label);
                   frame.add(panel);
                   frame.pack();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setLocationRelativeTo(null);
                   frame.setVisible(true);
            } catch (MalformedURLException e) {
                   e.printStackTrace();
            } catch (Exception e) {
                  e.printStackTrace();
            }
     }
     public Main(){
             show("6.423601,79.996755");
     }

     public static void main(String[] args){
           new Main();
     }

}





More details can be added to map by referring to google static maps api.

Monday, September 23, 2013

My GSoC project - A generic Android viewer for Apache ISIS

Project sources can be found from here 

Overview

Apache Isis software is a framework for rapidly developing domain-driven apps in Java. 
Write your business logic in entities, domain services and repositories, and the framework dynamically generates a representation of that domain model as a webapp or a RESTful API. 



Specially this can be used for rapid prototyping or production. Isis works by building a metamodel from the domain object models, from which a generic user interface is generated dynamically at runtime. 
There are several implementations of the generic UI, one based on Wicket, one based on Servlet/JSPs, and one based on jax-rs and serving up a Restful API over http and json. 

This API is fully documented in the Restful Objects spec there is also a (non-Apache) open source implementation on .NET.

This GSOC suggestion is to develop a native Android app that will consume the RESTful API provided by Isis to provide a generic (naked objects) viewer for use either from a smartphone or tablet. 
Optionally this generic viewer could be extensible to allow mashups (as is supported by Isis' own Wicket-based viewer).

Architecture of the application

Basic task of ISIS Android viewer is to use the Restful Object viewer of Apache ISIS and generate useful information that can be easily readable by Android users.

Application should be able to read the services, domain entities and other details given by framework as JSON objects. Then it should be able to decode it and understand what should be done with this data to create the user interface in application. 

Architectural Goals and Constraints

  • First and major constrain as name of the project implies, this should be a generic view. It should support to all versions of Android OS starting from quite old 2.2 version to current stable latest version  4.2. This is a great challenge to create a such an application maintaining performance and adaptability in single piece.
  • Because this is a application that runs on a mobile device, amount of processing power which is available is limited in compared to a typical computer. So it is needed to manage processor time effectively
  • Android phones are in different sizes. So applications UI should be adjusted according to the size of hosting device and amount of details that is shown on the window should be filtered.  

Layers


Application is basically built upon three layers


1 User Interface Layer


This Layer consists of the elements that are needed to render user interfaces of the application.

a. Activities:
This application is basically an Android application. So the user interfaces are drawn using the Activity classes. One of the major deviation of these activity classes in compared to general activity class in an Android application is, these classes never used pre defined XML to generate UI elements. That means there is no any single UI element, which is rendered in static way. All the UIs are rendered dynamically at the run time.

b. UIModel
This class stores most visited interfaces’ details. It acts like a proxy for the UI. This increases the responsiveness of the application.

2 Applib Layer


This layer contains basically logical stuff. It is responsible to fetch data from server and filter data out of it and pass useful information to UI layer 

a. Constants
Application runs between two significantly different platforms : Apache ISIS data layer and Android layer. The data types and its meanings are different to each other. So it is required to convert Android data types to ISIS data types and vise versa. “Constant” classes do that functionality using one to one mapping of java data types to Android view elements. Simply these classes are working as Adapter classes. 

b. Communicator 
These classes are responsible for encapsulating communication complexities of the server and application to higher layers. It provides interfaces to upper layers in a way that upper layer never feels the underlying stuff happening between application and server.

3 Representation Layer

This layer contains classes that are used to “bean binding”. They are responsible to read plain JSON strings coming from the server and map them to actual objects (Beans). Then upper layers receives the data as a sequence of objects so that data can be retrieved using getters and setters.

Setting up development environment



Isis Android client is compatible with Android 2.2 to 4.2. So basically it can be run on any any android phone or tablet available in market.

To set up the development environment you need eclipse IDE installed with an Android sdk.

http://www.eclipse.org/downloads/
http://developer.android.com/sdk/index.html

Clone the project sources from github

https://github.com/DImuthuUpe/ISIS_Android_Viewer

There are two projects

  • Android_Viewer
  •  Android_Viewer_Test
And one library project

  • JakeWharton-ActionBarSherlock

Android_Viewer is the main project that contains the actual application and for testing purposes "Android_Viewer_Test" project is used

To open these projects

  • Import the supporting library project

file -> import -> Android -> Existing Android Code Into Workspace
project folder->JakeWharton-ActionBarSherlock->library
Go to properties and in the Android tab make sure the check box with the title "Library" is ticked

  • Import other two projects

file -> import -> Android -> Existing Android Code Into Workspace
Select project root
Do this to both projects
Finish

  •  Set up an android emulator emulator to run the project.
  •  To run the application, right click on the Android_Viewer project -> run as -> Android Application.
  • Select the  target emulator to run

Maven build and Configuration

To build the project using maven build tool

  1. From command line go to Android_Viewer folder
  2. Type "mvn clean install" to create apk for the project. Those binaries are generated under "target" folder.

Configurations of pom.xml

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
    <!-- Specify the android version-->
            <version>4.1.1.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
      <groupId>com.actionbarsherlock</groupId>
      <artifactId>actionbarsherlock</artifactId>
      <version>4.4.0</version>
       <type>apklib</type>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <sourceDirectory>src</sourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>3.6.1</version>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <configuration>
                    <sdk>
                        <!-- platform or api level (api level 18 = platform 4.3)-->
                        <platform>18</platform>
                        <!-- provide the path to the android sdk -->
                     <path>/Users/dimuthuupeksha/Downloads/adt-bundle-mac-x86_64-20130729/sdk</path>
                    </sdk>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Demos





Screen Captures 












Friday, July 27, 2012

Create your own LED cube

A LED cube is a very interesting and fascinating item that can gain lot of fun and knowledge. It's simply a 3D screen that can demonstrate your own models in a 3D space. If you are very good at programming, it will be very useful to craft more complex patterns on the cube

In my project I created my LED cube which is size of 8*8*8 (512 Leds). Here is the video
 I used red 3mm LEDs with 2.5 cm distance. Size, color and distance of LEDs are very important to make cube more visible and avoid ghost effects of reflection. It is better if you can use diffused LEDs (they can emit light to every direction with same intensity) but I couldn't find those in local market.

I used PIC16f877A microcontroller to  control entire cube. It has 40 pins and about 30 -25 usable pins to our task. But thing we don't forget here is that cube has 512 LEDs and they should have separate 512 wires to work with. Here I used the theory of using layers. So each layer has 64 LEDs and there are 8 such layers. One layer is lighten up at a time. Using the sensitivity of our eyes we can pretend that all layers are lightening at once when we switch them in a very high frequency.

In a layer all cathodes of leds are connected to one and anodes are kept free. Creating a layer is a bit challenging task. We should guarantee that all leds in the layer are keeping 2.5 cm gap and lie on the same plane. So it is easy if we can make a model like this to create the layer.

Then we can put leds into these holes and solder as we wish

After all cathodes are soldered we can see a layer like this

After all layers are finished we can move to connect layers to each other. When we are connecting layers we should make sure that anodes of the leds of one layer should be connected to the same anodes of other layer. Finally we have 64 wires for layer anodes and 8 wires for layers (common cathodes of each layer).

Next problem is how to connect all 64 wires of layers to microcntroller. Instead connecting them directly I used 8 74hc574 latch ics which have D flipflops. Using these ICs, you need only 20 pins from PIC. 8 pins to light up the layer leds, 8 pins to select the layer and 4 pins to select the latch ic.

Q0 - Q7 pins of 74hc574 (for more information refer to 74hc574 datasheet) ics are connected to rows of layer anodes. There are 8 rows the layer anodes so each row is represented by one latch ic. For putting data on a layer first select the latch ic and put data on PORTB of pic. Then move to next latch ic. When we move to next ic, values of previous ones are not changed because its D flipflops remind the previous states. Likewise we can light up a whole layer and then select the layer. Layer common cathodes are connected to J9. Using PORTD we can select a layer.



This is the brief description of the cube I have made. Those all are electronic stuffs and the most challenging task I have experienced is programming this cube. I used Timer0 clock interrupts of pic to switch between layers. Here is the code I have written. I used mikroc pro compiler



unsigned char layers[8][8]={0},i,j,k,l,m,n,q,x,y; //layers array has all values of the cube LEDs.
void loadRegister(int num){ //Here correct latch is selected
     PORTA.B0  = num%2;
     num=num>>1;
     PORTA.B1  = num%2;
     num=num>>1;
     PORTA.B2  = num%2;
     PORTA.B3=0;
     PORTA.B3=1;
}


void loadLayer(int layerNum){ //lightening a layer is done
     PORTD= 0b00000000;
     for(i=0;i<8;i++){
          PORTB=layers[layerNum][i];
          loadRegister(i);
     }
     if(layerNum==0){
          PORTD= 0b00000001; //selecting the layer
     }else if(layerNum==1){
          PORTD= 0b00000010;
     }else if(layerNum==2){
          PORTD= 0b00000100;
     }else if(layerNum==3){
          PORTD= 0b00001000;
     }else if(layerNum==4){
          PORTD= 0b00010000;
     }else if(layerNum==5){
          PORTD= 0b00100000;
     }else if(layerNum==6){
          PORTD= 0b01000000;
     }else if(layerNum==7){
          PORTD= 0b10000000;
     }
}


int num=0,conter=0;
char doCount=0;


void interrupt(){                              //Timer0 interrupts are handled at here. Here is the main driving section of the cube
     if(INTCON.TMR0IF){
        if(doCount){
           conter++;
        }
        loadLayer(num);
        num++;
        if(num==8)num=0;
        INTCON.TMR0IF = 0; // clear TMR1IF
     }


}


void init_timer(){
    OPTION_REG = 0b10000101; //Timer0 interrupts are configured
    INTCON = 0b10100000; //Interrupts are enabled.
}


void delayms(int num){
     doCount=1;
     while(conter<num);
     conter=0;
     doCount=0;


}
int power(int p){
    int val=1;
    if(p==0)return 1;
    for(k=0;k<p;k++){
        val=val*2;
    }
    return val;
}


int clearAll(){
    int i,j;
    for(i=0;i<8;i++){
      for(j=0;j<8;j++){
         layers[i][j]=0;
      }
    }
}


void circle(int xcod,int ycod,int zcod,int rad){
     int x,y,m;
     for(x=0;x<8;x++){
         for(y=0;y<8;y++){
             for(m=0;m<8;m++){
                 if((x-xcod)*(x-xcod)+(y-ycod)*(y-ycod)+(m-zcod)*(m-zcod)-rad<=0){
                     layers[m][x]= layers[m][x]|power(y);
                 }
             }
         }
     }
}


void parabola(int xcod,int ycod,int c){


     int x,y,z;
     //clearAll();
     for(x=0;x<8;x++){
        for(y=0;y<8;y++){
            for(z=0;z<8;z++){
                if(-(x-xcod)*(x-xcod)-(y-ycod)*(y-ycod)+c-z>=0)
                layers[z][x] = layers[z][x]|power(y);
            }
        }


     }


}


void plane(int a,int b,int c,int d){
     int x,y,z;
     //clearAll();
     for(x=0;x<8;x++){
        for(y=0;y<8;y++){
            for(z=0;z<8;z++){
                if((a*x+b*y-d)/c==z)
                layers[z][x] = layers[z][x]|power(y);
            }
        }


     }
}


int random,temp;


void main() {
     TRISB =0;
     TRISD=0;
     TRISA=0;
     ADCON1 = 0b00000111;
     PORTA.B3=0; //en pin






     init_timer();
     while(1){
        for(m=0;m<8;m++){
           for(n=0;n<8;n++){
             for(l=0;l<8;l++){
                layers[l][m]=layers[l][m]+power(n);
                delayms(10);
             }
           }
        }
        for(m=0;m<8;m++){
           for(n=0;n<8;n++){
             for(l=0;l<8;l++){
                layers[l][m]=layers[l][m]-power(n);
                delayms(10);
             }
           }
        }
        clearAll();
        for(m=0;m<8;m++){
           for(n=0;n<8;n++){
             for(l=0;l<8;l++){
                layers[l][m]=power(n);
                delayms(10);
                clearAll();
             }
           }
        }
        clearAll();




     for(q=0;q<100;q++){
       for(x=0;x<5;x++){
       for(l=1;l<8;l++){
          for(m=0;m<8;m++){
             temp=layers[7-l][m];
             layers[8-l][m]=temp;
          }
       }
       random= rand()%64;
       layers[0][random/8]=power(random%8);
       delayms(15);
       }
     }
     clearAll();




      for(y=0;y<8;y++){
         for(m=0;m<8;m++){
             circle(m,m,m,4);
             delayms(20);
             clearAll();
         }
      }
      
      for(y=0;y<8;y++){
         for(m=0;m<8;m++){
             circle(m,7-m,m,4);
             delayms(20);
             clearAll();
         }
      }
      
      for(y=0;y<8;y++){
         for(m=0;m<8;m++){
             circle(7-m,7-m,m,4);
             delayms(20);
             clearAll();
         }
      }
      
      for(y=0;y<8;y++){
         for(m=0;m<8;m++){
             circle(7-m,m,m,4);
             delayms(20);
             clearAll();
         }
      }


      for(y=0;y<8;y++){
         for(m=0;m<8;m++){
            parabola(3,3,m);
            delayms(50);
            clearAll();
         }
      }


      for(y=0;y<10;y++){
         for(m=0;m<8;m++){
            plane(rand()%5,rand()%5,rand()%5,rand()%5);
            delayms(50);
            clearAll();
         }
      }
     
     }


}

I should say that this is a very very brief description and sometime you may find difficult to understand some concepts of this article. If so please don't hesitate to ask questions and I'm very happy to help you :)

Thank you

Wednesday, June 13, 2012

Configuring javacv

opencv (open computer vision) is an image processing library which is introduced by intel that is very powerful in processing images in very powerful and effective manner. However opencv is written in c language so most of my friends including me faced several difficulties with programming in c. So I decided to move to javacv ; a java wrapper that can naively call opencv functions. It is a general acceptance that java is relatively slower than C but believe me people who invented javacv have designed this in a way that you can never feel that kind of slowness in your java code.

In this blog post I hope to explain how to configure javacv with your IDE (In my case I use netbeans). Though javacv is easy, configuring is bit hard to catch from the first attempt. So I think this will be very useful to java people who are intending for new technology.

To install javacv there are few requirement
JDK
opencv 2.4 http://sourceforge.net/projects/opencvlibrary/files/
Netbeans ide
javacv-0.1-bin.zip  http://code.google.com/p/javacv/downloads/list


You need to install opencv2.4 into C:/opencv folder (It is the default location). I recommend to use same locations otherwise you will have to face many difficulties.
Extract javacv-0.1-bin.zip to a location in your hard disk 
Go to netbeans and create a new java project.
Add those jar files to your project







Then run this code




package javaapplication1;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;

public class JavaApplication1 {

    public static void main(String[] args) {
        show("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
    }
    public static void show(String filename) {
        cvNamedWindow("dim");
        IplImage image = cvLoadImage(filename);
        if (image != null) {
            cvShowImage("dim", image);
            cvWaitKey(0);
            cvReleaseImage(image);
        }
     
    }
}



If you have installed opencv to default folder this code should run without any error. 

If it gives an error like this
This exception tells that it can't find opencv location in your machine. Actually this error comes from the one of the jar you have added to the project. In my case it is javacv-windows-x86.jar. It depends on your processor architecture. To resolve this error you have to download  http://www.dependencywalker.com/
to catch what are the dependencies are missing in you jar.
Then extract that jar using win rar and make sure that jniopencv_core.dll is in that folder.
Open that dll using  Dependency Walker 
As you can see there is one dll is missing. It is marked from yellow color. These dll are located in your opencv installation folder. Go to it and search for that dll. Then you can add it to system path. There are two ways to do that.
1 Easy way - you can simply copy that dll s to system32 folder in windows folder
2 Using environmental variable settings
 go to my computer -> properties -> Advanced System Settings -> Environment Variables
Select the path variable from the category of system variables

Click edit and append the path of the dll file to the end of the variable

Then click view -> refresh in Dependancy walker
If more dependency errors are shown, do the same procedure until no error is shown. I know this is hard :) But I couldn't figure out an easy way to do this

If all configurations are ok then you will be able to run that small code

References 
Any questions?