在java中调用C语言的DLL共享动态库的方法.我主要参考是j2sdk文档中JNI中的部分,同时也看了看部分jni.h及相关头文件,
感觉实现起来还是比较容易的.
参考了http://www.cnblogs.com/Aimd/archive/2006/08/18/480213.html和recv的博客,很容易就制作成功了一个java方面的
JNI调用.
- package interfaces;
- public class MyNative {
- public static native String recognize(String file);
- public static String recognizeFile(String file) {
- return recognize(file);
- }
- static {
- System.loadLibrary("JavaJNIDLL");
- }
- public static void main(String[] args) {
- String res = recognizeFile("vercode.jpg");
- System.out.println(res);
- }
- }
[lidh04@localhost~$] javac interfaces.MyNative.java
[lidh04@localhost~$] javah -jni interfaces.MyNative
[lidh04@localhost~$] ls
interfaces_MyNative.h interfaces
然后部分C实现代码是这样的:
- #include <stdlib.h>
- #include "interfaces_MyNative.h"
- #include "recognize.h"
- int recognize_non8bit_figure_by_file(const char *file, char result[MAX_FIGURES+1]);
- jstring JNICALL Java_interfaces_MyNative_recognize (JNIEnv *jEnv, jclass jcls, jstring jfile)
- {
- const char *fileName = jEnv->GetStringUTFChars(jfile, 0);
- char buf[MAX_FIGURES+1] = {0};
- recognize_non8bit_figure_by_file(fileName, buf);
- jchar uniRes[MAX_FIGURES+1] = {0};
- char *p = buf;
- jchar *pUincode = uniRes;
- while (*p)
- {
- *pUincode++ = *p++;
- }
- int len = pUincode - uniRes;
- assert(len < MAX_FIGURES + 1 && len >= 0);
- jstring result = jEnv->NewString(uniRes, len);
- return result;
- }
然后制作一个共享库就行了,放在MyNative类类包interfaces的目录下,然后就可以运行进行了识别了,这样就可以完美地把
java和C++语言的结合进来, 不再出现前面用java调用外面程序出现死锁的情况了.就也算是一种解决方案了吧.
呵呵,,,用java自己开发出JPG到BMP程序,这样就可以完全实现纯JAVA的实现代码了.目前正在努力中....
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>