[Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888
版权声明:本文为博主原创文章,未经博主允许不得转载。
一段简单的JNI例子,输入是Bitmap(需要是Mutable),结果是把Bitmap变成灰度图。
为了看起来有点价值,所以同时支持了RGB565和ARGB8888(囧rz)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <jni.h>
- #include <android/bitmap.h>
- #include <android/log.h>
- #ifndef eprintf
- #define eprintf(...) __android_log_print(ANDROID_LOG_ERROR,"@",__VA_ARGS__)
- #endif
- #define RGB565_R(p) ((((p) & 0xF800) >> 11) << 3)
- #define RGB565_G(p) ((((p) & 0x7E0 ) >> 5) << 2)
- #define RGB565_B(p) ( ((p) & 0x1F ) << 3)
- #define MAKE_RGB565(r,g,b) ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
- #define RGBA_A(p) (((p) & 0xFF000000) >> 24)
- #define RGBA_R(p) (((p) & 0x00FF0000) >> 16)
- #define RGBA_G(p) (((p) & 0x0000FF00) >> 8)
- #define RGBA_B(p) ((p) & 0x000000FF)
- #define MAKE_RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
- JNIEXPORT void JNICALL Java_com_yxcorp_hello_Effect_update
- (JNIEnv *env, jclass clazz, jobject zBitmap) {
- JNIEnv J = *env;
- if (zBitmap == NULL) {
- eprintf("bitmap is null\n");
- return;
- }
- // Get bitmap info
- AndroidBitmapInfo info;
- memset(&info, 0, sizeof(info));
- AndroidBitmap_getInfo(env, zBitmap, &info);
- // Check format, only RGB565 & RGBA are supported
- if (info.width <= 0 || info.height <= 0 ||
- (info.format != ANDROID_BITMAP_FORMAT_RGB_565 && info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)) {
- eprintf("invalid bitmap\n");
- J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "invalid bitmap");
- return;
- }
- // Lock the bitmap to get the buffer
- void * pixels = NULL;
- int res = AndroidBitmap_lockPixels(env, zBitmap, &pixels);
- if (pixels == NULL) {
- eprintf("fail to lock bitmap: %d\n", res);
- J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "fail to open bitmap");
- return;
- }
- eprintf("Effect: %dx%d, %d\n", info.width, info.height, info.format);
- int x = 0, y = 0;
- // From top to bottom
- for (y = 0; y < info.height; ++y) {
- // From left to right
- for (x = 0; x < info.width; ++x) {
- int a = 0, r = 0, g = 0, b = 0;
- void *pixel = NULL;
- // Get each pixel by format
- if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {
- pixel = ((uint16_t *)pixels) + y * info.width + x;
- uint16_t v = *(uint16_t *)pixel;
- r = RGB565_R(v);
- g = RGB565_G(v);
- b = RGB565_B(v);
- } else {// RGBA
- pixel = ((uint32_t *)pixels) + y * info.width + x;
- uint32_t v = *(uint32_t *)pixel;
- a = RGBA_A(v);
- r = RGBA_R(v);
- g = RGBA_G(v);
- b = RGBA_B(v);
- }
- // Grayscale
- int gray = (r * 38 + g * 75 + b * 15) >> 7;
- // Write the pixel back
- if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {
- *((uint16_t *)pixel) = MAKE_RGB565(gray, gray, gray);
- } else {// RGBA
- *((uint32_t *)pixel) = MAKE_RGBA(gray, gray, gray, a);
- }
- }
- }
- AndroidBitmap_unlockPixels(env, zBitmap);
- }
[Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888的更多相关文章
- Android 通过 JNI 访问 Java 字段和方法调用
在前面的两篇文章中,介绍了 Android 通过 JNI 进行基础类型.字符串和数组的相关操作,并描述了 Java 和 Native 在类型和签名之间的转换关系. 有了之前那些基础,就可以实现 Jav ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- Android For JNI(二)——C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器
Android For JNI(二)--C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器 当我们把Hello World写完之后,我们就可以迈入C的大门了,今天就来讲讲基本的一些数据类型 ...
- Android通过JNI实现与C语言的串口通讯操作蓝牙硬件模块
一直想写一份技术文档,但因为自感能力有限而无从下笔,近期做了个关于Android平台下实现与C语言的通讯来操作蓝牙模块的项目,中间碰到了很多问题,也在网上查了很多资料,在完毕主要功能后.也有一些人在网 ...
- android的JNI 、 NDK 学习!
转载的! Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) ...
- android的JNI标准 android的NDK
转载的! Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) ...
- 【转】Android中JNI的使用方法
Android中JNI的使用方法 首先看一下Android平台的框架图:(网上盗用) 可以看到Android上层的Application和ApplicationFramework都是使用Java编写, ...
- Android通过JNI调用驱动程序(完全解析实例)
要达到的目的:android系统中,用JAVA写界面程序,调用jni中间库提供的接口,去操作某个驱动节点,实现read,writer ioctl等操作!这对底层驱动开发人员是很重要的一个调试通道,也是 ...
- I.MX6 android BatteryService jni hacking
/**************************************************************************** * I.MX6 android Batter ...
随机推荐
- js技巧之this,call,apply
具体到实际应用中,this的指向又可以分为以下四种: 作为对象的方法调用 作为普通函数调用 构造器调用 apply和call调用 接下来我们去剖析前3点,至于第4点的apply和call调用,会在ca ...
- 自己造容器List
//自己造容器--List /* 1.iterator 2.头迭代器 3.尾迭代器 4.链表长 5.判空 6.清除 7.取头元素 8.取尾元素 9.头插入 10.尾插入 11.头删除 12.尾删除 1 ...
- use 2 stacks to simulate a queue
class Stack{ private: ; ]; public: void push(int n); int pop(); int peek(); int size(); }; void Stac ...
- STM32F407的串口采用DMA收发数据
源:STM32F407的串口采用DMA收发数据
- How can I create an Asynchronous function in Javascript?
哈哈:)我的codepen 的代码笔记是:http://codepen.io/shinewaker/pen/eBwPxJ --------------------------------------- ...
- hibernate的批量更新、批量删除
hibernate的批处理API:session.createQuery(hql).executeUpdate(),如果有参数则在执行之前设置参数. 批量更新示例: @Test public void ...
- Git单独checkout子目录
http://schacon.github.io/git/git-read-tree.html#_sparse_checkout Existing Repository If you already ...
- html5--基础笔记
1.对于<a>标签 以前: <h2><a href="index.html">The home page</a></h2> ...
- Minigui3.0 自定义遥控输入引擎
本人最近在从事minigui的开发工作,使用的gui版本为最新的3.0.12,平台环境为海思的HI3515. 在历经千辛万苦,终于将gui成功的移植到了开发板上,这里不多赘述,没有移植成功的朋友可以点 ...
- Sping3.0版本+Quartz完成定时任务
----------------------不使用注解在XML中配置完成定时任务---------------------- 1.需要导入的jar包 2.编写我们的定时任务的完成类 3.在Spring ...