Android JNI Bitmap指定颜色值替换

复制代码
#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);
}
相关推荐
爱吃牛肉的大老虎12 分钟前
MySQL优化之系统表分析SQL
android·sql·mysql
Fate_I_C21 分钟前
实战案例:用 Kotlin 重写一个 Java Android 工具类
android·java·kotlin
Fate_I_C22 分钟前
Kotlin 特有语法糖
android·开发语言·kotlin
Fate_I_C38 分钟前
Kotlin 为什么是 Android 开发的首选语言
android·开发语言·kotlin
黄林晴40 分钟前
Android CLI 来了!终端一键建项目、控模拟器、给 Agent 喂官方规范
android
常利兵43 分钟前
Kotlin 助力 Android 启动“大提速”
android·开发语言·kotlin
撩得Android一次心动1 小时前
Android DataBinding 全面解析【源码篇2】
android·源码·android jetpack·databinding
守月满空山雪照窗1 小时前
图形 API 体系解析:Android Vulkan / OpenGL 与主流图形 API 对比
android
我命由我123451 小时前
Android 开发,getSystemService 警告信息:Must be one of: Context. POWER_SERVICE ...
android·java·java-ee·android studio·android jetpack·android-studio·android runtime