Dealing with bitmap object in android NDK
来源:互联网 发布:什么是网络优化 编辑:程序博客网 时间:2024/06/04 20:10
Reprinted from http://www.ibm.com/developerworks/cn/opensource/tutorials/os-androidndk/section5.html
First ,we will use android/bitmap.h,so the lowest version is Android 2.2(Froyo).
1.Transfer the bitmap object.
ibmphotophun.c:
/* * ibmphotophun.c * * Author: Frank Ableson * Contact Info: fableson@msiservices.com */#include <jni.h>#include <android/log.h>#include <android/bitmap.h>#define LOG_TAG "libibmphotophun"#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)typedef struct{ uint8_t alpha; uint8_t red; uint8_t green; uint8_t blue;} argb;/*convertToGrayPixel operation*/JNIEXPORT void JNICALL Java_com_wzh_test_NDKTestActivity_convertToGray(JNIEnv* env, jobject obj, jobject bitmapcolor,jobject bitmapgray){ AndroidBitmapInfo infocolor; void* pixelscolor; AndroidBitmapInfo infogray; void* pixelsgray; int ret; int y; int x; LOGI("convertToGray"); if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) { LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); return; } if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) { LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); return; } LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d", infocolor.width,infocolor.height,infocolor.stride,infocolor.format,infocolor.flags); if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { LOGE("Bitmap format is not RGBA_8888 !"); return; } LOGI("gray image :: width is %d; height is %d; stride is %d; format is %d;flags is %d", infogray.width,infogray.height,infogray.stride,infogray.format,infogray.flags); if (infogray.format != ANDROID_BITMAP_FORMAT_A_8) { LOGE("Bitmap format is not A_8 !");// return; } if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) { LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); } if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) { LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); } // modify pixels with image processing algorithm for (y=0;y<infocolor.height;y++) { argb * line = (argb *) pixelscolor; uint8_t * grayline = (uint8_t *) pixelsgray; for (x=0;x<infocolor.width;x++) { grayline[x] = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue; } pixelscolor = (char *)pixelscolor + infocolor.stride; pixelsgray = (char *) pixelsgray + infogray.stride; } LOGI("unlocking pixels"); AndroidBitmap_unlockPixels(env, bitmapcolor); AndroidBitmap_unlockPixels(env, bitmapgray);}
LOGI
和 LOGE
宏对 Logging 工具进行调用,并且在功能上分别相当于 Android SDK 中的 Log.i()
和 Log.e()
。具有 typedef
struct 关键字的 argb
数据类型支持 C 代码评估以 32 位整数存储的单个像素的 4 个数据元素。3 个包含语句为 C 编译器提供必要的声明,以进行 jni 粘合、日志记录和位图处理。
现在可以实现一些图像处理例程了,但是在我们检查代码之前,您需要了解 JNI 函数的命名约定。
当 Java 代码调用本机函数时,它将函数名称映射到一个展开或修饰函数,该函数由 JNI 共享库导出。下面是约定:Java_fully_qualified_classname_functionname
。
例如,convertToGray
函数在 C 代码中实现为 Java_com_msi_ibm_ndk_IBMPhotoPhun_convertToGray
。
JNI 函数的前两个参数包含一个 JNI 环境指针和调用类对象实例。有关 JNI 的更多信息,请参见 参考资料 部分。
构建库非常简单。打开终端(或 DOS)窗口并将目录更改到存储这些文件的 jni 文件夹。确保路径中有 NDK 并执行 ndk-build 脚本。此脚本包含构建库所需的所有粘合代码。生成的库放置在与 jni 文件夹级别相同的 libs 文件夹(例如,<project folder>/libs/)。
Eclipse 的 ADT 插件打包了 Android 应用程序后,会自动包含和传输库文件。会为每个支持的硬件平台都生成一个库文件。在运行时加载正确的库。
此函数有两个调用 Java 代码的参数:ARGB 格式中的彩色 Bitmap
和接收彩色图像的灰度版本的 8 位灰度 Bitmap
。下面是代码的简单介绍:
AndroidBitmapInfo
结构,在 bitmap.h 中定义,有助于了解Bitmap
对象。AndroidBitmap_getInfo
函数,在 jnigraphics 库中,获取有关具体Bitmap
对象的信息。- 下一步是确保传入到
convertToGray
函数的位图是想要的格式。 AndroidBitmap_lockPixels
函数锁定图像数据,这样您就可以直接在数据上执行操作。AndroidBitmap_unlockPixels
函数解锁之前锁定的像素数据。这些函数应该被称为“锁定/解锁对”。
Android.mk:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := ibmphotophunLOCAL_SRC_FILES := ibmphotophun.cLOCAL_LDLIBS := -llog -ljnigraphicsinclude $(BUILD_SHARED_LIBRARY)
其中,该 makefile(片段)指示 NDK:
- 将 ibmphotophun.c 源文件编译到共享库中。
- 命名共享库。默认情况下,共享库命名约定为 lib<modulename>.so。因此,在这里将生成文件命名为 libibmphotophun.so。
- 指定所需的 "input" 库。共享库依赖于两个用于日志记录(liblog.so)和 jni 图形(libjnigraphics.so)的内置库文件,日志记录库允许您为
LogCat
添加条目,这在项目的开发阶段很有用。图形库为使用 Android 位图及其图像数据提供例程。
libjnigraphics是从android 2.2开始引入的一个库,在NDK中可以通过引用
bitmap.h进行使用。这个库加快了NDK层访问java层的bitmap数据的速度,同时也
降低了内存消耗(少了一个临时中转用的int array)
细看了下,这个库实际上可以自己放到apk的lib下面进行调用。
这个库实际上只导入了三个外部函数,对bitmap的访问进行了封装
GraphicsJNI::getNativeBitmap
SkBitmap::lockPixels
SkBitmap::unlockPixels
这三个函数在目前所有版本的android里面都有。因此libjnigraphics.so实际上可以在目前所有版本的android中使用。只是低于android 2.2的版本,必须自己将 libjnigraphics.so打包进apk。但是需要注意的是目前新版ndk里面附带的libjnigraphics.so不行,那个只是个空壳。可以自己从比较老的版本的NDK里面拿出来使用,或者自己从android 2.2及以上的image里抠出来,具体路径/system/lib/libjnigraphics.so。 然后由于是打包进apk的so。所以需要自己在源码里面显示调用 system.loadlibrary(jnigraphics);
ibmphotophun.c 源文件包含一些 C 包含语句和 argb 类型(对应于 Android SDK 中的彩色数据类型)的定义。清单 4 显示了没有图像例程的 ibmphotophun.c,图像例程将在下一个清单中展示。
package com.wzh.test;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.os.Bundle;import android.widget.ImageView;import android.widget.TextView;public class NDKTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bitmap src=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); Bitmap dst=Bitmap.createBitmap(src.getWidth(), src.getHeight(), Config.ARGB_8888); convertToGray(src,dst); ImageView iv = new ImageView(this); iv.setImageBitmap(dst); setContentView(iv); } public native void convertToGray(Bitmap src,Bitmap dst); static { System.loadLibrary("ibmphotophun"); }}
转贴:http://www.cnblogs.com/qiengo/archive/2012/07/18/2597931.html
- Dealing with bitmap object in android NDK
- Dealing with unbalanced data in machine learning
- Dealing with Noise in Defect Prediction
- Dealing with the “Bitmap Size Exceeds VM Budget” error
- Dealing with Asset Compression in Android Apps(避免asset目录下文件被压缩)
- Internationalization in Struts framework dealing with 中文 乱码问题
- Three ways of dealing with Alert in automation(Python+Webdriver)
- Dealing With Memory Leaks in Unreal Engine 4
- Dealing with Adversity
- Use Android Hardware Decoder with OMXCodec in NDK
- Use Android Hardware Decoder with OMXCodec in NDK
- android with NDK
- NDK With Android Studio
- NDK With Android Studio
- 强参数 Strong parameters: Dealing with mass assignment in the controller instead of the model
- Dealing with WMI Timeouts (c#)
- XSLT(8) Dealing with namespace
- Dealing with Audio Output Hardware
- mysql load命令
- tomcat 内存回收机制配置
- C#读写记事本
- AFNetworking 学习笔记
- Linux文件缓冲区详解
- Dealing with bitmap object in android NDK
- HTML入门----简单网页制作
- SDP 协议分析
- XCode 4.5 提交App总是提示缺少“armv6”
- utf8编码文件的bom头
- Hibernate与 MyBatis的比较
- 将smartforms的输出结果另存为是PDF格式
- 在顺序存储模式下将所有的负数放到正数前面的三种算法
- Google莫斯科办公室里那个世界上最牛的程序员,这个程序员叫做Petr,他的感言...