2012-07-19 15 views
8

Il programma dovrebbe scattare un'immagine dalla scheda SD e regolarne la luminosità. E l'immagine è presa dalla scheda SD tramite il codice C NDK. Si noti che la stringa che rappresenta il percorso dell'immagine viene passata all'NDK tramite JNI.Passaggio di una stringa a codice C in Android NDK

codice Java:

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

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

codice NDK:

JNIEXPORT void JNICALL Java_com_example_ImageActivity_brightness(JNIEnv * env,char[] bitmappath, jfloat brightnessValue) 
{ 
    string bmpath = bitmappath+'\0'; 
    jobject obj = fopen(bitmappath , "rb"); 
} 
+0

Questo è ridicolo: '' jobject obj = fopen (bitmappath, "rb"); '' –

risposta

19

Non è possibile passare char [] in questo modo.

In Java usa:

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

In uso origini:

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

    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); 
} 
+0

Grazie, voglio la bitmap come un Jobject e non un file, perché esiste già una funzione per recuperare i pixel dal Jobject ... C'è un modo per farlo? –

+0

Usa java.nio.ByteBuffer per passare i pixel in giro. –

+0

Penso che tu mi abbia sbagliato ... o voglio recuperare il bmp come JObject (in NDK) o ho bisogno di ottenere un metodo per ottenere pixel dal file nel NDK. –