Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a string to C-code in Android NDK

The program should take an Image from the SD card and adjust its brightness. And the image is taken from the SD card via the NDK C-code. It is to be noted that the string depicting the path to the image is passed to the NDK via JNI.

Java code:

private void adjustBrightness() {
   imagePath     = (Environment.getExternalStorageDirectory().getPath()+"earthglobe.jpeg").toCharArray();
   brightness(imagePath, brightness);
}

public native void brightness(char[] imagePath, float brightness);

NDK code:

JNIEXPORT void JNICALL Java_com_example_ImageActivity_brightness(JNIEnv * env,char[] bitmappath, jfloat brightnessValue)
{
   string bmpath    =   bitmappath+'\0';
   jobject  obj = fopen( bitmappath , "rb" );
}
like image 805
Sreekanth Karumanaghat Avatar asked Jul 19 '12 10:07

Sreekanth Karumanaghat


Video Answer


1 Answers

You cannot pass char[] this way.

In Java use:

public static native void brightness(String imagePath, float brightness);

In native use:

std::string ConvertJString(JNIEnv* env, jstring str)
{
   if ( !str ) std::string();

   const jsize len = env->GetStringUTFLength(str);
   const char* strChars = env->GetStringUTFChars(str, (jboolean *)0);

   std::string Result(strChars, len);

   env->ReleaseStringUTFChars(str, strChars);

   return Result;
}

JNIEXPORT void JNICALL Java_com_example_ImageActivity_brightness(JNIEnv * env, jobject obj, jstring bitmappath, jfloat brightnessValue)
{
   std::string bmpath = ConvertJString( env, bitmappath );
   FILE* f = fopen( bmpath.c_str(), "rb" );

   // do something useful here

   fclose( f );
}
like image 115
Sergey K. Avatar answered Sep 27 '22 22:09

Sergey K.