什么是 JNI Graphics 或如何使用它? [英] What is JNI Graphics or how to use it?

查看:45
本文介绍了什么是 JNI Graphics 或如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android NDK 中有一个名为 JNI Graphics 的库.那是什么?我可以用它来加载带有 C/C++ 的 OpenGL ES 的图像吗?

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 库可用于从 android.bitmap.Graphics 类(当然是在 Java 中).

The jnigraphics library can be used to access bitmap buffers in C/C++ from the android.bitmap.Graphics class (in Java, of course).

NDK 文档以及 code>bitmap.h 头文件.

它可用于加载图像,例如C/C++ 中的 OpenGL ES,但您必须做一些工作才能将 jobject 交给该库,以便它可以让您直接访问缓冲区.您可以通过 glTexImage2D() 将该缓冲区传递给 OpenGL.

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 Bitmap 对象,您可以像这样获取并传递给您的本机方法:

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.

更新 Sid Datta 的问题:您可以通过将其添加到上面的选项来确保输出图像格式是您所期望的:

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.decodeResource()后,可以根据需要将图片转换成合适的格式:

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 Graphics 或如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆