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);
}
相关推荐
低调小一2 分钟前
Kuikly 小白拆解系列 · 第1篇|两棵树直调(Kotlin 构建与原生承载)
android·开发语言·kotlin
跟着珅聪学java17 分钟前
spring boot 整合 activiti 教程
android·java·spring
川石课堂软件测试2 小时前
全链路Controller压测负载均衡
android·运维·开发语言·python·mysql·adb·负载均衡
2501_915921432 小时前
iOS 26 电耗监测与优化,耗电问题实战 + 多工具 辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915921432 小时前
苹果软件混淆与 iOS 应用加固白皮书,IPA 文件加密、反编译防护与无源码混淆方案全解析
android·ios·小程序·https·uni-app·iphone·webview
倔强的石头1063 小时前
【Linux指南】Linux命令行进度条实现原理解析
android·linux
yeziyfx4 小时前
Android Studio制作.9图(点9图/9Patch图)
android·ide·android studio
Chejdj5 小时前
StateFlow、SharedFlow 和LiveData区别
android·面试
蓝不蓝编程5 小时前
AI编程作品:Android 极简倒计时应用
android·ai编程
小仙女喂得猪5 小时前
2025 Android原生开发者角度的Flutter 笔记整理(对比ReactNative)
android·flutter·react native