gpt4 book ai didi

java - 使用 ndk 从位图计算像素

转载 作者:行者123 更新时间:2023-11-30 01:41:29 25 4
gpt4 key购买 nike

如何使用 ndk 计算给定位图的透明颜色总量

Java 代码:

    static {
System.loadLibrary("bitmap-processing");
}

public native void calculatePixel(Bitmap bitmap);

Cpp代码

extern "C" JNIEXPORT jobject JNICALL
Java_com_example_myapplication_CustomLayout_calculatePixel(JNIEnv *env, jobject thiz,
jobject bitmap) {
uint8_t *bitmapPixel;

AndroidBitmapInfo info;

if (AndroidBitmap_getInfo(env, bitmap, &info) < 0) {
__android_log_print(ANDROID_LOG_INFO, "bitmap-processing", "ret valude = %d",
AndroidBitmap_getInfo(env, bitmap, &info));
return NULL;
}

if ((AndroidBitmap_lockPixels(env, bitmap, static_cast<void **>((void *) bitmapPixel))) < 0){
__android_log_print(ANDROID_LOG_INFO, "bitmap-processing", "Bitmap type error");
return NULL;
}

struct pixel { uint8_t r, g, b, a; };
uint32_t num_transparent = 0;
for (int y = 0; y < info.height; y++) {
pixel* row = (pixel *)(bitmapPixel + y * info.stride);
for (int x = 0; x < info.width; x++) {
const pixel& p = row[x];
if (p.a == 0)
num_transparent++;
}
}

float proportion_transparent = float(num_transparent) / (info.width * info.height);

__android_log_print(ANDROID_LOG_INFO, "Bitmap-processing", "Transparent value : %f", proportion_transparent);

AndroidBitmap_unlockPixels(env, bitmap);
return nullptr;
}

因为我是 ndk 新手,尝试图像处理

您甚至可以重写整个代码

最佳答案

由于您的像素格式为 RGBA8888,因此每四个字节都包含其 alpha 值。因此,我们可以逐行遍历位图(其中每行都是 info->stride bytes 长),并且有 info->height行。

uint8_t* bitmapPixel;
if ((AndroidBitmap_lockPixels(env, bitmap, (void **)&bitmapPixel)) < 0){
__android_log_print(ANDROID_LOG_INFO, "bitmap-processing", "Bitmap type error");
return NULL;
}

struct pixel { uint8_t r, g, b, a; };
uint32_t num_transparent = 0;
for (int y = 0; y < info->height; y++) {
pixel* row = (pixel *)(bitmapPixel + y * info->stride);
for (int x = 0; x < info->width; x++) {
const pixel& p = row[x];
if (p.a == 0)
num_transparent++;
}
}

float proportion_transparent = float(num_transparent) / (info->width * info->height);

完成后不要忘记AndroidBitmap_unlockPixels!

关于java - 使用 ndk 从位图计算像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59820980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com