hellojni.h
#define _Included_HelloJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloJNI
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloJNI_sayHello
(JNIEnv *, jobject);
/*
* Class: HelloJNI
* Method: sayOK
* Signature: (Ljava/lang/String;)D
*/
JNIEXPORT jdouble JNICALL Java_HelloJNI_sayOK
(JNIEnv *, jobject, jstring);
/*
* Class: HelloJNI
* Method: getFileGdb
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_HelloJNI_getFileGdb
(JNIEnv *, jobject, jstring);
/*
* Class: HelloJNI
* Method: getFileGdbStr
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_HelloJNI_getFileGdbStr
(JNIEnv *, jobject, jstring);
/*
* Class: HelloJNI
* Method: getGdbTable
* Signature: (LGeodbEntity;)LGeodbEntity;
*/
JNIEXPORT jobject JNICALL Java_HelloJNI_getGdbTable
(JNIEnv *, jobject, jobject);
#ifdef __cplusplus
}
#endif
#endif
----------hello c++------
#include <jni.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include "HelloJNI.h"
#include <fstream>
#include <time.h>
#include <FileGDBAPI.h>
using namespace std;
using namespace FileGDBAPI;
using namespace std;
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
printf("Hello World!\n");
return;
}
JNIEXPORT jdouble JNICALL Java_HelloJNI_sayOK
(JNIEnv* env, jobject obj, jstring name){
const char* pname=env->GetStringUTFChars(name,NULL);
if(strcmp(pname,"Apple")==0){
env->ReleaseStringUTFChars(name,pname);
cout<<"After release:"<<pname<<endl;
return 1.2;
}
else{
env->ReleaseStringUTFChars(name,pname);
cout<<"After release:"<<pname<<endl;
return 2.1;
}
}
JNIEXPORT jstring JNICALL Java_HelloJNI_getFileGdbStr
(JNIEnv *env, jobject thisObj, jstring path){
/* fgdbError hr;
wstring errorText;
Geodatabase geodatabase;
std::wstring value;
const jchar* raw = env->GetStringChars(path, NULL);
if (raw != NULL) {
jsize len = env->GetStringLength(path);
value.assign(raw, raw + len);
env->ReleaseStringChars(path, raw);
}
if ((hr = OpenGeodatabase(value, geodatabase)) != S_OK)
{
wcout << "An error occurred while opening the geodatabase." << endl;
ErrorInfo::GetErrorDescription(hr, errorText);
wcout << errorText << "(" << hr << ")." << endl;
return (env)->NewStringUTF("7");
}
Row attrQueryRow;
EnumRows attrQueryRows;
wstring sqlStatement(L"SELECT * FROM Cities WHERE TYPE = 'city' AND OBJECTID < 10");
if ((hr = geodatabase.ExecuteSQL(sqlStatement, true, attrQueryRows)) != S_OK)
{
wcout << "An error occurred while performing the attribute query." << endl;
ErrorInfo::GetErrorDescription(hr, errorText);
wcout << errorText << "(" << hr << ")." << endl;
return (env)->NewStringUTF("7");
}
FieldInfo fieldInfo;
attrQueryRows.GetFieldInformation(fieldInfo);
int fieldCount;
FieldType fieldType;
wstring fieldName;
// Iterate through the returned rows printing out all field values.
short shortField;
int32 longField;
float floatField;
double doubleField;
string stringField;
wstring wstringField;
tm dateTimeField;
char datetime[80];
Guid globalIDField;
Guid guidField;
wstring strGuid;
wstring strGlobalID;
bool isNull;
byte * shapeBufferBytes ;
ShapeBuffer geometry;
ByteArray bss;
while (attrQueryRows.Next(attrQueryRow) == S_OK)
{
fieldInfo.GetFieldCount(fieldCount);
for (long fieldNumber = 0; fieldNumber < fieldCount; fieldNumber++)
{
fieldInfo.GetFieldType(fieldNumber, fieldType);
fieldInfo.GetFieldName(fieldNumber, fieldName);
attrQueryRow.IsNull(fieldName, isNull);
if (!isNull)
{
switch (fieldType)
{
case fieldTypeSmallInteger:
attrQueryRow.GetShort(fieldName, shortField);
wcout << shortField << endl;
break;
case fieldTypeInteger:
attrQueryRow.GetInteger(fieldName, longField);
wcout << longField << '\t';
break;
case fieldTypeSingle:
attrQueryRow.GetFloat(fieldName, floatField);
wcout << floatField << '\t';
break;
case fieldTypeDouble:
attrQueryRow.GetDouble(fieldName, doubleField);
wcout << doubleField << '\t';
break;
case fieldTypeString:
attrQueryRow.GetString(fieldName, wstringField);
wcout << wstringField << '\t';
break;
case fieldTypeDate:
attrQueryRow.GetDate(fieldName, dateTimeField);
strftime(datetime,80,"%a %b %d %I:%M:%S%p %Y", &dateTimeField);
wcout << datetime << '\t';
break;
case fieldTypeOID:
attrQueryRow.GetOID(longField);
wcout << longField << '\t';
break;
case fieldTypeGeometry:{
attrQueryRow.GetGeometry(geometry);
// attrQueryRow.GetBinary(fieldName, bss);
double x, y;
shapeBufferBytes = geometry.shapeBuffer;
wcout << "d::x::"<< x<<"y:"<<y<< '\t';;
wcout <<shapeBufferBytes<< "Geometry" << '\t';}
break;
case fieldTypeBlob:
wcout << "Blob" << '\t';
break;
case fieldTypeGUID:
attrQueryRow.GetGUID(fieldName, guidField);
guidField.ToString(strGuid);
wcout << strGuid << '\t';
break;
case fieldTypeGlobalID:
attrQueryRow.GetGlobalID(globalIDField);
globalIDField.ToString(strGlobalID);
wcout << strGlobalID << '\t';
break;
default:
break;
}
}
else
{
wcout << "null" << '\t';
}
}
wcout << endl;
}
attrQueryRows.Close(); // Close the EnumRows
*/
const char * pat="zhy好棒呀 第一个东东出来了";
//定义java String类 strClass
jclass strClass = (env)->FindClass("Ljava/lang/String;");
//获取String(byte[],String)的构造器,用于将本地byte[]数组转换为一个新String
jmethodID ctorID = (env)->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
//建立byte数组
jbyteArray bytes = (env)->NewByteArray(strlen(pat));
//将char* 转换为byte数组
(env)->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
// 设置String, 保存语言类型,用于byte数组转换至String时的参数
jstring encoding = (env)->NewStringUTF("GB2312");
//将byte数组转换为java String,并输出
return (jstring)(env)->NewObject(strClass, ctorID, bytes, encoding);
//return path;
}
JNIEXPORT void JNICALL Java_HelloJNI_getFileGdb(JNIEnv *env, jobject thisObj, jstring path) {
fgdbError hr;
wstring errorText;
Geodatabase geodatabase;
// const wchar_t * szStr =(wchar_t * )env->GetStringChars(path, NULL);
std::wstring value;
const jchar* raw = env->GetStringChars(path, NULL);
if (raw != NULL) {
jsize len = env->GetStringLength(path);
value.assign(raw, raw + len);
env->ReleaseStringChars(path, raw);
}
if ((hr = OpenGeodatabase(value, geodatabase)) != S_OK)
{
wcout << "An error occurred while opening the geodatabase." << endl;
ErrorInfo::GetErrorDescription(hr, errorText);
wcout << errorText << "(" << hr << ")." << endl;
return;
}
wstring sqlStatement(L"SELECT CITY_NAME, POP1990 FROM Cities WHERE TYPE = 'city' AND OBJECTID < 10");
EnumRows attrQueryRows;
if ((hr = geodatabase.ExecuteSQL(sqlStatement, true, attrQueryRows)) != S_OK)
{
wcout << "An error occurred while performing the attribute query." << endl;
ErrorInfo::GetErrorDescription(hr, errorText);
wcout << errorText << "(" << hr << ")." << endl;
return ;
}
// Iterate through the returned rows.
Row attrQueryRow;
int32 cityPop;
wstring cityName;
while (attrQueryRows.Next(attrQueryRow) == S_OK)
{
attrQueryRow.GetInteger(L"POP1990", cityPop);
attrQueryRow.GetString(L"CITY_NAME", cityName);
wcout << cityName << '\t' << cityPop << endl;
}
// SELECT * - Return all fields.
sqlStatement.assign(L"SELECT * FROM Cities WHERE TYPE = 'city' AND OBJECTID < 10");
if ((hr = geodatabase.ExecuteSQL(sqlStatement, true, attrQueryRows)) != S_OK)
{
wcout << "An error occurred while performing the attribute query." << endl;
ErrorInfo::GetErrorDescription(hr, errorText);
wcout << errorText << "(" << hr << ")." << endl;
return;
}
// Get the field type and name from the row enumerator.
FieldInfo fieldInfo;
attrQueryRows.GetFieldInformation(fieldInfo);
int fieldCount;
FieldType fieldType;
wstring fieldName;
// Iterate through the returned rows printing out all field values.
short shortField;
int32 longField;
float floatField;
double doubleField;
string stringField;
wstring wstringField;
tm dateTimeField;
char datetime[80];
Guid globalIDField;
Guid guidField;
wstring strGuid;
wstring strGlobalID;
bool isNull;
byte * shapeBufferBytes ;
ShapeBuffer geometry;
while (attrQueryRows.Next(attrQueryRow) == S_OK)
{
fieldInfo.GetFieldCount(fieldCount);
for (long fieldNumber = 0; fieldNumber < fieldCount; fieldNumber++)
{
fieldInfo.GetFieldType(fieldNumber, fieldType);
fieldInfo.GetFieldName(fieldNumber, fieldName);
attrQueryRow.IsNull(fieldName, isNull);
if (!isNull)
{
switch (fieldType)
{
case fieldTypeSmallInteger:
attrQueryRow.GetShort(fieldName, shortField);
wcout << shortField << endl;
break;
case fieldTypeInteger:
attrQueryRow.GetInteger(fieldName, longField);
wcout << longField << '\t';
break;
case fieldTypeSingle:
attrQueryRow.GetFloat(fieldName, floatField);
wcout << floatField << '\t';
break;
case fieldTypeDouble:
attrQueryRow.GetDouble(fieldName, doubleField);
wcout << doubleField << '\t';
break;
case fieldTypeString:
attrQueryRow.GetString(fieldName, wstringField);
wcout << wstringField << '\t';
break;
case fieldTypeDate:
attrQueryRow.GetDate(fieldName, dateTimeField);
strftime(datetime,80,"%a %b %d %I:%M:%S%p %Y", &dateTimeField);
wcout << datetime << '\t';
break;
case fieldTypeOID:
attrQueryRow.GetOID(longField);
wcout << longField << '\t';
break;
case fieldTypeGeometry:{
attrQueryRow.GetGeometry(geometry);
// attrQueryRow.GetBinary(fieldName, bss);
double x, y;
shapeBufferBytes = geometry.shapeBuffer;
memcpy(&x, geometry.shapeBuffer + 4, sizeof(x));
memcpy(&y, geometry.shapeBuffer + 12, sizeof(y));
//std::string *str3 = new std::string((char *)shapeBufferBytes);
wcout << "d::x::"<< x<<"y:"<<y<< '\t';;
wcout <<shapeBufferBytes<< "Geometry" << '\t';}
break;
case fieldTypeBlob:
wcout << "Blob" << '\t';
break;
case fieldTypeGUID:
attrQueryRow.GetGUID(fieldName, guidField);
guidField.ToString(strGuid);
wcout << strGuid << '\t';
break;
case fieldTypeGlobalID:
attrQueryRow.GetGlobalID(globalIDField);
globalIDField.ToString(strGlobalID);
wcout << strGlobalID << '\t';
break;
default:
break;
}
}
else
{
wcout << "null" << '\t';
}
}
wcout << endl;
}
attrQueryRows.Close(); // Close the EnumRows
// Close the geodatabase
if ((hr = CloseGeodatabase(geodatabase)) != S_OK)
{
wcout << "An error occurred while closing the geodatabase." << endl;
ErrorInfo::GetErrorDescription(hr, errorText);
wcout << errorText << "(" << hr << ")." << endl;
return ;
}
printf("Hello World geodatebase!\n");
return;
}
std::string jstring2str(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("GB2312");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr,mid,strencode);
jsize alen = env->GetArrayLength(barr);
jbyte* ba = env->GetByteArrayElements(barr,JNI_FALSE);
if(alen > 0)
{
rtn = (char*)malloc(alen+1);
memcpy(rtn,ba,alen);
rtn[alen]=0;
}
env->ReleaseByteArrayElements(barr,ba,0);
std::string stemp(rtn);
free(rtn);
return stemp;
}
jstring str2jstring(JNIEnv* env,const char* pat)
{
//定义java String类 strClass
jclass strClass = (env)->FindClass("Ljava/lang/String;");
//获取String(byte[],String)的构造器,用于将本地byte[]数组转换为一个新String
jmethodID ctorID = (env)->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
//建立byte数组
jbyteArray bytes = (env)->NewByteArray(strlen(pat));
//将char* 转换为byte数组
(env)->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
// 设置String, 保存语言类型,用于byte数组转换至String时的参数
jstring encoding = (env)->NewStringUTF("GB2312");
//将byte数组转换为java String,并输出
return (jstring)(env)->NewObject(strClass, ctorID, bytes, encoding);
}
JNIEXPORT jobject JNICALL Java_HelloJNI_getGdbTable(JNIEnv *env, jobject obj,jobject _GeodbEntity){
const char * dd="4096";
//jclass userClass = env->FindClass("GeodbEntity");
jclass userClass=env->GetObjectClass(_GeodbEntity);
//jmethodID userMethod = env->GetMethodID(userClass,"<init>","()V");
//jobject userObject = env->NewObject(userClass,userMethod);
//jmethodID construction_id = env->GetMethodID(userClass, "<init>", "()V");
//得到构造方法的ID
//jobject userObject = env->NewObject(userClass, construction_id);
//jobject userObject = env->AllocObject(userClass);
//
//jmethodID setName_method=env->GetMethodID(userClass,"setSrid","(Ljava/lang/String;)V");
//env->CallVoidMethod(userObject,setName_method,dd);
//jclass objectClass = (env)->FindClass("GeodbEntity
jmethodID methodId=env->GetMethodID(userClass,"getTable","()Ljava/lang/String;");
//调用customer对象的特定方法getName
jstring js_name=(jstring)env->CallObjectMethod(_GeodbEntity,methodId,NULL);
//jfieldID table = (env)->GetFieldID(userClass,"table","Ljava/lang/String;");
//jchar dd=(env)->GetCharField(userClass,table);
//std::string value;
//const jchar* raw = env->GetStringChars(js_name, NULL);
//if (raw != NULL) {
//jsize len = env->GetStringLength(js_name);
//value.assign(raw, raw + len);
//env->ReleaseStringChars(js_name, raw);
//}
cout<<jstring2str(env,js_name)<<endl;
jfieldID str = (env)->GetFieldID(userClass,"srid","Ljava/lang/String;");
(env)->SetObjectField(_GeodbEntity,str,(env)->NewStringUTF(dd));
//获取arraylist 类
jclass cls_ArrayList = env->FindClass("java/util/ArrayList");
//获得arraylist id
jmethodID construct = env->GetMethodID(cls_ArrayList,"<init>","()V");
//创建arraylist
jobject obj_ArrayList = env->NewObject(cls_ArrayList,construct);
//获取 arraylist 的add 方法
jmethodID arrayList_add = env->GetMethodID(cls_ArrayList,"add","(Ljava/lang/Object;)Z");
for(int i=0;i<10;i++){
jobject alistadd = (env)->NewStringUTF("my name is D:"+i);
//通过 add 方法 添加数据到 arraylist中
env->CallObjectMethod(obj_ArrayList,arrayList_add,alistadd);
}
jfieldID str1 = (env)->GetFieldID(userClass,"FieldValues","Ljava/util/ArrayList;");
(env)->SetObjectField(_GeodbEntity,str1,obj_ArrayList);
return _GeodbEntity;
}