问题描述
我正在使用NDK在Android上制作一个Android Shapefile阅读器应用程序.我想使用C ++解析形状文件.我找到了库"Shapefile C库". Github: https://github.com/sw897/shapelib .
i am making an Android Shapefile reader app on Android with the use of NDK. I want to use C++ to parse shape files. I found library "Shapefile C Library". Github: https://github.com/sw897/shapelib.
我正在使用Android Studio,并且不知道如何将其导入到我的Android项目中,因此我可以使用以下功能中描述的功能: http://shapelib.maptools.org/shp_api.html
I am using Android studio and have no idea how to import it to my Android project so i could use functions described in: http://shapelib.maptools.org/shp_api.html
有什么提示吗?
推荐答案
首先,从Google开始研究Hello-JNI示例: https://github.com/googlesamples/android-ndk/tree/master/hello-jni
First, start hacking away at the Hello-JNI example from Google:https://github.com/googlesamples/android-ndk/tree/master/hello-jni
将其用作测试床.
然后,使用public native
方法创建一个Java类,使您可以与库进行交互.高级API之类的东西,可能是将文件名或缓冲区传递给SHPOpenLL,并返回指向ShapeFile上下文(ShapeHandle
)的指针.看起来您的库是用C编写的,因此您应该能够编写一个类似的接口来查询shapefile,并传递ShapeHandle
指针的(jint)c样式强制转换.
Then, create a Java Class with public native
methods that let you interact with your library. Something of a high level API, probably to pass a file name or buffer to SHPOpenLL and return a pointer to the ShapeFile context (ShapeHandle
). Looks like your library is written in C, so you should be able to write a similar interface to query the shapefile passing a (jint) c-style cast of your ShapeHandle
pointer.
接下来,您需要使用javah
来为shapefile界面生成标头.生成标头后,即可使用它在.cc文件中实现您的接口.从那里基本上,您将对C接口进行Java调用,它将返回jobjects. (jint,jbool,jstring等)
Next, you need to play around with javah
to generate the header for your shapefile interface. Once the header is generated, you can use it to implement your interface in a .cc file. From there you will basically make Java calls to your C interface and it will return jobjects. (jint, jbool, jstring, etc...)
我正在查看您要使用的ShapeLib,虽然它很简单,但仍有一些陷阱.
I'm looking at the ShapeLib that you want to use and while it's easy enough, there will be some gotchas.
- 您需要为文件I实现 SAHooks /O.我建议查看 NVFile 作为示例如何访问APK文件(或下载的文件).您将需要使用activity.context.assetmanager对其进行初始化.然后使用nv_file包装FRead/FSeek/etc ...
- 将信息传递回Java将会很困难.就个人而言,我将构建一个Java类来保存形状信息并将其填写在
C
一侧.但是,您可能很想一次从SHPObject
中查询这些参数.会很慢,但更不会出错.
- You will need to implement SAHooks for file I/O. I suggest looking at NVFile for an example how to access APK files (or downloaded files). You will need to init it using activity.context.assetmanager. Then use nv_file to wrap FRead/FSeek/etc...
- Passing info back to java is going to be tough. Personally, I would build a Java class to hold the shape information and fill it out on the
C
side. However, you might be tempted to query these parameters from theSHPObject
one at a time. Will be slow, but less error prone.
例如
// Java
MyJavaShapeObject obj = new MyJavaShapeObject();
_c_retrieveShapeObj((jint)pShapeFile, obj);
// C
java_blah_blah_c_retrieveShapeObj(JNIEnv* env, jclass activity, jint theShapeFile, jobject theObj){
ShapeHandle pShapeFileHandle = (ShapeHandle)theShapeFile; // c style conversion
// http://stackoverflow.com/questions/11647646/how-to-use-the-jni-to-change-the-fields-of-a-java-class
// http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html#wp16613
// http://stackoverflow.com/a/36759159/7949696
}
这篇关于Android,将外部库导入到NDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!