#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
相关推荐
幻雨様1 小时前
UE5多人MOBA+GAS 45、制作冲刺技能Jerry说前后端3 小时前
Android 数据可视化开发:从技术选型到性能优化Meteors.4 小时前
Android约束布局(ConstraintLayout)常用属性alexhilton4 小时前
玩转Shader之学会如何变形画布whysqwhw8 小时前
安卓图片性能优化技巧风往哪边走9 小时前
自定义底部筛选弹框Yyyy4829 小时前
MyCAT基础概念Android轮子哥10 小时前
尝试解决 Android 适配最后一公里雨白11 小时前
OkHttp 源码解析:enqueue 非同步流程与 Dispatcher 调度风往哪边走11 小时前
自定义仿日历组件弹框