What is JNI Graphics and how to use it?

本文介绍如何使用 JNI Graphics 库从 Android 的 Bitmap 类中获取位图缓冲区,以便在 C/C++ 中进行操作,如加载图像供 OpenGL ES 使用。文中详细说明了通过 Java 代码创建 Bitmap 对象,并将其传递给本地方法的过程,以及如何解锁像素缓冲区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://stackoverflow.com/questions/5605853/what-is-jni-graphics-or-how-to-use-it



The jnigraphics library can be used to access bitmap buffers in C/C++ from theandroid.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

It can be used to load images for e.g. OpenGL ES in C/C++, but you have to do some work to hand ajobject to that library so it can give you direct access to a buffer. You can pass that buffer to OpenGL via glTexImage2D().

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

}

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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值