Issue
How can I calculate total amount of transparent color from a given bitmap using ndk
Java code:
static {
System.loadLibrary("bitmap-processing");
}
public native void calculatePixel(Bitmap bitmap);
Cpp code
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;
}
As I am new to ndk trying out image processing
You can even rewrite the whole code
Solution
Since your pixel format is RGBA8888, every fourth byte contains its alpha value.
We can thus walk the bitmap line by line (where every line is info->stride bytes long), and there are info->height lines.
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);
Don't forget to AndroidBitmap_unlockPixels when you're done!
Answered By - Botje
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.