#include <jni.h>
#include <string>
#include <android/bitmap.h>
#include <cmath>
#include <android/log.h>
//定义TAG之后,我们可以在LogCat通过TAG过滤出NDK打印的日志
#define TAG "BitmapOperationNative"
// 定义info信息
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__)
void operationBitmap(JNIEnv *env, jobject bitmap, int srcColor, int dstColor) {
int result;
// 获取源Bitmap相关信息:宽、高等
AndroidBitmapInfo sourceInfo;
result = AndroidBitmap_getInfo(env, bitmap, &sourceInfo);
if (result < 0) {
LOGI(" AndroidBitmap_getInfo result=%d ", result);
return;
}
LOGI(" AndroidBitmapInfo format=%d srcColor=%x dstColor=%x", sourceInfo.format, srcColor,
dstColor);
// 获取源Bitmap像素数据 这里用的是32位的int类型 argb每个8位
uint32_t *sourceData;
//锁定像素的地址(不锁定的话地址可能会发生改变)
result = AndroidBitmap_lockPixels(env, bitmap, (void **) &sourceData);
if (result < 0) {
LOGI(" AndroidBitmap_lockPixels result=%d ", result);
return;
}
// 遍历各个像素点
int color;
int red, green, blue, alpha;
int width = sourceInfo.width;
int height = sourceInfo.height;
int w, h;
for (h = 0; h < height; h++) {
for (w = 0; w < width; w++) {
color = sourceData[h * width + w];
if (color == srcColor) {
sourceData[h * width + w] = dstColor;
}
}
}
AndroidBitmap_unlockPixels(env, bitmap);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_istep_bitmap_BitmapOperationNative_convertTo(JNIEnv *env, jclass clazz, jobject bitmap,
jint src, jint dst) {
operationBitmap(env, bitmap, src, dst);
}
Android JNI Bitmap指定颜色值替换
IstepGatlin2023-08-27 8:32
相关推荐
恋猫de小郭6 分钟前
Kotlin Toolchain 0.13,JetBrains 推荐 KMP 都切换到到全新工具链安卓机器35 分钟前
rom定制系列------小米8 安卓16线刷原生固件 体验新版本cyy29839 分钟前
小米电视安装tts引擎小锋学长生活大爆炸1 小时前
【工具】在 Android 手机上运行 DockerCarson带你学Android1 小时前
OpenAI Dots 登陆手机端:手机上派任务,AI 在云端写代码事圆则缓1 小时前
Android 依赖注入库到底应该怎么选大圣编蚕10 小时前
初探 NDK 的世界:从零开始理解 Android 原生开发云贝贝贝10 小时前
MySQL 慢查询定位与索引优化实战骑着蜗牛撵大象32711 小时前
服务下线不再炸:分层关闭、TCP 存活探测与负载均衡排水的落地套路三少爷的鞋13 小时前
别再这么写协程了!Marcin Moskała 剖析的 几个常见协程误区与重构