#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
相关推荐
小成很成5 小时前
mysql 8.0.21 升级 mysql 8.0.45(经过生产环境验证)虎王物联9 小时前
PHP实现MQTT消息处理:物联网设备数据接收与指令下发我命由我1234510 小时前
Android 开发 - 获取当前设备屏幕的旋转角度程序员-Benothing11 小时前
MySQL 索引下推是什么?——深入理解 ICP 原理与实践怀化纱厂球迷12 小时前
百度地图--android接入百度地图导航AFinalStone12 小时前
Android 7系统无障碍服务(四)AccessibilityEvent 的产生与投递智购科技无人售货机工厂13 小时前
2026自动售货机热敏打印模块集成:从串口驱动到小票自动裁切的工程实践~YHA133455513 小时前
苹果安卓手机播放器推荐:2026无广告4K怎么选hunterandroid15 小时前
ContentProvider 跨进程数据共享实战