I don’t know how to address this properly. But I will briefly discuss what I am trying to do.
We all know that there is no direct way of accessing the camera directly from Cocos2d-x. The only way to do this is to use JNI. Let the camera be called through Android code, save the image to disk and let the Cocos2dx game access that image for use.
That is exactly what I am doing with my code. I saw this code somewhere online and try to do the same thing with my code.
The only difference is that I am accessing the method whose signature is just void(). Since I am going to call the camera using this trivial code:
public static void callPhoneCamera()
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(mSelf.getPackageManager()) != null)
mSelf.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
Here is my JNI related code:
JniLink.cpp
#include "../../cocos2d/cocos/platform/android/jni/JniHelper.h"
#include "../proj.android/jni/JniLink.h"
extern "C"
{
void callPhoneCamera()
{
cocos2d::JniMethodInfo t;
if(cocos2d::JniHelper::getStaticMethodInfo(
t
, "com/indigo/android/mysamplecamera/AppActivity"
, "callPhoneCamera"
, "()V"))
{
t.env->CallStaticVoidMethod(t.classID , t.methodID);
}
}
}
JniLink.hpp
#ifndef JNILINK_H_
#define JNILINK_H_
extern "C"
{
extern void callPhoneCamera();
}
#endif
This compiles fine until during run time it ANRs on the emulator for Android 5 and on my phone Android 4.
Also I really don’t understand the semantics of paramCode. On the link I provided it was written this way:
(Ljava/lang/String;)V
I understand the L being the type. I don’t understand V. I assume that it is the return type which is void, thus ‘V’. With this in mind, I wrote it as “()V” since the function doesn’t have parameter at all and the return type is void.
Can you please help me on how to go about this? I haven’t got the chance to learn JNI and I still have a long way to go.
How can I call ‘callPhoneCamera’, a Java function, into a cpp code?
Thank you!