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);
}
相关推荐
歪楼小能手3 小时前
Android16系统go版关闭重力旋转开关后缺失手动旋转屏幕悬浮按钮
android·java·平板
崇山峻岭之间3 小时前
Matlab学习记录37
android·学习·matlab
stevenzqzq6 小时前
Android 协程 Channel 菜鸟教程
android·channel
遗悲风6 小时前
PHP伪协议全面解析:原理、常用场景、攻防实战与安全防护
android·安全·php
撩得Android一次心动6 小时前
Android Lifecycle 全面解析:掌握生命周期管理的艺术(源码篇)
android·lifecycle
stevenzqzq6 小时前
android fow 限流
android·限流·flow
冬奇Lab8 小时前
Android 15 显示子系统深度解析(二):图形缓冲区管理与HWC硬件合成
android
wings专栏8 小时前
Android触摸事件分发记录
android
aaajj8 小时前
【Android】声控拍照例子
android
stevenzqzq8 小时前
Android MVI 中 setState(reduce: State.() -> State) 设计说明文档
android·mvi框架