什么是JNI的图形或如何使用它? [英] What is JNI Graphics or how to use it?
问题描述
在Android NDK中有一个名为JNI图形库。这是什么?我可以用它来装载图像的OpenGL ES与C / C ++?
In the Android NDK there is a library named JNI Graphics. What is that? Can I use that to load Images for OpenGL ES with C/C++?
推荐答案
在 jnigraphics
库可用于从<$ C $访问C / C位图缓存++ C> android.bitmap.Graphics 类(当然,在Java中,)。它的详细随附NDK下的文档中描述的:
The jnigraphics
library can be used to access bitmap buffers in C/C++ from the android.bitmap.Graphics
class (in Java, of course). It's described in more detail in the documentation that comes with the NDK under:
android-ndk-r5b/docs/STABLE-APIS.html
有可用于载入图像为例如OpenGL ES的在C / C ++,但你必须做一些工作交给一个 jobject
到库,以便它可以给你直接进入缓冲区。您可以传递一个缓冲区的OpenGL通过 glTexImage2D()
。
It can be used to load images for e.g. OpenGL ES in C/C++, but you have to do some work to hand a jobject
to that library so it can give you direct access to a buffer. You can pass that buffer to OpenGL via glTexImage2D()
.
首先,您需要一个Java 位图
的对象,您可以获取并传递给你的本地方法是这样的:
First, you need a Java Bitmap
object, which you can acquire and pass to your native method like this:
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
...
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.myimage, options);
MyJniMethod(bitmap); // Should be static in this example
这是本机方法可以是这个样子:
That native method can look something like this:
#include <android/bitmap.h>
void MyJniMethod(JNIEnv *env, jobject obj, jobject bitmap) {
AndroidBitmapInfo info;
uint32_t *pixels;
int ret;
AndroidBitmap_getInfo(env, bitmap, &info);
if(info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGE("Bitmap format is not RGBA_8888!");
return false;
}
AndroidBitmap_lockPixels(env, bitmap, reinterpret_cast<void **>(&pixels));
// Now you can use the pixel array 'pixels', which is in RGBA format
}
请记住,你应该叫 AndroidBitmap_unlockPixels()
当你使用像素缓冲区完成的,这个例子不检查错误的。
Keep in mind you should call AndroidBitmap_unlockPixels()
when you are done with the pixel buffer, and that this example doesn't check for errors at all.
更新希德·达塔的问题:你能保证输出图像格式为你加入这个期权上述预期是什么:
Update for Sid Datta's question: You can ensure that the output image format is what you're expecting by adding this to options above:
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
还有一个情况下输出图像的还是的结束与一个未知格式JNI。这似乎只发生与GIF格式。调用后 BitmapFactory.de codeResource()
,你可以将图像转换为正确的格式,如果必要的:
There is one case where the output image will still end up with an unknown format in JNI. This seems to happen only with GIFs. After calling BitmapFactory.decodeResource()
, you can convert the image to the proper format if necessary:
if (bitmap.getConfig() != Bitmap.Config.ARGB_8888) {
Bitmap reformatted_bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);
bitmap.recycle(); /* reduce memory load in app w/o waiting for GC */
bitmap = reformatted_bitmap;
}
这篇关于什么是JNI的图形或如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!