JAVA版opencv helloworld

mkdir helloworld
cd helloworld/
vi HelloWorld.java
javac HelloWorld.java
javah -jni HelloWorld
vi HelloWorld.h
vi HelloWorld.c
cc -I /usr/lib/jvm/java-1.5.0-gcj/include/linux/ -I /usr/lib/jvm/java-1.5.0-gcj/include/  -I/usr/local/opencv/include/opencv  -L/usr/local/opencv/lib -lcv -lhighgui -fPIC -shared -o libHelloWorld.so HelloWorld.c
java HelloWorld
java -Djava.library.path='.' HelloWorld

 

HelloWorld.c源码:

#include <jni.h>

#include "cv.h"
#include "highgui.h"
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
 int ImageAdjust(IplImage *src,IplImage *dst, 
                 double low,double high,        //x direction 
                 double bottom,double top,    //y direction 
                 double gamma); 

// int main(void) 
 int ImageAdjust(IplImage* src, IplImage* dst, 
         double low, double high,   // X方向:low and high are the intensities of src 
         double bottom, double top, // Y方向:mapped to bottom and top of dst 
         double gamma ) 
 { 
     double low2 = low*255; 
     double high2 = high*255; 
     double bottom2 = bottom*255; 
     double top2 = top*255; 
     double err_in = high2 - low2; 
     double err_out = top2 - bottom2; 
     int x,y; 
     double val; 
     if(low<0 && low>1 && high <0 && high>1&& 
     bottom<0 && bottom>1 && top<0 && top>1 && low>high) 
         return -1; 
       // intensity transform 
     for( y = 0; y < src->height; y++) 
     { 
         for (x = 0; x < src->width; x++) 
         { 
             val = ((uchar*)(src->imageData + src->widthStep*y))[x]; 
             val=pow((val - low2)/err_in, gamma)*err_out+bottom2; 
             if(val>255) 
                 val=255; 
             if(val<0) 
                 val=0; // Make sure src is in the range [low,high] 
             ((uchar*)(dst->imageData + dst->widthStep*y))[x] = (uchar) val; 
         } 
     } 
     return 0; 
 }
//JNIEXPORT jint JNICALL Java_com_opencvtest_opencvtest_getKeypointNum(JNIEnv* env,jobject thiz)
JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv* env,jobject thiz)
 { 
     //char *filename="../Histogram/Equalize DarkClouds.jpg";
     char *filename="lena.jpg";
     IplImage *dst,*src = cvLoadImage(filename,0);
     if(!src) 
     { 
         printf("Couldn't seem to Open %s, sorry\n",filename); 
         return -1; 
     } 
     
  // cvNamedWindow( "src", 1 ); 
  //   cvNamedWindow( "result", 1 ); 

     
     // Image adjust 
        dst = cvCloneImage(src);
     // 输入参数 [0,0.5] 和 [0.5,1], gamma=1 
    // if( ImageAdjust( src, dst, 0, 0.5, 0.5, 1, 1)!=0)
        if( ImageAdjust( src, dst, 0, 1, 1, 0, 1)!=0)
         return -1; 


     
    // cvShowImage( "src", src ); 
    // cvShowImage( "result", dst ); 
     cvSaveImage("conre.jpg",dst,0);
     cvSaveImage("consrc.jpg",src,0);
    // cvWaitKey(0); 
    // cvDestroyWindow("src"); 
    // cvDestroyWindow("result"); 
     cvReleaseImage( &src ); 
     cvReleaseImage( &dst ); 
     
     return 0; 
 } 

 

HelloWorld.h源码:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorld
 * Method:    print
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_print
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

HelloWorld.java源码:

class HelloWorld
{
    private native void print();
    public static void main(String[] args)
    {
        new HelloWorld().print();
    }

    static
    {
        System.loadLibrary("HelloWorld");
    }
}

 


 
 

你可能感兴趣的:(java,jni,职场,opencv,休闲)