我需要通过JNI将图像数据从java端传递给cocos2d-x。我如何实现它? JNI函数的参数应该是什么以及如何在cocos2d-x一侧投射?如何通过JNI将图像从Java传递到Cocos2d-x?
2
A
回答
3
创建Java接口forJNI像:
public static native void setBG(int[] raw, int width, int height);
在C
++代码做:
//Use static variable here for simplicity int *imagedata; int staticwidth; int staticheight; Texture2D *userBackgroundImage; void Java_com_my_company_JniHelper_setBG(JNIEnv* env, jobject thiz, jintArray raw, jint width, jint height) { jint *carr; carr = env->GetIntArrayElements(raw, 0); if(carr == NULL) { return; /* exception occurred */ } ssize_t dataLen = (int)width * (int)height; int *data = new int[dataLen]; for (long i = 0; i < dataLen; i++) { data[i] = carr[i]; } imagedata = data;//Make a copy because it need to be done in GLThread staticwidth = (int)width; staticheight = (int)height; env->ReleaseIntArrayElements(raw, carr, 0); LOGD("set image: %d * %d", width, height); }
然后调用以下方法某处持续时间层init或其他cocos2d- x代码:
void createImage(const void *data, ssize_t dataLen, int width, int height)
{
Texture2D *image = new Texture2D();
if (!image->initWithData(data, dataLen, Texture2D::PixelFormat::BGRA8888, width, height, Size(width, height)))
{
delete image;
delete imagedata;
image = NULL;
imagedata = NULL;
userBackgroundImage = NULL;
return;
}
delete imagedata;
imagedata = NULL;
userBackgroundImage = image;
}
然后,您可以使用Texture2D对象来创建一个精灵或做任何你想做
从Java调用此代码:
public static int[] BitmapToRaw(Bitmap bitmap) { Bitmap image = bitmap.copy(Bitmap.Config.ARGB_8888, false); int width = image.getWidth(); int height = image.getHeight(); int[] raw = new int[width * height]; image.getPixels(raw, 0, width, 0, 0, width, height); return raw; } Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.bg); JniHelper.setBG(BitmapToRaw(image), image.getWidth(), image.getHeight());
1
我只是从cocos2d-x发送图像数据到Java,所以你需要找到一种方法来颠倒这种方法。它用于捕获节点并将其传递给屏幕截图。
CCNode* node = <some node>;
const CCSize& size(node->getContentSize());
CCRenderTexture* render = CCRenderTexture::create(size.width, size.height);
// render node to the texturebuffer
render->clear(0, 0, 0, 1);
render->begin();
node->visit();
render->end();
CCImage* image = render->newCCImage();
// If we don't clear then the JNI call gets corrupted.
render->clear(0, 0, 0, 1);
// Create the array to pass in
jsize length = image->getDataLen();
jintArray imageBytes = t.env->NewIntArray(length);
unsigned char* imageData = image->getData();
t.env->SetIntArrayRegion(imageBytes, 0, length, const_cast<const jint*>(reinterpret_cast<jint*>(imageData)));
t.env->CallStaticVoidMethod(t.classID, t.methodID, imageBytes, (jint)image->getWidth(), (jint)image->getHeight());
image->release();
t.env->DeleteLocalRef(imageBytes);
t.env->DeleteLocalRef(t.classID);
Java方面看起来是这样的:
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
public static Bitmap getImage(int[] imageData, int width, int height) {
Bitmap image = Bitmap.createBitmap(width, height, Config.ARGB_8888);
image.setPixels(imageData, 0, width, 0, 0, width, height);
return image;
}
0
我认为这样做会用Java来保存它,然后访问来自CPP文件,然后使用后删除它最好的,简单的方法。
相关问题
- 1. 如何通过JNI将大型结构从C传递到JAVA
- 2. 如何在Android开发中通过JNI将位图从Java传递给C++?
- 3. 如何通过ajax将图像从html传递到php
- 4. 通过jni传递一个2d矩阵从java到C++文件
- 5. 将图像从java applet传递到html
- 6. 如何使用JNI将System.out从Java传递到C?
- 7. JNI将树形图从java传递到c
- 8. JNI:从C++传递字节到java
- 9. 传递多个参数从Java通过JNI
- 10. 通过JNI(安卓NDK)传递由C到Java对象(DisplayMetrics)
- 11. 如何通过C++通过JNI传递扩展的ASCII字符
- 12. 通过UINavigationController传递图像
- 13. 如何将Mat对象从java类传递给jni cpp。
- 14. 将OpenCV轮廓从JNI C++函数传递到Java中的Android
- 15. JNI - 将HTML文件的内容从C端传递到Java端
- 16. 将指针从JNI传递到Java使用long
- 17. 将ANSI字符串从Java传递到C++(JNI)
- 18. Android:将数据从jni线程传递到java程序
- 19. JNI通过引用传递失败
- 20. Android Cocos2dX JNI Bridge
- 21. 将QByteArray图像从C++传递到QML
- 22. 将图像从Form2传递到Form1
- 23. 如何通过使用JNI将C结构转移到java?
- 24. 通过单击图像将图像ID传递给JQuery
- 25. 通过Android将图像上传到wordpress
- 26. 如何通过JNI发送从Java到H的HashMap
- 27. 如何通过jquery将图像数据与图像一起传递?
- 28. 如何通过java传递JMeter属性
- 29. 如何通过node.js将cookie从couchd传递到客户端
- 30. 使用prepareforsegue将图像从按钮传递到图像视图