#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
相关推荐
应用市场9 小时前
用本机跑的开源音乐模型,给安卓小游戏做 20 秒无缝循环背景音乐企业数字化笔记9 小时前
表单发布前怎么验收?版本引用、字段兼容和历史数据回归生信分析笔记9 小时前
Nature 正刊 GPN-Star 基因组语言模型渣渣盟9 小时前
Flink 状态管理深度剖析:从 KeyedState 到 RocksDB,状态如何存储、恢复与扩缩容avi91119 小时前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpfOxYGC9 小时前
[工程] PHP2Shell 漏洞解析:两条 PHP 内核 RCE,为什么应用层的防护全部失效拥小抱1 天前
Compose 知识点文章永久免费只为良心1 天前
Web应用安全与防护_第三章_日志文件包含_从一道 CTFshow 题目讲透「日志投毒型 LFI → RCE」:原理、时序陷阱与全变种攻防手册AIsoft_86891 天前
学术讲座录音如何整理?录音APP可行吗Patrick在香港1 天前
一个斜杠,把毫秒变成十几秒