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);
}
相关推荐
川石课堂软件测试11 分钟前
什么是BUG,你对BUG的了解有多少?
android·linux·服务器·python·功能测试·bug·安全性测试
玩机达人883 小时前
三星S25Ultra/S24安卓16系统Oneui8成功获取完美root权限+LSP框架
android·linux·里氏替换原则
居安思危_Ho4 小时前
RK平台Uniapp自启动缓存问题解决
android·缓存·uni-app·rk平台·uniapp资源文件
molong9314 小时前
Activity/Service/Broadcast/ContentProvider 生命周期交互
android·学习·交互
molong9317 小时前
Android 权限模型(前台、后台、特殊权限)
android
怪兽20147 小时前
Looper、MessageQueue、Message及Handler的关系是什么?如何保证MessageQueue的并发访问安全?
android·面试
奥尔特星云大使7 小时前
mysql高可用架构之MHA部署(二)VIP漂移(保姆级)
android·mysql·架构·mha·ip漂移
深海呐9 小时前
Android 编译速度优化:JVM堆内存扩充
android·jvm·jvm内存扩充·android 加快编译速度
心随雨下9 小时前
Flutter中新手需要掌握的几种Widget
android·flutter·ios