Ask Your Question
0

JNI call restarts the calling JAVA Activity

asked 2013-03-08 08:40:15 -0500

sumitsh87 gravatar image

updated 2013-03-08 08:45:52 -0500

I am stuck at a strange problem, pretty strange!!

Problem : In the following project, a native method is called from java android using JNI call. Whenever the native method returns, the android activity restarts (oncreate callback method is called again). I confirmed this using a Log inside the "oncreate()". It all results into a recursion!! why??

Please help :(

JAVA CODE

   public class Brahma extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.brahma);
    Log.v("Sumit","on create called");
    TextView text=(TextView)findViewById(R.id.textView);
    ImageView img=(ImageView)findViewById(R.id.imageView);
    Mat inputFrame= new Mat();
    String str =JNIMessage(inputFrame.getNativeObjAddr());
    text.setText(str);

    Bitmap bmp = Bitmap.createBitmap(inputFrame.cols(), inputFrame.rows(),    
                                             Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(inputFrame, bmp);
    img.setImageBitmap(bmp);


}


public static native String JNIMessage(long l);

static 
{
    if (!OpenCVLoader.initDebug()) 
    {
        // Handle initialization error
    } else
        System.loadLibrary("brahma");
}

}

NATIVE CODE

  using namespace std;
  using namespace cv;

  extern "C"
  {
 JNIEXPORT jstring JNICALL Java_com_example_brahma_Brahma_JNIMessage(JNIEnv* , jobject, jlong);

 JNIEXPORT jstring JNICALL Java_com_example_brahma_Brahma_JNIMessage(JNIEnv* env, jobject      
  thiz,jlong addr)
  {
   static int i=0;
   char buffer[100];
   sprintf(buffer, "native function called : time : %d", i++);
       __android_log_print(ANDROID_LOG_INFO, "Sumit", buffer);
   Mat& frame  = *(Mat*)addr;
       VideoCapture cap(0); // open the default camera
   cap>>frame;
   return env->NewStringUTF("Success");

  }

   }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-03-08 13:55:35 -0500

rics gravatar image

Are you sure that you can open the default camera? You simply return Success without a check.

First I would eliminate the videocapture handling from the native code and see if the native call works from the onCreate.

Anyway have you set the following in Android.mk?

OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on

Have you set camera rights in AndroidManifest.xml?

<uses-permission android:name="android.permission.CAMERA"/>

Do you specifically need static initialization? If so then you need to configure your environment according to this description. Otherwise it is recommended to use async initialization.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-03-08 08:40:15 -0500

Seen: 470 times

Last updated: Mar 08 '13