#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
相关推荐
敢敢のwings6 小时前
GR00T 2:当机器人学会先想象未来,再决定怎么动传奇开心果编程6 小时前
【Jetpack Compose娓娓道来】第55课:传感器与设备姿态——把物理世界的数据接进Compose传奇开心果编程7 小时前
【Jetpack Compose娓娓道来】第52课:蓝牙与位置权限——把“附近”和“在哪”安全地接进Composealexhilton8 小时前
卡顿定位:从 UI 线程到 RenderThreadFungLeo9 小时前
FengCMS· PHP+MySQL升级实录 · 后台参数统一治理:id[]数组、id=undefined 跳转与312组回调组合北斗星_And9 小时前
七年后再看 Android 底层:机制没变,路变短了王二蛋与他的张大花10 小时前
反编译后只剩 2 个类:一次自研 DEX 壳加固的完整复盘StarsGreen16 小时前
Android 12 中的“域“与“权限“介绍恋猫de小郭17 小时前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光FlightYe18 小时前
音视频修炼之容器与协议(六):HTTP 协议