5 linux jni 调用dcmtk

1 安装dcmtk

https://github.com/commontk/DCMTK/blob/patched-DCMTK-3.6.1_20160216/INSTALL
比较坑的是这个装的是静态的库,折腾了我一天,你们也可以先试试,看怎么利用第三方静态库编译成.so,然后供jni调用,按道理是可以成功的,但都是泪。

正确方法:血泪史
使用cmake,然后将BUILD_SHARED_LIBS=ON
http://stackoverflow.com/questions/18435516/how-to-set-a-cmake-option-at-command-line

cmake -DBUILD_SHARED_LIBS=ON
make
make install

这样就生成了动态库了,接下来方法与上一篇一致了
5 linux jni 调用dcmtk_第1张图片

2 编写jniDcmtk.java

public class jniDcmtk{
   public native void sayHello();
   public native String jniReadDcm(String src);
   public static void main(String[] args){
       System.loadLibrary("jniDcmtk");
       jniDcmtk jniDemo = new jniDcmtk();
       jniDemo.sayHello();
       System.out.println(jniDemo.jniReadDcm("test.dcm"));
   }
}

3 javac生成.class文件

javac jniDcmtk.java

4 javah 生成.h文件

javah jniDcmtk

5 编写source.cpp

#include<iostream>
#include<string>
#include "dcmtk/dcmimgle/dcmimage.h"
#include "dcmtk/dcmimgle/dipixel.h"
#include "dcmtk/dcmimgle/diimage.h"
#include "dcmtk/dcmimgle/dimo1img.h"
#include "dcmtk/dcmimgle/dimo2img.h"

#include"jniDcmtk.h"
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
using namespace std;

JNIEXPORT void JNICALL Java_jniDcmtk_sayHello
  (JNIEnv *env, jobject jj){
  cout<< " hello jniDcmtk " << endl;
};


JNIEXPORT jstring JNICALL Java_jniDcmtk_jniReadDcm
  (JNIEnv *env , jobject jj, jstring js){
        const char * str_in = env->GetStringUTFChars(js, false);
        string tem1, tem2;
        tem1 = str_in;
        cout<<"reading "<<tem1<<" now"<<endl;
        DicomImage *image = new DicomImage(str_in);
    if (image != NULL)
    {
      if (image->getStatus() == EIS_Normal)
      {
        if (image->isMonochrome())
        {
          image->setMinMaxWindow();
          Uint8 *pixelData = (Uint8 *)(image->getOutputData(8 /* bits */));
          if (pixelData != NULL)
          {
            /* do something useful with the pixel data */
          }
        }
      } else
        cerr << "Error: cannot load DICOM image (" << DicomImage::getString(image->getStatus()) << ")" << endl;
    }

        delete image;
        cout << "write successful!" << endl;

        cout << "tem1:" << tem1 << " tem2:" << tem2 << endl;
        const char * str_out = tem2.c_str();
        jstring rt = env->NewStringUTF(str_out);
        return rt;
};

6 生成动态链接库.so

g++ source.cpp -I/usr/local/include -L/usr/local/lib -ldcmimgle -ldcmdata -lz -loflog -lofstd -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux -shared -fPIC -o libjniDcmtk.so

7 编辑dcmtk.config
把mitk动态库路径添加进去

vim /etc/ld.so.conf.d/dcmtk.conf

8 把libjniDcmtk.so添加到环境中

export LD_LIBRARY_PATH=/home/huxiang_j/work/jni/jni_dcmtk

9 运行

root@4cd44daf2b17:/home/huxiang_312/work/jni/jni_dcmtk# ls 
jniDcmtk.h     libjniDcmtk.so
jniDcmtk.class                                  jniDcmtk.java  source.cpp
root@4cd44daf2b17:/home/huxiang_312/work/jni/jni_dcmtk# java jniDcmtk 
 hello jniDcmtk 
reading test.dcm now
write successful!
tem1:test.dcm tem2:

其他
1 如何使用g++编译dctmk.cpp代码
g++
http://stackoverflow.com/questions/41669590/how-to-load-a-dicom-image-using-dcmtk-3-6-1

root@4cd44daf2b17:/home/huxiang_312/work/dcmtk# g++ loadDicom.cpp -DHAVE_CONFIG_H -I/usr/local/include -L/usr/local/lib/ -pthread -ldcmimgle -ldcmdata -lz -loflog -lofstd -o main
root@4cd44daf2b17:/home/huxiang_312/work/jni/dcmtk# g++ source.cpp -I/usr/local/include -L/usr/local/lib/ -pthread -ldcmimgle -ldcmdata -lz -loflog -lofstd -static -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux -fPIC -o source.o

你可能感兴趣的:(java,jni,mitk)