标签: androidbitmapjni
2014-05-09 20:35 2985人阅读 评论(1) 收藏 举报

版权声明:本文为博主原创文章,未经博主允许不得转载。

一段简单的JNI例子,输入是Bitmap(需要是Mutable),结果是把Bitmap变成灰度图。

为了看起来有点价值,所以同时支持了RGB565和ARGB8888(囧rz)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <jni.h>
  6. #include <android/bitmap.h>
  7. #include <android/log.h>
  8. #ifndef eprintf
  9. #define eprintf(...) __android_log_print(ANDROID_LOG_ERROR,"@",__VA_ARGS__)
  10. #endif
  11. #define RGB565_R(p) ((((p) & 0xF800) >> 11) << 3)
  12. #define RGB565_G(p) ((((p) & 0x7E0 ) >> 5)  << 2)
  13. #define RGB565_B(p) ( ((p) & 0x1F  )        << 3)
  14. #define MAKE_RGB565(r,g,b) ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
  15. #define RGBA_A(p) (((p) & 0xFF000000) >> 24)
  16. #define RGBA_R(p) (((p) & 0x00FF0000) >> 16)
  17. #define RGBA_G(p) (((p) & 0x0000FF00) >>  8)
  18. #define RGBA_B(p)  ((p) & 0x000000FF)
  19. #define MAKE_RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
  20. JNIEXPORT void JNICALL Java_com_yxcorp_hello_Effect_update
  21. (JNIEnv *env, jclass clazz, jobject zBitmap) {
  22. JNIEnv J = *env;
  23. if (zBitmap == NULL) {
  24. eprintf("bitmap is null\n");
  25. return;
  26. }
  27. // Get bitmap info
  28. AndroidBitmapInfo info;
  29. memset(&info, 0, sizeof(info));
  30. AndroidBitmap_getInfo(env, zBitmap, &info);
  31. // Check format, only RGB565 & RGBA are supported
  32. if (info.width <= 0 || info.height <= 0 ||
  33. (info.format != ANDROID_BITMAP_FORMAT_RGB_565 && info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)) {
  34. eprintf("invalid bitmap\n");
  35. J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "invalid bitmap");
  36. return;
  37. }
  38. // Lock the bitmap to get the buffer
  39. void * pixels = NULL;
  40. int res = AndroidBitmap_lockPixels(env, zBitmap, &pixels);
  41. if (pixels == NULL) {
  42. eprintf("fail to lock bitmap: %d\n", res);
  43. J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "fail to open bitmap");
  44. return;
  45. }
  46. eprintf("Effect: %dx%d, %d\n", info.width, info.height, info.format);
  47. int x = 0, y = 0;
  48. // From top to bottom
  49. for (y = 0; y < info.height; ++y) {
  50. // From left to right
  51. for (x = 0; x < info.width; ++x) {
  52. int a = 0, r = 0, g = 0, b = 0;
  53. void *pixel = NULL;
  54. // Get each pixel by format
  55. if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {
  56. pixel = ((uint16_t *)pixels) + y * info.width + x;
  57. uint16_t v = *(uint16_t *)pixel;
  58. r = RGB565_R(v);
  59. g = RGB565_G(v);
  60. b = RGB565_B(v);
  61. } else {// RGBA
  62. pixel = ((uint32_t *)pixels) + y * info.width + x;
  63. uint32_t v = *(uint32_t *)pixel;
  64. a = RGBA_A(v);
  65. r = RGBA_R(v);
  66. g = RGBA_G(v);
  67. b = RGBA_B(v);
  68. }
  69. // Grayscale
  70. int gray = (r * 38 + g * 75 + b * 15) >> 7;
  71. // Write the pixel back
  72. if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {
  73. *((uint16_t *)pixel) = MAKE_RGB565(gray, gray, gray);
  74. } else {// RGBA
  75. *((uint32_t *)pixel) = MAKE_RGBA(gray, gray, gray, a);
  76. }
  77. }
  78. }
  79. AndroidBitmap_unlockPixels(env, zBitmap);
  80. }

[Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888的更多相关文章

  1. Android 通过 JNI 访问 Java 字段和方法调用

    在前面的两篇文章中,介绍了 Android 通过 JNI 进行基础类型.字符串和数组的相关操作,并描述了 Java 和 Native 在类型和签名之间的转换关系. 有了之前那些基础,就可以实现 Jav ...

  2. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

  3. Android For JNI(二)——C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器

    Android For JNI(二)--C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器 当我们把Hello World写完之后,我们就可以迈入C的大门了,今天就来讲讲基本的一些数据类型 ...

  4. Android通过JNI实现与C语言的串口通讯操作蓝牙硬件模块

    一直想写一份技术文档,但因为自感能力有限而无从下笔,近期做了个关于Android平台下实现与C语言的通讯来操作蓝牙模块的项目,中间碰到了很多问题,也在网上查了很多资料,在完毕主要功能后.也有一些人在网 ...

  5. android的JNI 、 NDK 学习!

    转载的! Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) ...

  6. android的JNI标准 android的NDK

    转载的! Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) ...

  7. 【转】Android中JNI的使用方法

    Android中JNI的使用方法 首先看一下Android平台的框架图:(网上盗用) 可以看到Android上层的Application和ApplicationFramework都是使用Java编写, ...

  8. Android通过JNI调用驱动程序(完全解析实例)

    要达到的目的:android系统中,用JAVA写界面程序,调用jni中间库提供的接口,去操作某个驱动节点,实现read,writer ioctl等操作!这对底层驱动开发人员是很重要的一个调试通道,也是 ...

  9. I.MX6 android BatteryService jni hacking

    /**************************************************************************** * I.MX6 android Batter ...

随机推荐

  1. js技巧之this,call,apply

    具体到实际应用中,this的指向又可以分为以下四种: 作为对象的方法调用 作为普通函数调用 构造器调用 apply和call调用 接下来我们去剖析前3点,至于第4点的apply和call调用,会在ca ...

  2. 自己造容器List

    //自己造容器--List /* 1.iterator 2.头迭代器 3.尾迭代器 4.链表长 5.判空 6.清除 7.取头元素 8.取尾元素 9.头插入 10.尾插入 11.头删除 12.尾删除 1 ...

  3. use 2 stacks to simulate a queue

    class Stack{ private: ; ]; public: void push(int n); int pop(); int peek(); int size(); }; void Stac ...

  4. STM32F407的串口采用DMA收发数据

    源:STM32F407的串口采用DMA收发数据

  5. How can I create an Asynchronous function in Javascript?

    哈哈:)我的codepen 的代码笔记是:http://codepen.io/shinewaker/pen/eBwPxJ --------------------------------------- ...

  6. hibernate的批量更新、批量删除

    hibernate的批处理API:session.createQuery(hql).executeUpdate(),如果有参数则在执行之前设置参数. 批量更新示例: @Test public void ...

  7. Git单独checkout子目录

    http://schacon.github.io/git/git-read-tree.html#_sparse_checkout Existing Repository If you already ...

  8. html5--基础笔记

    1.对于<a>标签 以前: <h2><a href="index.html">The home page</a></h2> ...

  9. Minigui3.0 自定义遥控输入引擎

    本人最近在从事minigui的开发工作,使用的gui版本为最新的3.0.12,平台环境为海思的HI3515. 在历经千辛万苦,终于将gui成功的移植到了开发板上,这里不多赘述,没有移植成功的朋友可以点 ...

  10. Sping3.0版本+Quartz完成定时任务

    ----------------------不使用注解在XML中配置完成定时任务---------------------- 1.需要导入的jar包 2.编写我们的定时任务的完成类 3.在Spring ...